1require 'faraday'
 
2
 
3class FetchCountryRegisterJob < ApplicationJob
 
4  HOST = 'https://country.register.gov.uk'
 
5  ENDPOINT = '/records.json?page-size=500'
 
6  TIMEOUT = 5
 
7
 
8  rescue_from StandardError do |exception|
 
 9    Appsignal.send_exception exception
 
10  end
 
  • FeatureEnvy - refers to 'location' more than self (maybe move it to another class?) » reek
  • TooManyStatements - has approx 6 statements » reek
  • Complexity 3 » saikuro
12  def perform
 
13    countries.each do |country|
 
14      location = Location.find_or_initialize_by(code: country['country'])
 
 
16      location.name = country['name']
 
17      location.start_date = country['start-date']
 
18      location.end_date = country['end-date']
 
 
20      if location.changed? || location.new_record?
 
21        location.save!
 
22      end
 
23    end
 
24  end
 
 
26  private
 
  • UncommunicativeVariableName - has the variable name 'x' » reek
  • Complexity 2 » saikuro
28  def countries
 
29    fetch_register.body.values.map { |x| x['item'].first }
 
30  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
32  def faraday
 
33    Faraday.new(HOST) do |f|
 
34      f.response :follow_redirects
 
35      f.response :json
 
36      f.response :raise_error
 
37      f.adapter :net_http_persistent
 
38    end
 
39  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
41  def fetch_register
 
42    faraday.get(ENDPOINT) do |request|
 
43      request.options[:timeout] = TIMEOUT
 
44      request.options[:open_timeout] = TIMEOUT
 
45    end
 
46  end
 
47end