shipping_connector 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98c92bddd04b8f92e6630534b66d4e169583db1264fd56ae0ae664aed41e2ebf
4
- data.tar.gz: 48e528d536d25680d36127600bb785ac267ce11b9a082b0b4c2df94ff8ad5b60
3
+ metadata.gz: b5dc5dd88edc69471c3d50e15f9e84e59cc604fbd4a7a33f6f37310a67e7060e
4
+ data.tar.gz: 86629918a4474ab6abb378ddd6f8b3c9500eaa86a7f8d343de498edc97cf23e1
5
5
  SHA512:
6
- metadata.gz: be449f20f233c15e90b0a2679d878eac4dfefaddf95645f62eaf474888db9a79369a2ebc2fb2c2c76e81a4a6e95064c2a4de35459749d317bddcb6cf7e5a0e9c
7
- data.tar.gz: b48be38a98eb8cc2f341d0177b491b2eb23859a098d782f8b358b117828dd6268fbcc4f3b04fe33360cca592ac5a643b0270cc38f935d793bbc9e1660df27dc2
6
+ metadata.gz: 6b591b23947cd52b0783bd376e3137fcadc996ba59e85cc76b3af86fa34b416f988fe3547eb790bdf67c71f24cb9f9b9c118f46407107b08b4f7b4be59bae4be
7
+ data.tar.gz: '094dbffaabfc78dfd8086614e1c40e4274f0c317cde2467ddaccf9e372b15c7b4cb1b30675473c18347678f699c1427fe669ec5c97145cc3dacfb3e21bec9963'
@@ -8,13 +8,12 @@ module ShippingConnector
8
8
  # @overload initialize(customer_id, password)
9
9
  # @param customer_id [Integer] login details for the API user
10
10
  # @param password [String] login details for the API user
11
- #
12
11
  def initialize(options = {})
13
12
  require! options, :customer_id, :password
14
13
  super
15
14
  end
16
15
 
17
- # Returns a list of service points or a single service point
16
+ # Returns a list of service points or a single service point. The returned distance is as the crow flies.
18
17
  # @overload service_points(scope, zip_code, address, limit = 10)
19
18
  # @param scope [Symbol] the scope: `:list` for listing nearest service points
20
19
  # @param zip_code [Integer, String] zip code for address to search from
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShippingConnector
4
+ class Postnord < Carrier
5
+ self.url = 'api2.postnord.com'
6
+
7
+ # Initializes a new carrier object for Postnord
8
+ # @overload initialize(api_key)
9
+ # @param api_key [String] authentication details
10
+ def initialize(options = {})
11
+ require! options, :api_key
12
+ super
13
+ end
14
+
15
+ # Returns a list of service points or a single service point. The returned distance is based on driving route.
16
+ # @overload service_points(scope, zip_code, city, address, country, limit = 10)
17
+ # @param scope [Symbol] :list or :list_address to find by address
18
+ # @param zip_code [String] this or city is required
19
+ # @param city [String] this or zip_code is required
20
+ # @param address [String] street address to search from
21
+ # @param limit [String] amount of service points to be returned
22
+ # @return [Array<ServicePoint>] the nearest service points ordered by distance
23
+ # @overload service_points(scope, latitude, longitude, limit = 10)
24
+ # @param scope [Symbol] :list_coordinates to find by coordinates
25
+ # @param latitude [String] required
26
+ # @param longitude [String] required
27
+ # @param limit [String] amount of service points to be returned
28
+ # @return [Array<ServicePoint>] the nearest service points ordered by distance
29
+ # @overload service_points(id)
30
+ # @param id [Integer] the id of the service_point to be returned
31
+ # @return [ServicePoint] the service point for the given id
32
+ def service_points(*arguments)
33
+ scope = arguments.slice!(0)
34
+ options = arguments.slice!(0) || {}
35
+
36
+ case scope
37
+ when :list, :list_address
38
+ require!(options, :country)
39
+ list_service_points(options)
40
+ when :list_coordinates
41
+ require!(options, :country, :latitude, :longitude)
42
+ list_by_coordinates(options)
43
+ else
44
+ require!(options, :country)
45
+ find_service_point(scope, options)
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def auth_params
52
+ { apikey: @options[:api_key], returnType: 'json' }
53
+ end
54
+
55
+ def get(path, params)
56
+ response = super(path, params.merge(auth_params))
57
+ JSON.parse response.body
58
+ rescue Faraday::ClientError => e
59
+ body = JSON.parse e.response[:body]
60
+ raise StandardError, "Postnord error: #{body['message']}"
61
+ end
62
+
63
+ def find_service_point(id, arguments)
64
+ service_point = get('/rest/businesslocation/v5/servicepoints/ids',
65
+ {
66
+ ids: id, countryCode: arguments[:country]
67
+ })['servicePointInformationResponse']['servicePoints'].first
68
+
69
+ ServicePoint.new(
70
+ id: service_point['servicePointId'], zip_code: service_point['visitingAddress']['postalCode'],
71
+ name: service_point['name'], city: service_point['visitingAddress']['city'],
72
+ address: "#{service_point['visitingAddress']['streetName']} #{service_point['visitingAddress']['streetName']}",
73
+ opening_hours: opening_hours(service_point['openingHours'])
74
+ )
75
+ end
76
+
77
+ def list_service_points(options)
78
+ unless options.key?(:city) || options.key?(:zip_code)
79
+ raise ArgumentError, 'At least one of :city or :zip_code is required'
80
+ end
81
+
82
+ params = { countryCode: options[:country] }
83
+
84
+ params[:city] = options[:city] if options[:city]
85
+ params[:postalCode] = options[:postalCode] if options[:zip_code]
86
+ params[:streetName] = options[:address] if options[:address]
87
+ params[:numberOfServicePoints] = options[:limit] || 10
88
+
89
+ array = get('/rest/businesslocation/v5/servicepoints/nearest/byaddress',
90
+ params)['servicePointInformationResponse']['servicePoints']
91
+
92
+ generate_service_points array
93
+ end
94
+
95
+ def list_by_coordinates(options)
96
+ array = get('/rest/businesslocation/v5/servicepoints/nearest/bycoordinates',
97
+ {
98
+ countryCode: options[:country],
99
+ northing: options[:latitude],
100
+ easting: options[:longitude],
101
+ numberOfServicePoints: options[:limit] || 10
102
+ })['servicePointInformationResponse']['servicePoints']
103
+
104
+ generate_service_points array
105
+ end
106
+
107
+ def generate_service_points(array)
108
+ result = []
109
+ array.each do |service_point|
110
+ result << ServicePoint.new(
111
+ id: service_point['servicePointId'], zip_code: service_point['visitingAddress']['postalCode'],
112
+ name: service_point['name'], city: service_point['visitingAddress']['city'],
113
+ address: "#{service_point['visitingAddress']['streetName']} #{service_point['visitingAddress']['streetName']}",
114
+ distance: service_point['routeDistance'], opening_hours: opening_hours(service_point['openingHours'])
115
+ )
116
+ end
117
+ result
118
+ end
119
+
120
+ def opening_hours(args)
121
+ hash = {}
122
+
123
+ args['postalServices'].each do |h|
124
+ hash[h['openDay'].downcase.to_s] = "#{h['openTime']} - #{h['closeTime']}"
125
+ end
126
+
127
+ ServicePoint::OpeningHours.new(hash)
128
+ end
129
+ end
130
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shipping_connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Thyregod Kristensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-24 00:00:00.000000000 Z
11
+ date: 2021-10-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  ShippingConnector is an abstraction library that makes connecting to various shipping
@@ -22,6 +22,7 @@ files:
22
22
  - lib/shipping_connector.rb
23
23
  - lib/shipping_connector/carrier.rb
24
24
  - lib/shipping_connector/carrier/dao.rb
25
+ - lib/shipping_connector/carrier/postnord.rb
25
26
  - lib/shipping_connector/carriers.rb
26
27
  - lib/shipping_connector/service_point.rb
27
28
  homepage: https://github.com/sthyregod/shipping_connector