postnord 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/postnord.rb +11 -0
- data/lib/postnord/base.rb +37 -0
- data/lib/postnord/business_location.rb +47 -0
- data/lib/postnord/client.rb +46 -0
- data/lib/postnord/shipment.rb +60 -0
- data/lib/postnord/transport.rb +26 -0
- data/lib/postnord/version.rb +3 -0
- data/postnord.gemspec +24 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 625ad1e48069d8cbc99a4b6c526e0a48bce29fd4
|
4
|
+
data.tar.gz: 42047c6e4d7275f2149d5a7bb50387eb6fc181ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d3608f020020a0657c1843baed46704dc4d43d8cab00589de24743e8c6bd42347bfae361dad31ae25fd166ad20cbf2811ea758e5bb7d09da24155608db8c83e
|
7
|
+
data.tar.gz: c5a26090a77829f2c95cb8bee2e7db2a3595348a73d3a8c1a931254334ff960ea736c610891cefc84b6917f9c7d41cdf6aa422556a15b907e9b21cbd9902b8e5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Stefan Åhman
|
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,41 @@
|
|
1
|
+
# Postnord
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/postnord`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'postnord'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install postnord
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/postnord. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "postnord"
|
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
|
data/bin/setup
ADDED
data/lib/postnord.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Postnord
|
2
|
+
class Base
|
3
|
+
class MissingMandatoryParameters < StandardError; end
|
4
|
+
|
5
|
+
def self.call(params)
|
6
|
+
validate_params(params)
|
7
|
+
|
8
|
+
client.do_request(service, endpoint, params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.client
|
12
|
+
@client ||= Client.new
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def self.validate_params(params)
|
18
|
+
missing_params = (mandatory_params - params.keys.map(&:to_s))
|
19
|
+
|
20
|
+
unless missing_params.empty?
|
21
|
+
fail MissingMandatoryParameters, missing_params
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.action
|
26
|
+
self.name.split('::').last.tap { |e| e[0] = e[0].downcase }
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.mandatory_params
|
30
|
+
fail NotImplementedError, 'mandatory_params'
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.endpoint
|
34
|
+
fail NotImplementedError, 'endpoint'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Postnord
|
2
|
+
|
3
|
+
# Classes
|
4
|
+
|
5
|
+
class BusinessLocation
|
6
|
+
def self.service
|
7
|
+
'businesslocation'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.endpoint
|
11
|
+
"servicepoint/#{action}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class FindNearestByCoordinates
|
16
|
+
def mandatory_params
|
17
|
+
[
|
18
|
+
'countryCode',
|
19
|
+
'northing',
|
20
|
+
'easting',
|
21
|
+
]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class FindByPostalCode
|
26
|
+
def mandatory_params
|
27
|
+
[
|
28
|
+
'countryCode',
|
29
|
+
'postalCode',
|
30
|
+
]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class FindNearestByAddress
|
35
|
+
def mandatory_params
|
36
|
+
[
|
37
|
+
'countryCode',
|
38
|
+
]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class GetServicePointInformation
|
43
|
+
def mandatory_params
|
44
|
+
[]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Postnord
|
2
|
+
class Client
|
3
|
+
POSTNORD_API_KEY = ''
|
4
|
+
POSTNORD_API_VERSION = 'v1'
|
5
|
+
POSTNORD_API_ENDPOINT = 'https://api2.postnord.com/rest'
|
6
|
+
POSTNORD_LOCALE = 'en'
|
7
|
+
POSTNORD_RETURN_TYPE = 'json'
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
@api_version = options[:api_version] || POSTNORD_API_VERSION
|
11
|
+
@api_key = options[:api_key] || POSTNORD_API_KEY
|
12
|
+
@locale = options[:locale] || POSTNORD_LOCALE || 'en'
|
13
|
+
@returntype = options[:returntype] || POSTNORD_RETURN_TYPE || 'json'
|
14
|
+
end
|
15
|
+
|
16
|
+
def do_request(service, endpoint, params={})
|
17
|
+
uri = build_uri(service, endpoint)
|
18
|
+
|
19
|
+
params.merge!(
|
20
|
+
apikey: POSTNORD_API_KEY,
|
21
|
+
locale: POSTNORD_LOCALE,
|
22
|
+
)
|
23
|
+
|
24
|
+
uri.query = URI.encode_www_form(params)
|
25
|
+
req = Net::HTTP::Get.new(uri)
|
26
|
+
|
27
|
+
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
28
|
+
http.request(req)
|
29
|
+
end
|
30
|
+
|
31
|
+
JSON.parse(res.body)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def build_uri(service, endpoint)
|
37
|
+
URI(
|
38
|
+
POSTNORD_API_ENDPOINT +
|
39
|
+
'/' + service +
|
40
|
+
'/' + POSTNORD_API_VERSION +
|
41
|
+
'/' + endpoint +
|
42
|
+
'.' + POSTNORD_RETURN_TYPE
|
43
|
+
)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Postnord
|
2
|
+
|
3
|
+
# Helper methods
|
4
|
+
|
5
|
+
def self.find_by_identifier(id)
|
6
|
+
FindByIdentifier.call(
|
7
|
+
id: id,
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.find_by_notification_code(phone_number, notification_code)
|
12
|
+
FindByNotificationCode.call(
|
13
|
+
notificationPhoneNumber: phone_number,
|
14
|
+
notificationCode: notification_code,
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.find_by_reference(customer_number, reference_value)
|
19
|
+
FindByReference.call(
|
20
|
+
customerNumber: customer_number,
|
21
|
+
referenceValue: reference_value,
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Classes
|
26
|
+
|
27
|
+
class Shipment < Base
|
28
|
+
def self.service
|
29
|
+
'shipment'
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.endpoint
|
33
|
+
"trackandtrace/#{action}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class FindByIdentifier < Shipment
|
38
|
+
def self.mandatory_params
|
39
|
+
['id']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class FindByNotificationCode < Shipment
|
44
|
+
def self.mandatory_params
|
45
|
+
[
|
46
|
+
'notificationPhoneNumber',
|
47
|
+
'notificationCode',
|
48
|
+
]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class FindByReference < Shipment
|
53
|
+
def self.mandatory_params
|
54
|
+
[
|
55
|
+
'customerNumber',
|
56
|
+
'referenceValue',
|
57
|
+
]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Postnord
|
2
|
+
|
3
|
+
# Classes
|
4
|
+
|
5
|
+
class Transport < Base
|
6
|
+
def self.service
|
7
|
+
'transport'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.endpoint
|
11
|
+
"transittime/#{action}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class GetTransitTimeInformation
|
16
|
+
def mandatory_params
|
17
|
+
[
|
18
|
+
'serviceGroupCode',
|
19
|
+
'fromAddressPostalCode',
|
20
|
+
'fromAddressCountryCode',
|
21
|
+
'toAddressPostalCode',
|
22
|
+
'toAddressCountryCode',
|
23
|
+
]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/postnord.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'postnord/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "postnord"
|
8
|
+
spec.version = Postnord::VERSION
|
9
|
+
spec.authors = ["Stefan Åhman"]
|
10
|
+
spec.email = ["stefan.ahman@apoex.se"]
|
11
|
+
|
12
|
+
spec.summary = %q{A gem for Postnord's API}
|
13
|
+
spec.homepage = 'https://github.com/apoex/postnord-gem'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postnord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefan Åhman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- stefan.ahman@apoex.se
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/postnord.rb
|
72
|
+
- lib/postnord/base.rb
|
73
|
+
- lib/postnord/business_location.rb
|
74
|
+
- lib/postnord/client.rb
|
75
|
+
- lib/postnord/shipment.rb
|
76
|
+
- lib/postnord/transport.rb
|
77
|
+
- lib/postnord/version.rb
|
78
|
+
- postnord.gemspec
|
79
|
+
homepage: https://github.com/apoex/postnord-gem
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.5.1
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: A gem for Postnord's API
|
103
|
+
test_files: []
|