Module: IdealPostcodes

Defined in:
lib/idealpostcodes.rb,
lib/idealpostcodes/key.rb,
lib/idealpostcodes/util.rb,
lib/idealpostcodes/errors.rb,
lib/idealpostcodes/address.rb,
lib/idealpostcodes/version.rb,
lib/idealpostcodes/postcode.rb

Defined Under Namespace

Modules: Address, Key, Postcode Classes: AuthenticationError, IdealPostcodesError, LimitReachedError, ResourceNotFoundError, TokenExhaustedError, Util

Constant Summary collapse

VERSION =
'2.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



21
22
23
# File 'lib/idealpostcodes.rb', line 21

def api_key
  @api_key
end

.base_urlObject

Returns the value of attribute base_url.



21
22
23
# File 'lib/idealpostcodes.rb', line 21

def base_url
  @base_url
end

.secretObject

Returns the value of attribute secret.



21
22
23
# File 'lib/idealpostcodes.rb', line 21

def secret
  @secret
end

.versionObject

Returns the value of attribute version.



21
22
23
# File 'lib/idealpostcodes.rb', line 21

def version
  @version
end

Class Method Details

.apply_secret(secret) ⇒ Object



52
53
54
# File 'lib/idealpostcodes.rb', line 52

def self.apply_secret(secret)
  @secret = secret
end

.key_availableObject



56
57
58
59
# File 'lib/idealpostcodes.rb', line 56

def self.key_available
  response = Key.lookup @api_key
  response[:available]
end

.key_detailsObject



61
62
63
64
65
# File 'lib/idealpostcodes.rb', line 61

def self.key_details
  raise IdealPostcodes::AuthenticationError.new('No Secret Key provided. ' +
      'Set your secret key with IdealPostcodes.apply_secret #your_key') if @secret.nil?
  response = Key.lookup_details @api_key, @secret
end

.request(method, path, params = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/idealpostcodes.rb', line 24

def self.request(method, path, params = {})
  unless @api_key
    raise IdealPostcodes::AuthenticationError.new('No API Key provided. ' +
      'Set your key with IdealPostcodes.api_key = #your_key')
  end

  url = URI.parse(resource_url(path))
  params.merge! api_key: @api_key
  url.query = Util.merge_params(params)
  request_options = {
    method: method.downcase.to_sym,
    url: url.to_s
  }

  begin
    response = generate_request(request_options)
  rescue RestClient::ExceptionWithResponse => error
    if rcode = error.http_code && rbody = error.http_body
      handle_error(rcode, rbody)
    else
      handle_client_error(error)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => error
    handle_client_error(error)
  end
  parse response.body
end