-
Notifications
You must be signed in to change notification settings - Fork 2
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
Stefan Åhman
committed
May 12, 2016
1 parent
053459c
commit 8335010
Showing
6 changed files
with
226 additions
and
4 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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
require "postnord/version" | ||
require 'uri' | ||
require 'net/http' | ||
require 'net/https' | ||
require 'json' | ||
require 'openssl' | ||
require 'byebug' | ||
|
||
module Postnord | ||
# Your code goes here... | ||
end | ||
require 'postnord/client' | ||
require 'postnord/base' | ||
require 'postnord/shipment' | ||
require 'postnord/version' |
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,37 @@ | ||
module Postnord | ||
class Base | ||
class MissingMandatoryParameters < StandardError; end | ||
|
||
def self.call(params) | ||
validate_params(params) | ||
|
||
client.do_request(service, endpoint, params) | ||
end | ||
|
||
def self.client | ||
@client ||= Client.new | ||
end | ||
|
||
private | ||
|
||
def self.validate_params(params) | ||
missing_params = (mandatory_params - params.keys.map(&:to_s)) | ||
|
||
unless missing_params.empty? | ||
fail MissingMandatoryParameters, missing_params | ||
end | ||
end | ||
|
||
def self.action | ||
self.name.split('::').last.tap { |e| e[0] = e[0].downcase } | ||
end | ||
|
||
def self.mandatory_params | ||
fail NotImplementedError, 'mandatory_params' | ||
end | ||
|
||
def self.endpoint | ||
fail NotImplementedError, 'endpoint' | ||
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,47 @@ | ||
module Postnord | ||
|
||
# Classes | ||
|
||
class BusinessLocation | ||
def self.service | ||
'businesslocation' | ||
end | ||
|
||
def self.endpoint | ||
"servicepoint/#{action}" | ||
end | ||
end | ||
|
||
class FindNearestByCoordinates | ||
def mandatory_params | ||
[ | ||
'countryCode', | ||
'northing', | ||
'easting', | ||
] | ||
end | ||
end | ||
|
||
class FindByPostalCode | ||
def mandatory_params | ||
[ | ||
'countryCode', | ||
'postalCode', | ||
] | ||
end | ||
end | ||
|
||
class FindNearestByAddress | ||
def mandatory_params | ||
[ | ||
'countryCode', | ||
] | ||
end | ||
end | ||
|
||
class GetServicePointInformation | ||
def mandatory_params | ||
[] | ||
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,46 @@ | ||
module Postnord | ||
class Client | ||
POSTNORD_API_KEY = '' | ||
POSTNORD_API_VERSION = 'v1' | ||
POSTNORD_API_ENDPOINT = 'https://api2.postnord.com/rest' | ||
POSTNORD_LOCALE = 'en' | ||
POSTNORD_RETURN_TYPE = 'json' | ||
|
||
def initialize(options={}) | ||
@api_version = options[:api_version] || POSTNORD_API_VERSION | ||
@api_key = options[:api_key] || POSTNORD_API_KEY | ||
@locale = options[:locale] || POSTNORD_LOCALE || 'en' | ||
@returntype = options[:returntype] || POSTNORD_RETURN_TYPE || 'json' | ||
end | ||
|
||
def do_request(service, endpoint, params={}) | ||
uri = build_uri(service, endpoint) | ||
|
||
params.merge!( | ||
apikey: POSTNORD_API_KEY, | ||
locale: POSTNORD_LOCALE, | ||
) | ||
|
||
uri.query = URI.encode_www_form(params) | ||
req = Net::HTTP::Get.new(uri) | ||
|
||
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http| | ||
http.request(req) | ||
end | ||
|
||
JSON.parse(res.body) | ||
end | ||
|
||
private | ||
|
||
def build_uri(service, endpoint) | ||
URI( | ||
POSTNORD_API_ENDPOINT + | ||
'/' + service + | ||
'/' + POSTNORD_API_VERSION + | ||
'/' + endpoint + | ||
'.' + POSTNORD_RETURN_TYPE | ||
) | ||
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,60 @@ | ||
module Postnord | ||
|
||
# Helper methods | ||
|
||
def self.find_by_identifier(id) | ||
FindByIdentifier.call( | ||
id: id, | ||
) | ||
end | ||
|
||
def self.find_by_notification_code(phone_number, notification_code) | ||
FindByNotificationCode.call( | ||
notificationPhoneNumber: phone_number, | ||
notificationCode: notification_code, | ||
) | ||
end | ||
|
||
def self.find_by_reference(customer_number, reference_value) | ||
FindByReference.call( | ||
customerNumber: customer_number, | ||
referenceValue: reference_value, | ||
) | ||
end | ||
|
||
# Classes | ||
|
||
class Shipment < Base | ||
def self.service | ||
'shipment' | ||
end | ||
|
||
def self.endpoint | ||
"trackandtrace/#{action}" | ||
end | ||
end | ||
|
||
class FindByIdentifier < Shipment | ||
def self.mandatory_params | ||
['id'] | ||
end | ||
end | ||
|
||
class FindByNotificationCode < Shipment | ||
def self.mandatory_params | ||
[ | ||
'notificationPhoneNumber', | ||
'notificationCode', | ||
] | ||
end | ||
end | ||
|
||
class FindByReference < Shipment | ||
def self.mandatory_params | ||
[ | ||
'customerNumber', | ||
'referenceValue', | ||
] | ||
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,26 @@ | ||
module Postnord | ||
|
||
# Classes | ||
|
||
class Transport < Base | ||
def self.service | ||
'transport' | ||
end | ||
|
||
def self.endpoint | ||
"transittime/#{action}" | ||
end | ||
end | ||
|
||
class GetTransitTimeInformation | ||
def mandatory_params | ||
[ | ||
'serviceGroupCode', | ||
'fromAddressPostalCode', | ||
'fromAddressCountryCode', | ||
'toAddressPostalCode', | ||
'toAddressCountryCode', | ||
] | ||
end | ||
end | ||
end |