Class: PostcodeInfo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/postcodeinfo/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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


24
25
26
27
# File 'lib/postcodeinfo/client.rb', line 24

def initialize(opts={})
  @auth_token = opts[:auth_token]
  @api_uri = URI.parse(infer_api_url(opts))
end

Instance Attribute Details

#api_uriObject

Returns the value of attribute api_uri.



11
12
13
# File 'lib/postcodeinfo/client.rb', line 11

def api_uri
  @api_uri
end

#auth_tokenObject

Returns the value of attribute auth_token.



11
12
13
# File 'lib/postcodeinfo/client.rb', line 11

def auth_token
  @auth_token
end

Instance Method Details

#addresses(postcode) ⇒ Object



62
63
64
65
# File 'lib/postcodeinfo/client.rb', line 62

def addresses(postcode)
  response = make_request('/addresses/?postcode=' + postcode)
  handle_response(response)
end

#api_urlObject



29
30
31
# File 'lib/postcodeinfo/client.rb', line 29

def api_url
  @api_uri.to_s
end

#api_url=(url) ⇒ Object



33
34
35
# File 'lib/postcodeinfo/client.rb', line 33

def api_url=(url)
  @api_uri = URI.parse(url)
end

#info(postcode) ⇒ Object



67
68
69
70
# File 'lib/postcodeinfo/client.rb', line 67

def info(postcode)
  response = make_request('/postcodes/' + postcode)
  handle_response(response)
end

#lookup_postcode(postcode) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/postcodeinfo/client.rb', line 37

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/postcodeinfo/client.rb', line 72

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

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/postcodeinfo/client.rb', line 48

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