Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Åhman committed May 12, 2016
1 parent 053459c commit 8335010
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/postnord.rb
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'
37 changes: 37 additions & 0 deletions lib/postnord/base.rb
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
47 changes: 47 additions & 0 deletions lib/postnord/business_location.rb
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
46 changes: 46 additions & 0 deletions lib/postnord/client.rb
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
60 changes: 60 additions & 0 deletions lib/postnord/shipment.rb
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
26 changes: 26 additions & 0 deletions lib/postnord/transport.rb
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

0 comments on commit 8335010

Please sign in to comment.