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
- DEFAULT_PARSER =
URI::Parser.new
- VERSION =
'2.0.1'
Class Attribute Summary collapse
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.base_url ⇒ Object
Returns the value of attribute base_url.
-
.secret ⇒ Object
Returns the value of attribute secret.
-
.version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
- .apply_secret(secret) ⇒ Object
- .general_error(response_code, response_body) ⇒ Object
- .generate_request(options) ⇒ Object
- .handle_client_error(error) ⇒ Object
- .handle_error(http_code, http_body) ⇒ Object
- .key_available ⇒ Object
- .key_details ⇒ Object
- .parse(response) ⇒ Object
- .request(method, path, params = {}) ⇒ Object
- .resource_url(path = '') ⇒ Object
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
21 22 23 |
# File 'lib/idealpostcodes.rb', line 21 def api_key @api_key end |
.base_url ⇒ Object
Returns the value of attribute base_url.
21 22 23 |
# File 'lib/idealpostcodes.rb', line 21 def base_url @base_url end |
.secret ⇒ Object
Returns the value of attribute secret.
21 22 23 |
# File 'lib/idealpostcodes.rb', line 21 def secret @secret end |
.version ⇒ Object
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
49 50 51 |
# File 'lib/idealpostcodes.rb', line 49 def self.apply_secret(secret) @secret = secret end |
.general_error(response_code, response_body) ⇒ Object
119 120 121 122 123 |
# File 'lib/idealpostcodes.rb', line 119 def self.general_error(response_code, response_body) IdealPostcodesError.new 'Invalid response object', response_code, response_body end |
.generate_request(options) ⇒ Object
70 71 72 |
# File 'lib/idealpostcodes.rb', line 70 def self.generate_request() RestClient::Request.execute() end |
.handle_client_error(error) ⇒ Object
115 116 117 |
# File 'lib/idealpostcodes.rb', line 115 def self.handle_client_error(error) raise IdealPostcodesError, "An unexpected error occured: #{error.message})" end |
.handle_error(http_code, http_body) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/idealpostcodes.rb', line 80 def self.handle_error(http_code, http_body) error = parse http_body ideal_code = error[:code] = error[:message] case ideal_code when 4010 raise AuthenticationError.new , http_code, http_body, ideal_code when 4020 raise TokenExhaustedError.new , http_code, http_body, ideal_code when 4021 raise LimitReachedError.new , http_code, http_body, ideal_code when 4040 raise ResourceNotFoundError.new , http_code, http_body, ideal_code else raise IdealPostcodesError.new , http_code, http_body, ideal_code end end |
.key_available ⇒ Object
53 54 55 56 |
# File 'lib/idealpostcodes.rb', line 53 def self.key_available response = Key.lookup @api_key response[:available] end |
.key_details ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/idealpostcodes.rb', line 58 def self.key_details if @secret.nil? raise IdealPostcodes::AuthenticationError, 'No Secret Key provided. ' + 'Set your secret key with IdealPostcodes.apply_secret #your_key' end response = Key.lookup_details @api_key, @secret end |
.parse(response) ⇒ Object
74 75 76 77 78 |
# File 'lib/idealpostcodes.rb', line 74 def self.parse(response) Util.keys_to_sym JSON.parse(response) rescue JSON::ParserError => e raise handle_client_error(e) 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 |
# File 'lib/idealpostcodes.rb', line 24 def self.request(method, path, params = {}) unless @api_key raise IdealPostcodes::AuthenticationError, '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) = { method: method.downcase.to_sym, url: url.to_s } begin response = generate_request() rescue RestClient::ExceptionWithResponse => e if rcode = e.http_code && rbody = e.http_body handle_error(rcode, rbody) else handle_client_error(e) end rescue RestClient::Exception, Errno::ECONNREFUSED => e handle_client_error(e) end parse response.body end |
.resource_url(path = '') ⇒ Object
66 67 68 |
# File 'lib/idealpostcodes.rb', line 66 def self.resource_url(path = '') IdealPostcodes::Util.escape "#{@base_url}/v#{@version}/#{path}" end |