1class ImportConstituenciesJob < ApplicationJob
 
2  HOST = "http://data.parliament.uk"
 
3  ENDPOINT = "/membersdataplatform/services/mnis/ReferenceData/Constituencies/"
 
4  TIMEOUT = 30
 
5  UTF_BOM = /\A\xEF\xBB\xBF/
 
6
 
7  rescue_from StandardError do |exception|
 
8    Appsignal.send_exception exception
 
 9  end
 
  • DuplicateMethodCall - calls 'row["ONSCode"]' 2 times » reek
  • FeatureEnvy - refers to 'constituency' more than self (maybe move it to another class?) » reek
  • TooManyStatements - has approx 6 statements » reek
  • Complexity 3 » saikuro
11  def perform
 
12    current_constituencies.each do |row|
 
13      constituency = Constituency.find_or_initialize_by(external_id: row["Constituency_Id"])
 
 
15      constituency.name = row["Name"]
 
16      constituency.ons_code = row["ONSCode"]
 
17      constituency.example_postcode = example_postcodes[row["ONSCode"]]
 
 
19      if constituency.changed? || constituency.new_record?
 
20        constituency.save!
 
21      end
 
22    end
 
23  end
 
 
25  private
 
  • FeatureEnvy - refers to 'body' more than self (maybe move it to another class?) » reek
  • Complexity 2 » saikuro
27  def constituencies
 
28    response = fetch_constituencies
 
 
30    # The response from the Parliament API includes a BOM (byte-order mark)
 
31    # and JSON.parse barfs on this so remove it if it is present
 
32    body = response.body.force_encoding("utf-8")
 
33    body = body[1..-1] if body =~ UTF_BOM
 
34    json = JSON.parse(body)
 
 
36    json["Constituencies"]["Constituency"]
 
37  end
 
  • NilCheck - performs a nil-check » reek
  • UncommunicativeVariableName - has the variable name 'c' » reek
  • Complexity 2 » saikuro
39  def current_constituencies
 
40    constituencies.select { |c| c["EndDate"].nil? }
 
41  end
 
  • UncommunicativeVariableName - has the variable name 'f' » reek
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 2 » saikuro
43  def faraday
 
44    Faraday.new(HOST) do |f|
 
45      f.response :follow_redirects
 
46      f.response :raise_error
 
47      f.adapter :net_http_persistent
 
48    end
 
49  end
 
  • DuplicateMethodCall - calls 'request.options' 2 times » reek
  • FeatureEnvy - refers to 'request' more than self (maybe move it to another class?) » reek
  • Complexity 2 » saikuro
51  def fetch_constituencies
 
52    faraday.get(ENDPOINT) do |request|
 
53      request.headers["Accept"] = "application/json"
 
54      request.options[:timeout] = TIMEOUT
 
55      request.options[:open_timeout] = TIMEOUT
 
56    end
 
57  end
 
  • Complexity 1 » saikuro
59  def example_postcodes
 
60    @example_postcodes ||= YAML.load_file(Rails.root.join("data", "example_postcodes.yml"))
 
61  end
 
62end