forked from crouchygt/postcode_anywhere
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
96 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source "http://rubygems.org" | ||
|
||
gemspec |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module HatchPostcodeAnywhere | ||
|
||
autoload :PostcodeAnywhere, 'hatch/postcode_anywhere' | ||
autoload :Version, 'hatch/version' | ||
|
||
end | ||
|
||
require 'hatch/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require "httparty" | ||
require 'cgi' | ||
|
||
module Hatch | ||
class PostcodeAnywhere | ||
|
||
SERVICE_ADDRESS = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.00/xmle.ws" | ||
|
||
include HTTParty | ||
format :xml | ||
|
||
class << self | ||
|
||
attr_accessor :key#, :postcode, :number | ||
|
||
def license_key | ||
@key | ||
end | ||
|
||
|
||
end | ||
|
||
|
||
|
||
def self.find(query_options={}) | ||
PostcodeAnywhere.validate_key | ||
data = PostcodeAnywhere.lookup(postcode) | ||
data["Table"]["Row"] | ||
end | ||
|
||
def self.find_by_postcode(postcode) | ||
PostcodeAnywhere.validate_key | ||
data = PostcodeAnywhere.lookup(postcode) | ||
data["Table"]["Row"] | ||
end | ||
|
||
def self.find_by_postcode_and_number(postcode, number) | ||
PostcodeAnywhere.validate_key | ||
data = PostcodeAnywhere.lookup(postcode, number) | ||
data["Table"]["Row"] | ||
end | ||
|
||
def self.params(options={}) | ||
{ | ||
:Key => POSTCODE_ANYWHERE_KEY, | ||
:Postcode => options[:postcode], | ||
:Building => options[:number] | ||
} | ||
end | ||
|
||
def self.query_string(options={}) | ||
params(options).map{|k,v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v)}"}.join('&') | ||
end | ||
|
||
# appends query string to HOST_URL | ||
def to_s | ||
"#{HOST_URL}?#{query_string}" | ||
end | ||
|
||
|
||
def self.validate_key | ||
unless PostcodeAnywhere.key | ||
raise PostcodeAnywhereException, "Please provide a valid Postcode Anywhere License Key" | ||
end | ||
end | ||
|
||
def self.sanitised_postcode(postcode) | ||
postcode.gsub(/\s/, "") | ||
end | ||
|
||
def self.lookup(postcode, number='') | ||
param_string = query_string(:postcode => postcode, :number => number) | ||
PostcodeAnywhere.get "#{SERVICE_ADDRESS}?#{param_string}" | ||
end | ||
|
||
class PostcodeAnywhereException < StandardError;end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Hatch | ||
VERSION = "0.0.1" | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.