eshipper 0.1.2 → 0.1.3

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: 57b78cb7082d86f9669043760716042e72cfceff28c5480ce82da89417a21f6f
4
- data.tar.gz: d268d104958c610d2d3f3fc86e97272514389fb79904b22e2247f9686c78d590
3
+ metadata.gz: ebd04fae554c9b5537c9d898c5ae5afdfa66e40937b7f03ef5f4f698a358dfdd
4
+ data.tar.gz: b310e67019d3e95e5dbeb6867d19d55e6ca46c4b278a2f5b9da12277778622dd
5
5
  SHA512:
6
- metadata.gz: fba5257151a5f55e7f973950a8e6b987461ffe08d737993bf2366cecbc25f90eb6d3f4c98c07d3ac8c9de8814aa1f724302def293a205190d6236298563ac742
7
- data.tar.gz: 7654f83d0b8e3ace3ebbf379748e6f046420beb003979bd5e7b6093789229c74a05cbea633fd835b20e0b24c898f70b8a4603d3e1508b98cd2e4f8eaa025a971
6
+ metadata.gz: fe4802cebcf82acc4c6900d091ac0f32bd96c431ca394843cdf193454b05fc35be61e91894fd51b1e27f8bf90a5be3746ac7390ac778e20a60e18047b13d3779
7
+ data.tar.gz: 43f25618f1c423a5afe4fdffbeecdaf5ec32013a75a5d993391980497d989ef79244ca7dc21847fe3f132f059f3ddc18ae8b73a90381d37bcc1b265120bf6c38
@@ -3,12 +3,59 @@
3
3
  module EShipper
4
4
  class Order < EShipperResponse
5
5
  def self.search(options = {})
6
- orders = SearchReply.search(options).OrderSearchReply.Order
7
- orders.is_a?(Array) ? orders : [orders]
6
+ orders = SearchReply.search(options).OrderSearchReply
7
+
8
+ if orders == "\n" # No results
9
+ []
10
+ elsif orders.Order.is_a?(Array) # Multiple results
11
+ orders.Order
12
+ else # Single result
13
+ [orders.Order]
14
+ end
15
+ end
16
+
17
+ def references
18
+ if !respond_to?(:References)
19
+ []
20
+ elsif self.References.Reference == "\n"
21
+ []
22
+ elsif self.References.Reference.is_a?(Array)
23
+ self.References.Reference
24
+ else
25
+ [self.References.Reference]
26
+ end
8
27
  end
9
28
 
10
29
  def fetch
11
- OrderInformationReply.fetch(order_id: self.Id).OrderInformationReply
30
+ order_information_reply = OrderInformationReply.fetch(fetch_options)
31
+
32
+ if order_information_reply.respond_to?(:OrderInformationReply)
33
+ order_information_reply = order_information_reply.OrderInformationReply
34
+ if order_information_reply.Order.is_a?(Array)
35
+ # Sometimes there is a partial referenceCode match. In that case the API will return multiple Orders
36
+ # However, the way they format the Orders, the properties of all orders are siblings.
37
+ # In other words, the properties of an Order are NOT nested within the Order.
38
+ # So the way the parser works is that it gives us arrays for each property.
39
+ # TBD: Are properties of each Order ordered consistently?
40
+ # TBD: Does the API always return the better matching result first???
41
+ # Assume that the first result is the one we want!
42
+ this_index = order_information_reply.Order.each_with_index.find do |_, index|
43
+ order_information_reply.Order[index].id == self.Id
44
+ end[1]
45
+ order_information_reply.attributes.keys.each do |attribute_key|
46
+ order_information_reply.send("#{attribute_key}=", order_information_reply.attributes[attribute_key][this_index])
47
+ end
48
+ throw("id mismatch") if order_information_reply.Order.id != self.Id
49
+ end
50
+ order_information_reply
51
+ else
52
+ # TODO: error handling
53
+ throw(order_information_reply)
54
+ end
55
+ end
56
+
57
+ def fetch_options
58
+ { order_id: self.Id, referenceCode: references.first&.Code }
12
59
  end
13
60
  end
14
61
  end
@@ -2,18 +2,20 @@
2
2
 
3
3
  module EShipper
4
4
  class OrderInformationRequest < EShipper::Request
5
- attr_reader :order_id
5
+ attr_reader :order_id, :referenceCode
6
6
 
7
- def initialize(order_id:)
7
+ def initialize(order_id:, referenceCode: nil)
8
8
  @order_id = order_id
9
+ @referenceCode = referenceCode
9
10
  super()
10
11
  end
11
12
 
12
13
  def order_properties
13
14
  {
14
15
  orderId: @order_id,
16
+ referenceCode: referenceCode,
15
17
  detailed: "true",
16
- }
18
+ }.compact
17
19
  end
18
20
 
19
21
  def request_body
@@ -21,7 +21,7 @@ module EShipper
21
21
  carrierTrackingNumber: nil, # [Optional]
22
22
  referenceValue: nil # [Optional]
23
23
  )
24
- throw("Order date range should be within 3 month.") if toDate - 90 > fromDate
24
+ throw("Order date range should be within 3 month.") if fromDate + 90 > toDate
25
25
 
26
26
  @fromDate = fromDate
27
27
  @toDate = toDate
@@ -1,16 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class EShipperXMLFormatter
4
- include ActiveResource::Formats::XmlFormat
5
-
6
- def decode(xml)
7
- ActiveResource::Formats::XmlFormat.decode(xml)
8
- end
9
- end
10
-
11
3
  module EShipper
12
4
  class EShipperResponse < ::ActiveResource::Base
13
5
  self.site = EShipper::Client.instance.url
14
6
  self.format = :xml
7
+
8
+ def self.decode(xml)
9
+ # Remove raw & which are illegal
10
+ # https://stackoverflow.com/questions/17237334/illegal-character-in-raw-string-rexml-parsing
11
+ xml.gsub!(/&(?!(?:amp|lt|gt|quot|apos);)/, "&amp;")
12
+ format.decode(xml)
13
+ end
15
14
  end
16
15
  end
@@ -2,11 +2,14 @@
2
2
 
3
3
  module EShipper
4
4
  class OrderInformationReply < EShipperResponse
5
- self.format = :xml
6
-
7
5
  def self.fetch(options)
8
6
  request = EShipper::OrderInformationRequest.new(options)
9
- new(format.decode(request.send_now))
7
+ response = decode(request.send_now)
8
+ if response["ErrorReply"]
9
+ throw(ErrorReply.new(response))
10
+ else
11
+ new(response)
12
+ end
10
13
  end
11
14
  end
12
15
  end
@@ -2,11 +2,14 @@
2
2
 
3
3
  module EShipper
4
4
  class SearchReply < EShipperResponse
5
- self.format = :xml
6
-
7
5
  def self.search(options)
8
6
  request = EShipper::OrderSearchRequest.new(options)
9
- new(format.decode(request.send_now))
7
+ response = decode(request.send_now)
8
+ if response["ErrorReply"]
9
+ throw(ErrorReply.new(response))
10
+ else
11
+ new(response)
12
+ end
10
13
  end
11
14
  end
12
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EShipperRuby
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/readme.md ADDED
@@ -0,0 +1,14 @@
1
+ # How to release
2
+
3
+ 1. Bump the version in ./lib/eshipper/version.rb
4
+ 2. Build: `$ gem build eshipper.gemspec`
5
+ 3. Install: `gem install ./eshipper-X.X.X.gem`
6
+ 4. Smoke test:
7
+ ```
8
+ $ pry
9
+ pry > require 'eshipper'
10
+ pry > EShipper::Client.instance.username = "XXX"
11
+ pry > EShipper::Client.instance.password = "XXX"
12
+ pry > orders = EShipper::Order.search(carrierTrackingNumber: "XXX")
13
+ ```
14
+ 5. Push: `$ gem push eshipper-X.X.X.gem`
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eshipper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marc \U0001F41D"
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2022-08-20 00:00:00.000000000 Z
14
+ date: 2022-08-24 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activeresource
@@ -63,6 +63,7 @@ files:
63
63
  - lib/eshipper/responses/order_information_reply.rb
64
64
  - lib/eshipper/responses/search_reply.rb
65
65
  - lib/eshipper/version.rb
66
+ - readme.md
66
67
  homepage: https://github.com/hrdwdmrbl/eshipper
67
68
  licenses:
68
69
  - MIT