Class: PostcodeInfo::Client
- Inherits:
-
Object
- Object
- PostcodeInfo::Client
- Defined in:
- lib/postcodeinfo/client.rb
Instance Attribute Summary collapse
-
#api_uri ⇒ Object
Returns the value of attribute api_uri.
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
Instance Method Summary collapse
- #addresses(postcode) ⇒ Object
- #api_url ⇒ Object
- #api_url=(url) ⇒ Object
- #info(postcode) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
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 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.
- #lookup_postcode(postcode) ⇒ Object
- #make_request(endpoint) ⇒ Object
- #valid?(postcode) ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ Client
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
21 22 23 24 |
# File 'lib/postcodeinfo/client.rb', line 21 def initialize(opts={}) @auth_token = opts[:auth_token] @api_uri = URI.parse(infer_api_url(opts)) end |
Instance Attribute Details
#api_uri ⇒ Object
Returns the value of attribute api_uri.
8 9 10 |
# File 'lib/postcodeinfo/client.rb', line 8 def api_uri @api_uri end |
#auth_token ⇒ Object
Returns the value of attribute auth_token.
8 9 10 |
# File 'lib/postcodeinfo/client.rb', line 8 def auth_token @auth_token end |
Instance Method Details
#addresses(postcode) ⇒ Object
59 60 61 62 |
# File 'lib/postcodeinfo/client.rb', line 59 def addresses(postcode) response = make_request('/addresses/?postcode=' + postcode) handle_response(response) end |
#api_url ⇒ Object
26 27 28 |
# File 'lib/postcodeinfo/client.rb', line 26 def api_url @api_uri.to_s end |
#api_url=(url) ⇒ Object
30 31 32 |
# File 'lib/postcodeinfo/client.rb', line 30 def api_url=(url) @api_uri = URI.parse(url) end |
#info(postcode) ⇒ Object
64 65 66 67 |
# File 'lib/postcodeinfo/client.rb', line 64 def info(postcode) response = make_request('/postcodes/' + postcode) handle_response(response) end |
#lookup_postcode(postcode) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# 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) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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) ⇒ Boolean
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 |