require 'faraday'
class RefreshConstituencyPartyJob < ApplicationJob - RefreshConstituencyPartyJob has no descriptive comment
- 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] -
constituency.update!(party: member[:party]) -
else
constituency.update!(mp_id: nil, mp_name: nil, mp_date: nil, party: nil) -
end
end
end
private
def members
@members ||= load_members
end
def load_members - RefreshConstituencyPartyJob#load_members has approx 6 statements
response = fetch_members
if response.success?
Hash[parse(response.body)]
else
{}
end
rescue Faraday::Error => e - RefreshConstituencyPartyJob#load_members has the variable name 'e'
Appsignal.send_exception(e)
return {}
end
def parse(body) - RefreshConstituencyPartyJob#parse has approx 6 statements
- 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, -
-
{}.tap { |attrs|
attrs[:id] = node.xpath(MP_ID).text -
-
-
attrs[:name] = node.xpath(MP_NAME).text
attrs[:party] = node.xpath(MP_PARTY).text
}
]
end
end
def faraday -
- RefreshConstituencyPartyJob#faraday doesn't depend on instance state (maybe move it to another class?)
Faraday.new(HOST) do |f| - 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 -
faraday.get(ENDPOINT) do |request|
request.options[:timeout] = TIMEOUT -
-
request.options[:open_timeout] = TIMEOUT
end
end
end