shipping_connector 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 98c92bddd04b8f92e6630534b66d4e169583db1264fd56ae0ae664aed41e2ebf
4
+ data.tar.gz: 48e528d536d25680d36127600bb785ac267ce11b9a082b0b4c2df94ff8ad5b60
5
+ SHA512:
6
+ metadata.gz: be449f20f233c15e90b0a2679d878eac4dfefaddf95645f62eaf474888db9a79369a2ebc2fb2c2c76e81a4a6e95064c2a4de35459749d317bddcb6cf7e5a0e9c
7
+ data.tar.gz: b48be38a98eb8cc2f341d0177b491b2eb23859a098d782f8b358b117828dd6268fbcc4f3b04fe33360cca592ac5a643b0270cc38f935d793bbc9e1660df27dc2
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShippingConnector
4
+ class Dao < Carrier
5
+ self.url = 'https://api.dao.as'
6
+
7
+ # Initializes a new carrier object for DAO
8
+ # @overload initialize(customer_id, password)
9
+ # @param customer_id [Integer] login details for the API user
10
+ # @param password [String] login details for the API user
11
+ #
12
+ def initialize(options = {})
13
+ require! options, :customer_id, :password
14
+ super
15
+ end
16
+
17
+ # Returns a list of service points or a single service point
18
+ # @overload service_points(scope, zip_code, address, limit = 10)
19
+ # @param scope [Symbol] the scope: `:list` for listing nearest service points
20
+ # @param zip_code [Integer, String] zip code for address to search from
21
+ # @param address [String] street address to search from
22
+ # @param limit [Integer] amount of service points to be returned
23
+ # @return [Array<ServicePoint>] the nearest service points ordered by distance
24
+ # @overload service_points(id)
25
+ # @param id [Integer] the `id` of the service_point to be returned
26
+ # @return [ServicePoint] the service point for the given `id`
27
+ def service_points(*arguments)
28
+ scope = arguments.slice!(0)
29
+ options = arguments.slice!(0) || {}
30
+
31
+ case scope
32
+ when :list
33
+ list_service_points(options)
34
+ else
35
+ find_service_point(scope)
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def auth_params
42
+ { kundeid: @options[:customer_id], kode: @options[:password] }
43
+ end
44
+
45
+ def find_service_point(id)
46
+
47
+ service_point = get('/DAOPakkeshop/FindPakkeshop.php', { id: id })['pakkeshops'].first
48
+
49
+ ServicePoint.new(id: service_point['shopId'], name: service_point['navn'],
50
+ address: service_point['adresse'], zip_code: service_point['postnr'],
51
+ city: service_point['bynavn'], opening_hours: opening_hours(service_point['aabningstider']))
52
+ end
53
+
54
+ def list_service_points(options)
55
+ require! options, :zip_code, :address
56
+
57
+ array = get('/DAOPakkeshop/FindPakkeshop.php',
58
+ {
59
+ postnr: options[:zip_code],
60
+ adresse: options[:address],
61
+ antal: options[:limit] || 10
62
+ })['pakkeshops']
63
+
64
+ generate_service_points array
65
+ end
66
+
67
+ def get(path, params)
68
+ response = super(path, params.merge(auth_params))
69
+ body = JSON.parse response.body
70
+
71
+ return body['resultat'] if body['status'] == 'OK'
72
+
73
+ raise StandardError, "DAO errror ##{body['fejlkode']}: #{body['fejltekst']}"
74
+ end
75
+
76
+ def generate_service_points(array)
77
+ result = []
78
+ array.each do |service_point|
79
+ result << ServicePoint.new(id: service_point['shopId'], name: service_point['navn'],
80
+ address: service_point['adresse'], zip_code: service_point['postnr'],
81
+ city: service_point['bynavn'], distance: service_point['afstand'],
82
+ opening_hours: opening_hours(service_point['aabningstider']))
83
+ end
84
+ result
85
+ end
86
+
87
+ def opening_hours(args)
88
+ hash = {}
89
+
90
+ args.each do |weekday, hours|
91
+ hash[weekdays[weekday]] = hours
92
+ end
93
+
94
+ ServicePoint::OpeningHours.new(hash)
95
+ end
96
+
97
+ def weekdays
98
+ { 'man' => :monday, 'tir' => :tuesday, 'ons' => :wednesday, 'tor' => :thursday,
99
+ 'fre' => :friday, 'lor' => :saturday, 'son' => :sunday }
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/class/attribute'
4
+ require 'faraday'
5
+ require 'json'
6
+
7
+ module ShippingConnector
8
+ class Carrier
9
+ class_attribute :url
10
+
11
+ def initialize(options = {})
12
+ @options = options
13
+ self.url = options[:mock_url] if options[:mock_url]
14
+ end
15
+
16
+ def require!(hash, *options)
17
+ options.each do |option|
18
+ raise ArgumentError, "Missing required parameter: #{option}" unless hash.key? option
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ # TODO: rescue HTTP error codes (or not?)
25
+ def get(path, params)
26
+ connection.get(path, params)
27
+ end
28
+
29
+ def connection
30
+ Faraday.new url
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'carrier/dao'
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShippingConnector
4
+ class ServicePoint
5
+ attr_accessor :id, :name, :address, :zip_code, :city, :distance, :opening_hours
6
+
7
+ def initialize(params = {})
8
+ params.each { |key, value| instance_variable_set("@#{key}", value) }
9
+ end
10
+
11
+ class OpeningHours
12
+ attr_accessor :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
13
+
14
+ def initialize(params)
15
+ params.each { |key, value| instance_variable_set("@#{key}", value) }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'json'
5
+ require 'active_support/core_ext/class/attribute'
6
+
7
+ require 'shipping_connector/carrier'
8
+ require 'shipping_connector/carriers'
9
+ require 'shipping_connector/service_point'
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shipping_connector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Thyregod Kristensen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ ShippingConnector is an abstraction library that makes connecting to various shipping
15
+ carriers' APIs easier. As with everything Ruby, the goal is to make writing code that
16
+ handles shipping logic fast and comfortable
17
+ email: git@simon.thyregod.eu
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/shipping_connector.rb
23
+ - lib/shipping_connector/carrier.rb
24
+ - lib/shipping_connector/carrier/dao.rb
25
+ - lib/shipping_connector/carriers.rb
26
+ - lib/shipping_connector/service_point.rb
27
+ homepage: https://github.com/sthyregod/shipping_connector
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '2.5'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.2.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A simple shipping_connector abstraction library
50
+ test_files: []