janio_api 0.1.11 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e5a4f0c3a8d908fb97aaa1619d6bbf3e8d7cc8ad9128c6691800e3c7d3601f7
4
- data.tar.gz: 9c5a2cf08df7e220a5a098babc783c04d031cd41628b9899f29f09aa32654067
3
+ metadata.gz: 6f2e5b733830dbef549108d5c13cf4393e74c82e50183728f3f553bdf4434a99
4
+ data.tar.gz: c0f92b02cdc5d0d0df9ed0b7dab082695488928697c948fe61a01e7ca36191b8
5
5
  SHA512:
6
- metadata.gz: 29b90689b835150963c56944d25b1aca6a0370ad79c5f0dee34cd0f0062ba4587aba356c939f5716edc1f8435c0b5001a8dc3c2f61e44e57d1e9db49e6cf8584
7
- data.tar.gz: cb6f23615af5fcde865da4b1ca0a862652537be167e05d3a93eedae0bc86f912d430ab80202f929568d1858ac48074ce6cb594ea1a4bc39b7bea69203ba23699
6
+ metadata.gz: 8b2a4c8145c67f5b2e082881c044ff48b4c95fb39de9b5e77800c140c77b3fc0ab35acf88d1fc1a278383269fadcfe68326ced6c3c19caf6fe30f17bf050f025
7
+ data.tar.gz: 5a2fa08310d2cd3968dcf81df2211d3f3075262af22e339f54163b3e878e7220827fe266f9a0afe2bae1b155e241adcd6de80885683c46b9943129dac18d3082
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- janio_api (0.1.11)
4
+ janio_api (0.2.0)
5
5
  activeresource
6
6
 
7
7
  GEM
data/bin/console CHANGED
@@ -5,10 +5,7 @@ require "janio_api"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
- JanioAPI.configure do |config|
9
- config.api_host = ENV["API_HOST"]
10
- config.api_token = ENV["API_TOKEN"]
11
- end
8
+ require "dev_config"
12
9
 
13
10
  # (If you use this, don't forget to add pry to your Gemfile!)
14
11
  require "pry"
data/lib/dev_config.rb ADDED
@@ -0,0 +1,7 @@
1
+ def set_config
2
+ JanioAPI.configure do |config|
3
+ config.api_host = ENV["API_HOST"]
4
+ config.api_token = ENV["API_TOKEN"]
5
+ end
6
+ end
7
+ set_config
@@ -15,29 +15,71 @@ module JanioAPI
15
15
 
16
16
  self.collection_parser = Collection
17
17
 
18
- SUPPORTED_COUNTRIES = ["Australia", "Brunei", "China", "Hong Kong", "Indonesia", "Italy", "Japan", "Malaysia", "Philippines", "Saudi Arabia",
19
- "Singapore", "South Korea", "Spain", "Taiwan", "Thailand", "UAE", "UK", "US", "Vietnam"].freeze
18
+ SUPPORTED_PICKUP_COUNTRIES = ["Singapore", "China", "Hong Kong", "Indonesia", "Malaysia", "Philippines", "Thailand"].freeze
19
+ SUPPORTED_CONSIGNEE_COUNTRIES = ["Indonesia", "Singapore", "Thailand", "Malaysia", "Philippines", "China", "Hong Kong", "Taiwan", "Brunei", "South Korea", "Japan", "Vietnam"].freeze
20
20
 
21
21
  POSTAL_EXCLUDED_COUNTRIES = ["Hong Kong", "Vietnam"].freeze
22
22
  VALID_PAYMENT_TYPES = ["cod", "prepaid"].freeze
23
23
 
24
+ SERVICE_ID_MAP = [
25
+ {id: 1, from: "Singapore", to: "Indonesia"},
26
+ {id: 6, from: "China", to: "Indonesia"},
27
+ {id: 5, from: "China", to: "Singapore"},
28
+ {id: 7, from: "China", to: "Thailand"},
29
+ {id: 11, from: "Hong Kong", to: "Indonesia"},
30
+ {id: 37, from: "Hong Kong", to: "Malaysia"},
31
+ {id: 33, from: "Hong Kong", to: "Philippines"},
32
+ {id: 13, from: "Hong Kong", to: "Singapore"},
33
+ {id: 12, from: "Hong Kong", to: "Thailand"},
34
+ {id: 41, from: "Indonesia", to: "China"},
35
+ {id: 41, from: "Indonesia", to: "Hong Kong"},
36
+ {id: 41, from: "Indonesia", to: "Taiwan"},
37
+ {id: 41, from: "Indonesia", to: "Brunei"},
38
+ {id: 41, from: "Indonesia", to: "Philippines"},
39
+ {id: 41, from: "Indonesia", to: "Malaysia"},
40
+ {id: 3, from: "Indonesia", to: "Indonesia"},
41
+ {id: 47, from: "Indonesia", to: "Philippines"},
42
+ {id: 54, from: "Indonesia", to: "Philippines"},
43
+ {id: 20, from: "Indonesia", to: "Singapore"},
44
+ {id: 48, from: "Indonesia", to: "Singapore"},
45
+ {id: 22, from: "Indonesia", to: "South Korea"},
46
+ {id: 22, from: "Indonesia", to: "Japan"},
47
+ {id: 22, from: "Indonesia", to: "Vietnam"},
48
+ {id: 4, from: "Malaysia", to: "Indonesia"},
49
+ {id: 40, from: "Malaysia", to: "Malaysia"},
50
+ {id: 36, from: "Philippines", to: "Philippines"},
51
+ {id: 2, from: "Singapore", to: "China"},
52
+ {id: 2, from: "Singapore", to: "Taiwan"},
53
+ {id: 2, from: "Singapore", to: "South Korea"},
54
+ {id: 2, from: "Singapore", to: "Brunei"},
55
+ {id: 2, from: "Singapore", to: "Vietnam"},
56
+ {id: 2, from: "Singapore", to: "Hong Kong"},
57
+ {id: 2, from: "Singapore", to: "Japan"},
58
+ {id: 26, from: "Singapore", to: "Malaysia"},
59
+ {id: 10, from: "Singapore", to: "Singapore"},
60
+ {id: 17, from: "Singapore", to: "Thailand"},
61
+ {id: 34, from: "Thailand", to: "Indonesia"},
62
+ {id: 35, from: "Thailand", to: "Singapore"}
63
+ ].freeze
64
+
24
65
  self.prefix = "/api/order/orders/"
25
66
  self.element_name = ""
26
67
 
27
68
  has_many :items, class_name: "JanioAPI::Item"
28
69
 
29
- validates :service_id, :order_length, :order_width, :order_height, :order_weight,
30
- :cod_amount_to_collect, :consignee_name, :consignee_number, :consignee_country, :consignee_address, :consignee_state,
31
- :consignee_city, :consignee_province, :consignee_email, :pickup_contact_name, :pickup_contact_number, :pickup_country, :pickup_address,
32
- :pickup_state, :pickup_city, :pickup_province, :pickup_date, :pickup_notes, presence: true
70
+ validates :service_id, :order_length, :order_width, :order_height, :order_weight, :cod_amount_to_collect, :consignee_name, :consignee_number, :consignee_country,
71
+ :consignee_address, :consignee_state, :consignee_email, :pickup_contact_name, :pickup_contact_number, :pickup_country, :pickup_address,
72
+ :pickup_state, :pickup_date, presence: true
33
73
 
34
- validates :pickup_country, :consignee_country, inclusion: {in: SUPPORTED_COUNTRIES, message: "%{value} is not a supported country, supported countries are #{SUPPORTED_COUNTRIES.join(", ")}"}
74
+ validates :pickup_country, inclusion: {in: SUPPORTED_PICKUP_COUNTRIES, message: "%{value} is not a supported pickup country, supported countries are #{SUPPORTED_PICKUP_COUNTRIES.join(", ")}"}
75
+ validates :consignee_country, inclusion: {in: SUPPORTED_CONSIGNEE_COUNTRIES, message: "%{value} is not a supported consignee country, supported countries are #{SUPPORTED_CONSIGNEE_COUNTRIES.join(", ")}"}
35
76
  validates :pickup_postal, :consignee_postal, presence: true, unless: -> { POSTAL_EXCLUDED_COUNTRIES.include?(consignee_country) }
36
77
  validates :pickup_postal, presence: true, unless: -> { POSTAL_EXCLUDED_COUNTRIES.include?(pickup_country) }
37
78
  validates :payment_type, inclusion: {in: VALID_PAYMENT_TYPES, message: "%{value} is not a valid payment type, valid payment types are #{VALID_PAYMENT_TYPES.join(", ")}"}
38
79
  validates :cod_amount_to_collect, presence: true, if: -> { payment_type == "cod" }
39
80
  validates :items, length: {minimum: 1, message: "are required. Please add at least one."}
40
81
  validate :items_validation
82
+ validate :route_supported?
41
83
 
42
84
  class << self
43
85
  def tracking_path
@@ -119,6 +161,15 @@ module JanioAPI
119
161
  }
120
162
  attributes = default_attrs.merge(attributes)
121
163
  super
164
+ set_service_id
165
+ end
166
+
167
+ def get_service_id
168
+ SERVICE_ID_MAP.find { |route| route[:from] == pickup_country && route[:to] == consignee_country }&.dig(:id)
169
+ end
170
+
171
+ def set_service_id
172
+ @attributes[:service_id] = get_service_id
122
173
  end
123
174
 
124
175
  # Tracks the current order
@@ -128,7 +179,7 @@ module JanioAPI
128
179
  body = {
129
180
  get_related_updates: true,
130
181
  flatten_data: true,
131
- tracking_nos: [@attributes["tracking_no"]]
182
+ tracking_nos: [@attributes[:tracking_no]]
132
183
  }
133
184
  response = connection.post(self.class.tracking_path, body.to_json, self.class.headers)
134
185
  self.class.format.decode(response.body)[0]
@@ -142,6 +193,12 @@ module JanioAPI
142
193
 
143
194
  private
144
195
 
196
+ def route_supported?
197
+ unless set_service_id
198
+ errors.add(:route, "not supported, if new route not available in service_id_map, please contact gem author.")
199
+ end
200
+ end
201
+
145
202
  def items_validation
146
203
  items&.each_with_index do |item, index|
147
204
  item.errors.full_messages.each { |msg| errors.add("item_#{index}".to_sym, msg) } unless item.valid?
@@ -168,11 +225,11 @@ module JanioAPI
168
225
  def reformat_before_save(blocking)
169
226
  attributes = @attributes.dup
170
227
  @attributes.clear
171
- @attributes["secret_key"] = JanioAPI.config.api_token
228
+ @attributes[:secret_key] = JanioAPI.config.api_token
172
229
  # set blocking until label generated
173
- @attributes["blocking"] = blocking
230
+ @attributes[:blocking] = blocking
174
231
  # reformat attributes
175
- @attributes["orders"] = [attributes]
232
+ @attributes[:orders] = [attributes]
176
233
  end
177
234
 
178
235
  def reset_attributes_format
@@ -1,3 +1,3 @@
1
1
  module JanioAPI
2
- VERSION = "0.1.11"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/janio_api.rb CHANGED
@@ -2,7 +2,7 @@ require "janio_api/version"
2
2
  require "active_resource"
3
3
  require "dotenv/load"
4
4
  require_relative "zeitwerk_loader" if ENV["JANIO_API_GEM_ENV"] == "development"
5
-
5
+ require_relative "patch_exception"
6
6
  module JanioAPI
7
7
  require "janio_api/configuration"
8
8
 
@@ -0,0 +1,18 @@
1
+ module ActiveResource
2
+ class ConnectionError < StandardError # :nodoc:
3
+ attr_reader :response
4
+
5
+ def initialize(response, message = nil)
6
+ @response = response
7
+ @message = message
8
+ end
9
+
10
+ def to_s
11
+ message = "Failed.".dup
12
+ message << " Response code = #{response.code}." if response.respond_to?(:code)
13
+ message << " Response message = #{response.message}." if response.respond_to?(:message)
14
+ message << " Response body = #{response.body}." if response.respond_to?(:body)
15
+ message
16
+ end
17
+ end
18
+ end
@@ -5,11 +5,15 @@ loader.inflector.inflect(
5
5
  "janio_api" => "JanioAPI"
6
6
  )
7
7
  loader.collapse("./lib/janio_api/resources")
8
+ loader.ignore("#{__dir__}/dev_config.rb")
8
9
  loader.enable_reloading
9
- loader.tag = "janio_api_gem"
10
10
  # loader.log!
11
11
  loader.setup
12
+
12
13
  $__janio_api_loader__ = loader
14
+
13
15
  def reload!
14
16
  $__janio_api_loader__.reload
17
+ set_config
18
+ true
15
19
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: janio_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Chong
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-14 00:00:00.000000000 Z
11
+ date: 2020-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource
@@ -99,6 +99,7 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - janio_api.gemspec
102
+ - lib/dev_config.rb
102
103
  - lib/janio_api.rb
103
104
  - lib/janio_api/configuration.rb
104
105
  - lib/janio_api/connection.rb
@@ -107,6 +108,7 @@ files:
107
108
  - lib/janio_api/resources/item.rb
108
109
  - lib/janio_api/resources/order.rb
109
110
  - lib/janio_api/version.rb
111
+ - lib/patch_exception.rb
110
112
  - lib/zeitwerk_loader.rb
111
113
  homepage: https://github.com/PostCo/janio_api
112
114
  licenses: