Updated

app/jobs / refresh_constituency_party_job.rb

C
75 lines of codes
6 methods
8.1 complexity/method
1 churn
48.54 complexity
57 duplications
require 'faraday' class RefreshConstituencyPartyJob < ApplicationJob
  1. RefreshConstituencyPartyJob has no descriptive comment
  2. RefreshConstituencyPartyJob has 7 constants
HOST = "http://data.parliament.uk" ENDPOINT = "/membersdataplatform/services/mnis/members/query/House=Commons%7CIsEligible=true/" TIMEOUT = 5 MEMBERS = "//Members/Member" MP_ID = "./@Member_Id" MP_NAME = "./FullTitle" MP_PARTY = "./Party" rescue_from StandardError do |exception| Appsignal.send_exception exception end def perform Constituency.find_each do |constituency| if member = members[constituency.mp_id]
  1. RefreshConstituencyPartyJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2
constituency.update!(party: member[:party])
  1. RefreshConstituencyPartyJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2
else constituency.update!(mp_id: nil, mp_name: nil, mp_date: nil, party: nil)
  1. RefreshConstituencyPartyJob#perform refers to 'constituency' more than self (maybe move it to another class?) Locations: 0 1 2
end end end private def members @members ||= load_members end def load_members
  1. RefreshConstituencyPartyJob#load_members has approx 6 statements
response = fetch_members if response.success? Hash[parse(response.body)] else {} end rescue Faraday::Error => e
  1. RefreshConstituencyPartyJob#load_members has the variable name 'e'
Appsignal.send_exception(e) return {} end def parse(body)
  1. RefreshConstituencyPartyJob#parse has approx 6 statements
  2. RefreshConstituencyPartyJob#parse doesn't depend on instance state (maybe move it to another class?)
xml = Nokogiri::XML(body) xml.xpath(MEMBERS).map do |node| [ node.xpath(MP_ID).text,
  1. RefreshConstituencyPartyJob#parse calls 'node.xpath(MP_ID)' 2 times Locations: 0 1
  2. RefreshConstituencyPartyJob#parse calls 'node.xpath(MP_ID).text' 2 times Locations: 0 1
{}.tap { |attrs| attrs[:id] = node.xpath(MP_ID).text
  1. Similar code found in 2 nodes Locations: 0 1
  2. RefreshConstituencyPartyJob#parse calls 'node.xpath(MP_ID)' 2 times Locations: 0 1
  3. RefreshConstituencyPartyJob#parse calls 'node.xpath(MP_ID).text' 2 times Locations: 0 1
attrs[:name] = node.xpath(MP_NAME).text attrs[:party] = node.xpath(MP_PARTY).text } ] end end def faraday
  1. Identical code found in 2 nodes Locations: 0 1
  2. RefreshConstituencyPartyJob#faraday doesn't depend on instance state (maybe move it to another class?)
Faraday.new(HOST) do |f|
  1. RefreshConstituencyPartyJob#faraday has the variable name 'f'
f.response :follow_redirects f.response :raise_error f.adapter :net_http_persistent end end def fetch_members
  1. Similar code found in 2 nodes Locations: 0 1
faraday.get(ENDPOINT) do |request| request.options[:timeout] = TIMEOUT
  1. RefreshConstituencyPartyJob#fetch_members calls 'request.options' 2 times Locations: 0 1
  2. RefreshConstituencyPartyJob#fetch_members refers to 'request' more than self (maybe move it to another class?) Locations: 0 1
request.options[:open_timeout] = TIMEOUT
  1. RefreshConstituencyPartyJob#fetch_members calls 'request.options' 2 times Locations: 0 1
  2. RefreshConstituencyPartyJob#fetch_members refers to 'request' more than self (maybe move it to another class?) Locations: 0 1
end end end