|
1class ImportConstituenciesJob < ApplicationJob
|
|
2 HOST = "http://data.parliament.uk"
|
|
3 ENDPOINT = "/membersdataplatform/services/mnis/ReferenceData/Constituencies/"
|
|
|
|
5 UTF_BOM = /\A\xEF\xBB\xBF/
|
|
|
|
7 rescue_from StandardError do |exception|
|
|
8 Appsignal.send_exception exception
|
|
|
|
|
- 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
|
|
|
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?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- FeatureEnvy - refers to 'body' more than self (maybe move it to another class?) » reek
- Complexity 2 » saikuro
|
|
|
28 response = fetch_constituencies
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
- 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? }
|
|
|
|
|
- UncommunicativeVariableName - has the variable name 'f' » reek
- UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
- Complexity 2 » saikuro
|
|
|
44 Faraday.new(HOST) do |f|
|
|
45 f.response :follow_redirects
|
|
46 f.response :raise_error
|
|
47 f.adapter :net_http_persistent
|
|
|
|
|
|
|
- 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
|
|
|
|
|
|
|
|
|
|
60 @example_postcodes ||= YAML.load_file(Rails.root.join("data", "example_postcodes.yml"))
|
|
|
|
|