Updated

app/jobs / import_constituencies_job.rb

B
62 lines of codes
6 methods
7.6 complexity/method
1 churn
45.75 complexity
17 duplications
class ImportConstituenciesJob < ApplicationJob
  1. ImportConstituenciesJob has no descriptive comment
HOST = "http://data.parliament.uk" ENDPOINT = "/membersdataplatform/services/mnis/ReferenceData/Constituencies/" TIMEOUT = 30 UTF_BOM = /\A\xEF\xBB\xBF/ rescue_from StandardError do |exception| Appsignal.send_exception exception end def perform
  1. ImportConstituenciesJob#perform has approx 6 statements
current_constituencies.each do |row| constituency = Constituency.find_or_initialize_by(external_id: row["Constituency_Id"]) constituency.name = row["Name"]
  1. ImportConstituenciesJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
constituency.ons_code = row["ONSCode"]
  1. ImportConstituenciesJob#perform calls 'row["ONSCode"]' 2 times Locations: 0 1
  2. ImportConstituenciesJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
constituency.example_postcode = example_postcodes[row["ONSCode"]]
  1. ImportConstituenciesJob#perform calls 'row["ONSCode"]' 2 times Locations: 0 1
  2. ImportConstituenciesJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
if constituency.changed? || constituency.new_record?
  1. ImportConstituenciesJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
constituency.save!
  1. ImportConstituenciesJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2 3 4
end end end private def constituencies response = fetch_constituencies # The response from the Parliament API includes a BOM (byte-order mark) # and JSON.parse barfs on this so remove it if it is present body = response.body.force_encoding("utf-8") body = body[1..-1] if body =~ UTF_BOM
  1. ImportConstituenciesJob#constituencies refers to 'body' more than self (maybe move it to another class?)
json = JSON.parse(body) json["Constituencies"]["Constituency"] end def current_constituencies constituencies.select { |c| c["EndDate"].nil? }
  1. ImportConstituenciesJob#current_constituencies performs a nil-check
  2. ImportConstituenciesJob#current_constituencies has the variable name 'c'
end def faraday
  1. Identical code found in 2 nodes Locations: 0 1
  2. ImportConstituenciesJob#faraday doesn't depend on instance state (maybe move it to another class?)
Faraday.new(HOST) do |f|
  1. ImportConstituenciesJob#faraday has the variable name 'f'
f.response :follow_redirects f.response :raise_error f.adapter :net_http_persistent end end def fetch_constituencies faraday.get(ENDPOINT) do |request| request.headers["Accept"] = "application/json"
  1. ImportConstituenciesJob#fetch_constituencies refers to 'request' more than self (maybe move it to another class?) Locations: 0 1 2
request.options[:timeout] = TIMEOUT
  1. ImportConstituenciesJob#fetch_constituencies calls 'request.options' 2 times Locations: 0 1
  2. ImportConstituenciesJob#fetch_constituencies refers to 'request' more than self (maybe move it to another class?) Locations: 0 1 2
request.options[:open_timeout] = TIMEOUT
  1. ImportConstituenciesJob#fetch_constituencies calls 'request.options' 2 times Locations: 0 1
  2. ImportConstituenciesJob#fetch_constituencies refers to 'request' more than self (maybe move it to another class?) Locations: 0 1 2
end end def example_postcodes @example_postcodes ||= YAML.load_file(Rails.root.join("data", "example_postcodes.yml")) end end