class PostcodeInfo::Client
Attributes
api_uri[RW]
auth_token[RW]
Public Class Methods
new(opts={})
click to toggle source
params:
auth_token: 'your auth token' (optional) api_url: 'url of a postcodeinfo server' (optional) If this is not supplied, it will be inferred from the params below If no value can be inferred, it will default to http://localhost:8000/ env: (development|test|staging|production) (optional - default is development) If provided, the API url will be looked up in .api_urls by this value. If not provided, it will look for a RAILS_ENV environment variable and use that. If it still can't work out the environment, it will default to development
# File lib/postcodeinfo/client.rb, line 21 def initialize(opts={}) @auth_token = opts[:auth_token] @api_uri = URI.parse(infer_api_url(opts)) end
Protected Class Methods
api_urls()
click to toggle source
# File lib/postcodeinfo/client.rb, line 117 def self.api_urls { development: 'http://localhost:8000/', test: 'http://localhost:8000/', staging: 'https://postcodeinfo-staging.dsd.io/', production: 'https://postcodeinfo.service.justice.gov.uk/' } end
Public Instance Methods
addresses(postcode)
click to toggle source
# File lib/postcodeinfo/client.rb, line 59 def addresses(postcode) response = make_request('/addresses/?postcode=' + postcode) handle_response(response) end
api_url()
click to toggle source
# File lib/postcodeinfo/client.rb, line 26 def api_url @api_uri.to_s end
api_url=(url)
click to toggle source
# File lib/postcodeinfo/client.rb, line 30 def api_url=(url) @api_uri = URI.parse(url) end
info(postcode)
click to toggle source
# File lib/postcodeinfo/client.rb, line 64 def info(postcode) response = make_request('/postcodes/' + postcode) handle_response(response) end
lookup_postcode(postcode)
click to toggle source
# File lib/postcodeinfo/client.rb, line 34 def lookup_postcode(postcode) begin pc = Postcode.new(postcode, self) pc.lookup_info! pc.lookup_addresses! pc rescue RestClient::ResourceNotFound => e raise PostcodeInfo::UnrecognisedPostcode.new(e, nil) end end
make_request(endpoint)
click to toggle source
# File lib/postcodeinfo/client.rb, line 69 def make_request(endpoint) headers = { 'Authorization' => 'Token ' + @auth_token } begin response = RestClient::Request.execute( method: :get, url: URI.join(@api_uri, endpoint).to_s, headers: headers, timeout: 5 ) rescue Exception => ex raise wrapped_exception(ex, response) end end
valid?(postcode)
click to toggle source
# File lib/postcodeinfo/client.rb, line 45 def valid?(postcode) begin response = make_request('/postcodes/' + postcode) case response.code when 200 then true when /^5[0-9]{2}/ then nil when 404 then false end rescue RestClient::ResourceNotFound => e false end end
Protected Instance Methods
effective_env(opts)
click to toggle source
# File lib/postcodeinfo/client.rb, line 109 def effective_env(opts) opts[:env] || ENV['RAILS_ENV'] || :development end
from_env(env)
click to toggle source
# File lib/postcodeinfo/client.rb, line 113 def from_env(env) self.class.api_urls[env.to_sym] || self.class.api_urls[:development] end
handle_response(response)
click to toggle source
# File lib/postcodeinfo/client.rb, line 100 def handle_response(response) JSON.parse(response.body, symbolize_names: true) end
infer_api_url(opts={})
click to toggle source
# File lib/postcodeinfo/client.rb, line 104 def infer_api_url(opts={}) opts[:api_url] || from_env( effective_env(opts) ) end
wrapped_exception(exception, response)
click to toggle source
# File lib/postcodeinfo/client.rb, line 87 def wrapped_exception(exception, response) case exception when RestClient::Unauthorized PostcodeInfo::InvalidAuthToken.new(exception, response) when RestClient::InternalServerError PostcodeInfo::ServerError.new(exception, response) when SocketError PostcodeInfo::ServiceUnavailable.new(exception, response) else exception end end