Module: Postcodeapi
- Defined in:
- lib/postcodeapi.rb,
lib/postcodeapi/version.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Class Attribute Summary collapse
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.api_url ⇒ Object
Returns the value of attribute api_url.
Class Method Summary collapse
- .configure(&blk) ⇒ Object
- .format_zipcode(zipcode = nil) ⇒ Object
- .get_address(zipcode, number) ⇒ Object
- .get_street(zipcode) ⇒ Object
- .stringify(obj) ⇒ Object
- .version_string ⇒ Object
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/postcodeapi.rb', line 9 def api_key @api_key end |
.api_url ⇒ Object
Returns the value of attribute api_url.
9 10 11 |
# File 'lib/postcodeapi.rb', line 9 def api_url @api_url end |
Class Method Details
.configure(&blk) ⇒ Object
10 |
# File 'lib/postcodeapi.rb', line 10 def configure(&blk); class_eval(&blk); end |
.format_zipcode(zipcode = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/postcodeapi.rb', line 33 def self.format_zipcode(zipcode = nil) if zipcode.nil? || zipcode == "" raise Exception.new("No zipcode provided") elsif (zipcode =~ /^[0-9]{4}$|[0-9]{4}[\ ]{1}[a-zA-Z]{2}$|[0-9]{4}[a-zA-Z]{2}$/).nil? raise Exception.new("Incorrect zipcode format (1234AB format expected)") else zipcode.gsub(" ", "").upcase end end |
.get_address(zipcode, number) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/postcodeapi.rb', line 23 def self.get_address(zipcode,number) # Format the zipcode zipcode = format_zipcode(zipcode) # Perform request response = RestClient.get "#{@api_url}/#{zipcode}/#{number}", {:content_type => :json, :accept => :json, :'Api-Key' => @api_key} # Return openstruct output res = JSON.parse(response.to_str,{symbolize_names: true}) OpenStruct.new res[:resource] end |
.get_street(zipcode) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/postcodeapi.rb', line 13 def self.get_street(zipcode) # Format the zipcode zipcode = format_zipcode(zipcode) # Perform request response = RestClient.get "#{@api_url}/#{zipcode}", {:content_type => :json, :accept => :json, :'Api-Key' => @api_key} # Return openstruct output res = JSON.parse(response.to_str,{symbolize_names: true}) OpenStruct.new res[:resource] end |
.stringify(obj) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/postcodeapi.rb', line 43 def self.stringify(obj) if obj.is_a? Array obj.join elsif obj.is_a? Hash obj.merge( obj ) {|k, val| stringify val } end end |
.version_string ⇒ Object
51 52 53 |
# File 'lib/postcodeapi.rb', line 51 def self.version_string "Postcodeapi version #{Postcodeapi::VERSION}" end |