1require 'faraday'
 
2
 
3class RefreshConstituencyPartyJob < ApplicationJob
 
4  HOST = "http://data.parliament.uk"
 
5  ENDPOINT = "/membersdataplatform/services/mnis/members/query/House=Commons%7CIsEligible=true/"
 
6  TIMEOUT = 5
 
7
 
8  MEMBERS    = "//Members/Member"
 
 9  MP_ID      = "./@Member_Id"
 
10  MP_NAME    = "./FullTitle"
 
11  MP_PARTY   = "./Party"
 
 
13  rescue_from StandardError do |exception|
 
14    Appsignal.send_exception exception
 
15  end
 
  • FeatureEnvy - refers to 'constituency' more than self (maybe move it to another class?) » reek
  • Complexity 3 » saikuro
17  def perform
 
18    Constituency.find_each do |constituency|
  • Found = in conditional. It should probably be an == » roodi
19      if member = members[constituency.mp_id]
 
20        constituency.update!(party: member[:party])
 
21      else
 
22        constituency.update!(mp_id: nil, mp_name: nil, mp_date: nil, party: nil)
 
23      end
 
24    end
 
25  end
 
 
27  private
 
  • Complexity 1 » saikuro
29  def members
 
30    @members ||= load_members
 
31  end
 
  • TooManyStatements - has approx 6 statements » reek
  • UncommunicativeVariableName - has the variable name 'e' » reek
  • Complexity 3 » saikuro
33  def load_members
 
34    response = fetch_members
 
 
36    if response.success?
 
37      Hash[parse(response.body)]
 
38    else
 
39      {}
 
40    end
 
41  rescue Faraday::Error => e
 
42    Appsignal.send_exception(e)
 
43    return {}
 
44  end
 
  • DuplicateMethodCall - calls 'node.xpath(MP_ID)' 2 times » reek
  • DuplicateMethodCall - calls 'node.xpath(MP_ID).text' 2 times » reek
  • TooManyStatements - has approx 6 statements » reek
  • UtilityFunction - doesn't depend on instance state (maybe move it to another class?) » reek
  • Complexity 3 » saikuro
46  def parse(body)
 
47    xml = Nokogiri::XML(body)
 
 
49    xml.xpath(MEMBERS).map do |node|
 
50      [
 
51        node.xpath(MP_ID).text,
 
52        {}.tap { |attrs|
 
53          attrs[:id]    = node.xpath(MP_ID).text
 
54          attrs[:name]  = node.xpath(MP_NAME).text
 
55          attrs[:party] = node.xpath(MP_PARTY).text
 
56        }
 
57      ]
 
58    end
 
59  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
61  def faraday
 
62    Faraday.new(HOST) do |f|
 
63      f.response :follow_redirects
 
64      f.response :raise_error
 
65      f.adapter :net_http_persistent
 
66    end
 
67  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
69  def fetch_members
 
70    faraday.get(ENDPOINT) do |request|
 
71      request.options[:timeout] = TIMEOUT
 
72      request.options[:open_timeout] = TIMEOUT
 
73    end
 
74  end
 
75end