idealpostcodesrb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef5b1df933b79c7a2469d6158cb3e9d37648c1b5c685033a49fe3ce33d675642
4
+ data.tar.gz: 032db0a324989a3a179bf499328e069c3a592b7e0349888eb335b0373513f7d1
5
+ SHA512:
6
+ metadata.gz: 498af55a7b5ff6810d47a3899912639e687074ef2c9bb705352f73c98a7be441a978aa0186885884e7092148af8df58bc17c8c8ef0481f1930cbca0c747e41e0
7
+ data.tar.gz: 0f2031224b9d9fe33cc4935a05228e7925cf9049224375ece7fe596371a20727f4218766b38a29566a65c8dd972847fa1ab674506c92f429f912d332ae4d2d34
data/.env.example ADDED
@@ -0,0 +1 @@
1
+ API_KEY=
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in youtuberb.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "dotenv"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ idealpostcodesrb (0.1.0)
5
+ faraday (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ dotenv (2.7.6)
11
+ faraday (2.7.4)
12
+ faraday-net_http (>= 2.0, < 3.1)
13
+ ruby2_keywords (>= 0.0.4)
14
+ faraday-net_http (3.0.2)
15
+ rake (13.0.6)
16
+ ruby2_keywords (0.0.5)
17
+
18
+ PLATFORMS
19
+ x86_64-linux
20
+
21
+ DEPENDENCIES
22
+ dotenv
23
+ idealpostcodesrb!
24
+ rake (~> 13.0)
25
+
26
+ BUNDLED WITH
27
+ 2.3.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Dean Perry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Ideal Postcodes
2
+
3
+ **This library is a work in progress**
4
+
5
+ IdealPostcodes is a Ruby library for the Ideal-Postcodes.co.uk API.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'idealpostcodes'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Set your API Key
18
+
19
+ You can find your API Key from the [Ideal Postcodes account](https://account.ideal-postcodes.co.uk/tokens) page, then set it like so:
20
+
21
+ ```ruby
22
+ @client = IdealPostcodes::Client.new(api_key: "abc123")
23
+ ```
24
+
25
+ ### Address
26
+
27
+ ```ruby
28
+ # Find an address
29
+ # Docs: https://docs.ideal-postcodes.co.uk/api#tag/Address-Search/operation/AddressAutocomplete
30
+ @client.addresses.find query: "10 drowning street"
31
+ #=> #<IdealPostcodes::Collection...
32
+
33
+ # Lookup address for a postcode
34
+ @client.addresses.lookup postcode: "sw1a 0aa"
35
+ #=> #<IdealPostcodes::Collection...
36
+
37
+ # Resolve an address
38
+ # Kind should be GBR or USA
39
+ # Address would be the ID of the Address in Ideal Postcodes
40
+ @client.addresses.resolve kind: "gbr", address: "paf_22690298"
41
+
42
+ # Retrieves an address as identified by its Unique Delivery Point Reference Number (UDPRN).
43
+ @client.addresses.udprn query: "abc123"
44
+
45
+ # Retrieves an address as identified by its UMPRN (Multiple Residence Unique ID.
46
+ @client.addresses.umprn query: "abc123"
47
+ ```
48
+
49
+ ### Places
50
+
51
+ ```ruby
52
+ # Query places across countries
53
+ # Docs: https://docs.ideal-postcodes.co.uk/api#tag/Place-Search/operation/FindPlace
54
+ @client.places.find query: "London"
55
+ #=> #<IdealPostcodes::Collection
56
+
57
+ # Resolve a Place
58
+ @client.places.resolve place: "geonames_2643743"
59
+ #=> #<IdealPostcodes::Place id="geonames_2643743"...
60
+ ```
61
+
62
+ ### Email Validation
63
+
64
+ Queries and validates a given email address.
65
+
66
+ ```ruby
67
+ @client.emails.validate email: "test-email@example.com"
68
+ #=> #<IdealPostcodes::Email result="deliverable"...
69
+ ```
70
+
71
+ ### Phone Validation
72
+
73
+ Queries and validates a given phone number. Number should include the country code.
74
+
75
+ ```ruby
76
+ @client.phones.validate number: "447123123123"
77
+ #=> #<IdealPostcodes::Phone valid=true...
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/idealpostcodes.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ideal_postcodes"
6
+
7
+ # Load environment variables from .env file
8
+ require 'dotenv/load'
9
+
10
+ # You can add fixtures and/or initialization code here to make experimenting
11
+ # with your gem easier. You can also use a different console, if you like.
12
+
13
+ # (If you use this, don't forget to add pry to your Gemfile!)
14
+ # require "pry"
15
+ # Pry.start
16
+
17
+ @client = IdealPostcodes::Client.new(api_key: ENV["API_KEY"])
18
+
19
+ require "irb"
20
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ideal_postcodes/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "idealpostcodesrb"
7
+ spec.version = IdealPostcodes::VERSION
8
+ spec.authors = ["Dean Perry"]
9
+ spec.email = ["dean@deanpcmad.com"]
10
+
11
+ spec.summary = "A Ruby library for the Ideal Postcodes API"
12
+ spec.homepage = "https://deanpcmad.com"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/deanpcmad/idealpostcodesrb"
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "faraday", "~> 2.0"
32
+ end
@@ -0,0 +1,46 @@
1
+ module IdealPostcodes
2
+ class Client
3
+ attr_reader :api_key, :adapter
4
+
5
+ def initialize(api_key:, adapter: Faraday.default_adapter, stubs: nil)
6
+ @api_key = api_key
7
+ @adapter = adapter
8
+
9
+ # Test stubs for requests
10
+ @stubs = stubs
11
+ end
12
+
13
+ def addresses
14
+ AddressesResource.new(self)
15
+ end
16
+
17
+ def places
18
+ PlacesResource.new(self)
19
+ end
20
+
21
+ def emails
22
+ EmailsResource.new(self)
23
+ end
24
+
25
+ def phones
26
+ PhonesResource.new(self)
27
+ end
28
+
29
+ def connection
30
+ url = "https://api.ideal-postcodes.co.uk/v1"
31
+
32
+ @connection ||= Faraday.new(url) do |conn|
33
+ conn.headers = {
34
+ "User-Agent" => "idealpostcodes/v#{VERSION} (github.com/deanpcmad/idealpostcodes)",
35
+ "Authorization" => "api_key=\"#{api_key}\""
36
+ }
37
+
38
+ conn.request :json
39
+ conn.response :json
40
+
41
+ conn.adapter adapter, @stubs
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ module IdealPostcodes
2
+ class Collection
3
+ attr_reader :data, :total
4
+
5
+ def self.from_response(response, type:)
6
+ body = response.body
7
+
8
+ if body["result"] and body["result"].include? "hits"
9
+ data = body["result"]["hits"]
10
+ else
11
+ data = body["result"]
12
+ end
13
+
14
+ new(
15
+ data: data.map { |attrs| type.new(attrs) },
16
+ total: data.count
17
+ )
18
+ end
19
+
20
+ def initialize(data:, total:)
21
+ @data = data
22
+ @total = total
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module IdealPostcodes
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module IdealPostcodes
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map { |o| to_ostruct(o) }
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module IdealPostcodes
2
+ class Address < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module IdealPostcodes
2
+ class Email < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module IdealPostcodes
2
+ class Phone < Object
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module IdealPostcodes
2
+ class Place < Object
3
+ end
4
+ end
@@ -0,0 +1,56 @@
1
+ module IdealPostcodes
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(url, params: {}, headers: {})
12
+ handle_response client.connection.get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body:, headers: {})
16
+ handle_response client.connection.post(url, body, headers)
17
+ end
18
+
19
+ def patch_request(url, body:, headers: {})
20
+ handle_response client.connection.patch(url, body, headers)
21
+ end
22
+
23
+ def put_request(url, body:, headers: {})
24
+ handle_response client.connection.put(url, body, headers)
25
+ end
26
+
27
+ def delete_request(url, params: {}, headers: {})
28
+ handle_response client.connection.delete(url, params, headers)
29
+ end
30
+
31
+ def handle_response(response)
32
+ case response.status
33
+ when 400
34
+ raise Error, "Error 400: Your request was malformed. '#{response.body["message"]}'"
35
+ when 401
36
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["message"]}'"
37
+ when 403
38
+ raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["message"]}'"
39
+ when 404
40
+ raise Error, "Error 404: No results were found for your request. '#{response.body["message"]}'"
41
+ when 409
42
+ raise Error, "Error 409: Your request was a conflict. '#{response.body["message"]}'"
43
+ when 422
44
+ raise Error, "Error 422: Unprocessable Entity. '#{response.body["message"]}'"
45
+ when 429
46
+ raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["message"]}'"
47
+ when 500
48
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["message"]}'"
49
+ when 503
50
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["message"]}'"
51
+ end
52
+
53
+ response
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,31 @@
1
+ module IdealPostcodes
2
+ class AddressesResource < Resource
3
+
4
+ def find(**params)
5
+ response = get_request("autocomplete/addresses", params: params)
6
+ Collection.from_response(response, type: Address)
7
+ end
8
+
9
+ def resolve(kind:, address:)
10
+ response = get_request("autocomplete/addresses/#{address}/#{kind}")
11
+ Address.new(response.body["result"])
12
+ end
13
+
14
+ def lookup(postcode:)
15
+ query = postcode.gsub(" ", "")
16
+ response = get_request("postcodes/#{query}")
17
+ Collection.from_response(response, type: Address)
18
+ end
19
+
20
+ def udprn(query:)
21
+ response = get_request("udprn/#{query}")
22
+ Address.new(response.body["result"])
23
+ end
24
+
25
+ def umprn(query:)
26
+ response = get_request("umprn/#{query}")
27
+ Address.new(response.body["result"])
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,10 @@
1
+ module IdealPostcodes
2
+ class EmailsResource < Resource
3
+
4
+ def validate(email:)
5
+ response = get_request("emails", params: {query: email})
6
+ Email.new(response.body["result"])
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module IdealPostcodes
2
+ class PhonesResource < Resource
3
+
4
+ def validate(number:)
5
+ response = get_request("phone_numbers", params: {query: number})
6
+ Phone.new(response.body["result"])
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module IdealPostcodes
2
+ class PlacesResource < Resource
3
+
4
+ def find(**params)
5
+ response = get_request("places", params: params)
6
+ Collection.from_response(response, type: Place)
7
+ end
8
+
9
+ def resolve(place:)
10
+ response = get_request("places/#{place}")
11
+ Place.new(response.body["result"])
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module IdealPostcodes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require_relative "ideal_postcodes/version"
5
+
6
+ module IdealPostcodes
7
+
8
+ autoload :Client, "ideal_postcodes/client"
9
+ autoload :Collection, "ideal_postcodes/collection"
10
+ autoload :Error, "ideal_postcodes/error"
11
+ autoload :Resource, "ideal_postcodes/resource"
12
+ autoload :Object, "ideal_postcodes/object"
13
+
14
+ autoload :AddressesResource, "ideal_postcodes/resources/addresses"
15
+ autoload :EmailsResource, "ideal_postcodes/resources/emails"
16
+ autoload :PhonesResource, "ideal_postcodes/resources/phones"
17
+ autoload :PlacesResource, "ideal_postcodes/resources/places"
18
+
19
+ autoload :Address, "ideal_postcodes/objects/address"
20
+ autoload :Email, "ideal_postcodes/objects/email"
21
+ autoload :Phone, "ideal_postcodes/objects/phone"
22
+ autoload :Place, "ideal_postcodes/objects/place"
23
+
24
+ end
@@ -0,0 +1 @@
1
+ require "ideal_postcodes"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idealpostcodesrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description:
28
+ email:
29
+ - dean@deanpcmad.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.example"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/console
41
+ - bin/setup
42
+ - idealpostcodesrb.gemspec
43
+ - lib/ideal_postcodes.rb
44
+ - lib/ideal_postcodes/client.rb
45
+ - lib/ideal_postcodes/collection.rb
46
+ - lib/ideal_postcodes/error.rb
47
+ - lib/ideal_postcodes/object.rb
48
+ - lib/ideal_postcodes/objects/address.rb
49
+ - lib/ideal_postcodes/objects/email.rb
50
+ - lib/ideal_postcodes/objects/phone.rb
51
+ - lib/ideal_postcodes/objects/place.rb
52
+ - lib/ideal_postcodes/resource.rb
53
+ - lib/ideal_postcodes/resources/addresses.rb
54
+ - lib/ideal_postcodes/resources/emails.rb
55
+ - lib/ideal_postcodes/resources/phones.rb
56
+ - lib/ideal_postcodes/resources/places.rb
57
+ - lib/ideal_postcodes/version.rb
58
+ - lib/idealpostcodesrb.rb
59
+ homepage: https://deanpcmad.com
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://deanpcmad.com
64
+ source_code_uri: https://github.com/deanpcmad/idealpostcodesrb
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.6.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.3.7
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A Ruby library for the Ideal Postcodes API
84
+ test_files: []