class ShippingConnector::Dao

Public Class Methods

new(options = {}) click to toggle source

Initializes a new carrier object for DAO @overload initialize(customer_id, password)

@param customer_id [Integer] login details for the API user
@param password [String] login details for the API user
Calls superclass method ShippingConnector::Carrier::new
# File lib/shipping_connector/carrier/dao.rb, line 11
def initialize(options = {})
  require! options, :customer_id, :password
  super
end

Public Instance Methods

service_points(*arguments) click to toggle source

Returns a list of service points or a single service point. The returned distance is as the crow flies. @overload service_points(scope, zip_code, address, limit = 10)

@param scope [Symbol] the scope: `:list` for listing nearest service points
@param zip_code [Integer, String] zip code for address to search from
@param address [String] street address to search from
@param limit [Integer] amount of service points to be returned
@return [Array<ServicePoint>] the nearest service points ordered by distance

@overload service_points(id)

@param id [Integer] the `id` of the service_point to be returned
@return [ServicePoint] the service point for the given `id`
# File lib/shipping_connector/carrier/dao.rb, line 26
def service_points(*arguments)
  scope   = arguments.slice!(0)
  options = arguments.slice!(0) || {}

  case scope
  when :list
    list_service_points(options)
  else
    find_service_point(scope)
  end
end

Private Instance Methods

auth_params() click to toggle source
# File lib/shipping_connector/carrier/dao.rb, line 40
def auth_params
  { kundeid: @options[:customer_id], kode: @options[:password] }
end
find_service_point(id) click to toggle source
# File lib/shipping_connector/carrier/dao.rb, line 44
def find_service_point(id)

  service_point = get('/DAOPakkeshop/FindPakkeshop.php', { id: id })['pakkeshops'].first

  ServicePoint.new(id: service_point['shopId'], name: service_point['navn'],
                   address: service_point['adresse'], zip_code: service_point['postnr'],
                   city: service_point['bynavn'], opening_hours: opening_hours(service_point['aabningstider']))
end
generate_service_points(array) click to toggle source
# File lib/shipping_connector/carrier/dao.rb, line 75
def generate_service_points(array)
  result = []
  array.each do |service_point|
    result << ServicePoint.new(id: service_point['shopId'], name: service_point['navn'],
                               address: service_point['adresse'], zip_code: service_point['postnr'],
                               city: service_point['bynavn'], distance: service_point['afstand'],
                               opening_hours: opening_hours(service_point['aabningstider']))
  end
  result
end
get(path, params) click to toggle source
Calls superclass method ShippingConnector::Carrier#get
# File lib/shipping_connector/carrier/dao.rb, line 66
def get(path, params)
  response = super(path, params.merge(auth_params))
  body = JSON.parse response.body

  return body['resultat'] if body['status'] == 'OK'

  raise StandardError, "DAO errror ##{body['fejlkode']}: #{body['fejltekst']}"
end
list_service_points(options) click to toggle source
# File lib/shipping_connector/carrier/dao.rb, line 53
def list_service_points(options)
  require! options, :zip_code, :address

  array = get('/DAOPakkeshop/FindPakkeshop.php',
              {
                postnr: options[:zip_code],
                adresse: options[:address],
                antal: options[:limit] || 10
              })['pakkeshops']

  generate_service_points array
end
opening_hours(args) click to toggle source
# File lib/shipping_connector/carrier/dao.rb, line 86
def opening_hours(args)
  hash = {}

  args.each do |weekday, hours|
    hash[weekdays[weekday]] = hours
  end

  ServicePoint::OpeningHours.new(hash)
end
weekdays() click to toggle source
# File lib/shipping_connector/carrier/dao.rb, line 96
def weekdays
  { 'man' => :monday, 'tir' => :tuesday, 'ons' => :wednesday, 'tor' => :thursday,
    'fre' => :friday, 'lor' => :saturday, 'son' => :sunday }
end