international_postcode_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ebc490944a9cf04410b96b757011f1a846967225fb62b25a04b45eed04bc83e2
4
+ data.tar.gz: a0e33a8c2be7a8710b3fb5f151081567802f2cc748f51a153c6e96019a124b34
5
+ SHA512:
6
+ metadata.gz: 3f1a8e95a734875d01d86408ec103ff3757fe9c74d400fabebe2eac0da7fce186ad5aa79fcc156beb40d7bc7c0feaf0bdc5ac292273ed5647e6f11ef08f89941
7
+ data.tar.gz: f972286ea123866175b197e399495d0dc24d74994d4e1ed04e1a52ff78c61bf7a657b9d5968868031db45b80b4e9ab8089df8bedf3803f6e3c9ae765969e0318
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .DS_Store
13
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.2
6
+ before_install: gem install bundler -v 2.1.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ # Added
2
+ - initial release 0.1.0
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ gem 'rake', '~> 12.0'
5
+ gem 'rspec', '~> 3.0'
6
+
7
+ group :test do
8
+ gem 'byebug'
9
+ gem 'webmock'
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ international_postcode_api (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
11
+ byebug (11.1.3)
12
+ crack (0.4.5)
13
+ rexml
14
+ diff-lcs (1.4.4)
15
+ hashdiff (1.0.1)
16
+ public_suffix (4.0.6)
17
+ rake (12.3.3)
18
+ rexml (3.2.4)
19
+ rspec (3.10.0)
20
+ rspec-core (~> 3.10.0)
21
+ rspec-expectations (~> 3.10.0)
22
+ rspec-mocks (~> 3.10.0)
23
+ rspec-core (3.10.1)
24
+ rspec-support (~> 3.10.0)
25
+ rspec-expectations (3.10.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.10.0)
28
+ rspec-mocks (3.10.2)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-support (3.10.2)
32
+ webmock (3.11.2)
33
+ addressable (>= 2.3.6)
34
+ crack (>= 0.3.2)
35
+ hashdiff (>= 0.4.0, < 2.0.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ byebug
42
+ international_postcode_api!
43
+ rake (~> 12.0)
44
+ rspec (~> 3.0)
45
+ webmock
46
+
47
+ BUNDLED WITH
48
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Ruby wrapper for the postcode.eu endpoints
2
+ https://www.postcode.nl/en/services/adresdata/international
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'international_postcode_api'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install international_postcode_api
19
+
20
+ ## Usage
21
+
22
+ Create an initializer (config/intializers/international_postcode_api.rb)
23
+
24
+ ```ruby
25
+ InternationalPostcodeApi.configure do |config|
26
+ config.api_key = 'your-API-key'
27
+ config.secret_key = 'your-secret-key'
28
+
29
+ # set new base_uri if new versions are released
30
+ # config.base_uri = 'https://api.postcode.eu/international/v1'
31
+
32
+ # Do not switch to Dutch endpoint for Dutch requests will dynamicly switch by default
33
+ # config.dynamic_endpoints = false
34
+ end
35
+ ```
36
+
37
+ ## Methods
38
+
39
+ All methods can be called from the client object:
40
+ ```ruby
41
+ InternationalPostcodeApi::Client.autocomplete('Amsterdam', 'nld')
42
+ required: term
43
+ optional: country_code, default: 'nld'
44
+
45
+ InternationalPostcodeApi::Client.details('$1234...')
46
+ required: context
47
+
48
+ InternationalPostcodeApi::Client.dutch_postcode('1000AB', '50', 'A')
49
+ required: zipcode, house_number
50
+ optional: house_number_addition, default: nil
51
+
52
+ # important!! #postcode only returns a hash with :street and :city dynamicly depending on
53
+ # which endpoint you are using, if you need access to the raw response use #dutch_postcode or #autocomplete + #details
54
+
55
+ InternationalPostcodeApi::Client.postcode('1000AB', '50', 'DE')
56
+ required: zipcode, house_number
57
+ optional: country_code, default: 'NL'
58
+
59
+ InternationalPostcodeApi::Client.supported_countries
60
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "international_postcode_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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,27 @@
1
+ require_relative 'lib/international_postcode_api/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "international_postcode_api"
5
+ spec.version = InternationalPostcodeApi::VERSION
6
+ spec.authors = ["Dennis de Vulder"]
7
+ spec.email = ["dennisdevulder@gmail.com"]
8
+
9
+ spec.summary = %q{A ruby wrapper for the api.postcode.eu endpoints.}
10
+ spec.description = spec.summary
11
+ spec.homepage = 'https://github.com/advalley/international_postcode_api'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = 'https://github.com/advalley/international_postcode_api/blob/main/CHANGELOG.md'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,26 @@
1
+ require 'international_postcode_api/version'
2
+ require 'international_postcode_api/client'
3
+ require 'securerandom'
4
+ require 'byebug'
5
+
6
+ module InternationalPostcodeApi
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ def self.configure
12
+ self.configuration ||= Configuration.new
13
+ yield(configuration)
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :api_key, :secret_key, :base_uri, :dynamic_endpoints
18
+ attr_reader :session_token
19
+
20
+ def initialize
21
+ @base_uri = 'https://api.postcode.eu/international/v1/'
22
+ @session_token = SecureRandom.hex(8)
23
+ @dynamic_endpoints = true
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,61 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ module InternationalPostcodeApi
5
+ class Client
6
+ def self.autocomplete(term, context = 'nld')
7
+ uri = generate_uri(:autocomplete, context, term)
8
+ fetch(uri)
9
+ end
10
+
11
+ def self.details(context)
12
+ uri = generate_uri(:address, context)
13
+ fetch(uri)
14
+ end
15
+
16
+ def self.dutch_postcode(postcode, house_number, house_number_addition = nil)
17
+ encoded_uri = URI.encode(['https://api.postcode.eu/nl/v1/addresses/postcode', postcode, house_number, house_number_addition].join('/'))
18
+ fetch URI.parse(encoded_uri)
19
+ end
20
+
21
+ def self.supported_countries
22
+ uri = generate_uri('supported-countries')
23
+ fetch(uri)
24
+ end
25
+
26
+ def self.postcode(postcode, house_number, lang = 'NL')
27
+ if lang == 'NL' && InternationalPostcodeApi.configuration.dynamic_endpoints
28
+ response = dutch_postcode(postcode, house_number)
29
+ return { street: response.dig('street'), city: response.dig('city') }
30
+ else
31
+ query = autocomplete [postcode, house_number].join(' ')
32
+ context = query['matches'][0].dig('context')
33
+ if context
34
+ response = details(context)
35
+ return { street: response.dig('address').dig('street'), city: response.dig('address').dig('locality')}
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def self.generate_uri(*path)
43
+ encoded_uri = URI.encode(InternationalPostcodeApi.configuration.base_uri + path.join('/'))
44
+ URI.parse encoded_uri
45
+ end
46
+
47
+ def self.fetch(uri)
48
+ config = InternationalPostcodeApi.configuration
49
+
50
+ req = Net::HTTP::Get.new(uri)
51
+ req['X-Autocomplete-Session'] = config.session_token
52
+ req.basic_auth config.api_key, config.secret_key
53
+
54
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
55
+ http.request req
56
+ end
57
+
58
+ JSON.parse(response.body)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module InternationalPostcodeApi
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: international_postcode_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dennis de Vulder
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A ruby wrapper for the api.postcode.eu endpoints.
14
+ email:
15
+ - dennisdevulder@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - international_postcode_api.gemspec
32
+ - lib/international_postcode_api.rb
33
+ - lib/international_postcode_api/client.rb
34
+ - lib/international_postcode_api/version.rb
35
+ homepage: https://github.com/advalley/international_postcode_api
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/advalley/international_postcode_api
40
+ source_code_uri: https://github.com/advalley/international_postcode_api
41
+ changelog_uri: https://github.com/advalley/international_postcode_api/blob/main/CHANGELOG.md
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.3.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.0.3
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A ruby wrapper for the api.postcode.eu endpoints.
61
+ test_files: []