Updated

app/models/constituency / api_query.rb

B
63 lines of codes
4 methods
12.1 complexity/method
4 churn
48.21 complexity
22 duplications
require 'nokogiri' class Constituency < ActiveRecord::Base class ApiQuery
  1. Constituency::ApiQuery has no descriptive comment
  2. Constituency::ApiQuery has 9 constants
CONSTITUENCIES = '//Constituencies/Constituency' CONSTITUENCY_ID = './Constituency_Id' CONSTITUENCY_NAME = './Name' CONSTITUENCY_CODE = './ONSCode' CURRENT_MP = './RepresentingMembers/RepresentingMember[1]' MP_ID = './Member_Id' MP_NAME = './Member' MP_START = './StartDate' MP_END = './EndDate' def fetch(postcode)
  1. Constituency::ApiQuery#fetch has approx 8 statements
response = client.call(postcode) if response.success? parse(response.body) else [] end rescue Faraday::ResourceNotFound, Faraday::ClientError => e
  1. Constituency::ApiQuery#fetch has the variable name 'e' Locations: 0 1
return [] rescue Faraday::Error => e
  1. Constituency::ApiQuery#fetch has the variable name 'e' Locations: 0 1
Appsignal.send_exception(e) return [] end def self.before_remove_const Thread.current[:__api_client__] = nil end private def client
  1. Constituency::ApiQuery#client doesn't depend on instance state (maybe move it to another class?)
Thread.current[:__api_client__] ||= ApiClient.new end def parse(body)
  1. Constituency::ApiQuery#parse has a flog score of 35
  2. Constituency::ApiQuery#parse has approx 10 statements
  3. Constituency::ApiQuery#parse doesn't depend on instance state (maybe move it to another class?)
xml = Nokogiri::XML(body) xml.xpath(CONSTITUENCIES).map do |node| {}.tap do |attrs| attrs[:name] = node.xpath(CONSTITUENCY_NAME).text attrs[:external_id] = node.xpath(CONSTITUENCY_ID).text attrs[:ons_code] = node.xpath(CONSTITUENCY_CODE).text if mp = node.at_xpath(CURRENT_MP) if mp.at_xpath(MP_END).text.present? attrs.merge!(mp_id: nil, mp_name: nil, mp_date: nil) else attrs[:mp_id] = mp.xpath(MP_ID).text
  1. Similar code found in 2 nodes Locations: 0 1
attrs[:mp_name] = mp.xpath(MP_NAME).text attrs[:mp_date] = mp.xpath(MP_START).text end end end end end end end