Module: Postcodes

Defined in:
lib/postcodes.rb,
lib/postcodes/util.rb,
lib/postcodes/errors.rb,
lib/postcodes/postcode.rb

Defined Under Namespace

Classes: AuthenticationError, InvalidInputError, LimitReachedError, Postcode, PostcodesError, RefererExcludedError, ResourceNotFoundError, TokenExhaustedError, Util

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/postcodes.rb', line 14

def api_key
  @api_key
end

.base_urlObject

Returns the value of attribute base_url.



14
15
16
# File 'lib/postcodes.rb', line 14

def base_url
  @base_url
end

.versionObject

Returns the value of attribute version.



14
15
16
# File 'lib/postcodes.rb', line 14

def version
  @version
end

Class Method Details

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/postcodes.rb', line 17

def self.request(method, path, params = {})
	unless @api_key
		raise Postcodes::AuthenticationError.new('No API Key provided. ' +
			'Set your API key using Postcodes.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(e)
	end
	parse response.body
end