class Courtfinder::Client::HousingPossession

Constants

PATH

Public Instance Methods

empty?() click to toggle source
# File lib/courtfinder/client.rb, line 24
def empty?
  @json.empty?
end
get(postcode) click to toggle source
# File lib/courtfinder/client.rb, line 11
def get postcode
  conn = nil
  begin
    endpoint = "#{Courtfinder::SERVER}#{PATH}#{URI.escape(postcode)}"
    conn = Excon.get(endpoint, :read_timeout => 90)
    check_for_error_code conn
  rescue Excon::Errors::RequestTimeout
    @json = { error: 'timeout' }
  end

  @json
end

Private Instance Methods

check_for_error_code(response) click to toggle source
# File lib/courtfinder/client.rb, line 30
def check_for_error_code response
  process response.body if response.status == 200
  @json = { error: 'timeout' } if response.status == 400
  @json = { error: "postcode couldn't be found" } if response.status == 500
end
cleanup_court_data() click to toggle source
# File lib/courtfinder/client.rb, line 46
def cleanup_court_data
  unwanted_attributes = ['areas_of_law', 'slug', 'dx_number',
                         'lon', 'lat', 'types', 'number', 'distance']
  @json.each do |court|
    unwanted_attributes.each {|attr| court.delete attr }
  end
end
process(data) click to toggle source
# File lib/courtfinder/client.rb, line 36
def process data
  begin
    parsed = JSON.parse data
    @json = (parsed.size > 1) ? [parsed[0]] : parsed
    cleanup_court_data
  rescue JSON::ParserError
    @json = { error: 'invalid JSON returned' }
  end
end