dji 0.1.1 → 0.2.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
  SHA1:
3
- metadata.gz: 99c613192536d1beb1acc577ae701ec56b25dfaf
4
- data.tar.gz: df2972a66397e7d063fb3b69b77a0d8a387029c2
3
+ metadata.gz: 2c537b14753592e862427e2acff55711db4a3493
4
+ data.tar.gz: 3da898f5726aa3372edcb7f73d2593ed461b0f60
5
5
  SHA512:
6
- metadata.gz: 0d14ba56c39806c116901a2015b9b1ec3ee36629609dae3ef0cd443b46309f9790bdea2b8fad4cc9c52ef931755263e8cd46079b98a56b59068b3aa7dc42646d
7
- data.tar.gz: fbe12e7d0bc3c125b81541173e0ed680c9bbbcc02262db078c17d04b43dfaa383c8006615d3cdd4b0d91b9be23e327b2beceb7d9e1279bb70c248492c28be6b6
6
+ metadata.gz: ffe1d34948ae6d308009980cb6eda2070aaa80fcae158c74785368bf158b91e37ba4cc461c1d7a9714ae43fa8bfc689cf8c1edf34ad73f6f3f074f8a5c7ce14d
7
+ data.tar.gz: dd535a177f5cb221fcba32bcad0f29c0a5fdae547c7281e36dcd9a8fa49b15bb9216a401a42028e2a6964468f112cc301b213d2f1b098645ba86b895dc0e394f
data/README.md CHANGED
@@ -29,6 +29,31 @@ Use your order number in place of ORDER_NUMBER and the last 4 digits of your pho
29
29
  Shipping Company : Tba
30
30
  Tracking Number :
31
31
 
32
+ If you want this to repeat automatically at an interval, specify the option for repeat (either -r or --repeat) with the number of seconds. Do not use this nefariously, I suggest a reasonable interval such as 60 seconds, but more useful is probably around 300 seconds (5 minutes) to 600 seconds (10 minutes).
33
+
34
+ $ dji track -o 123456789012 -p 1234 -r 60
35
+
36
+ Requesting order tracking details every 60 seconds. Press CONTROL-C to stop...
37
+
38
+ ORDER TRACKING AS OF 2016-10-27 01:29:36 -0700
39
+ ------------------------------------------------------
40
+ Order Number : 123456789012
41
+ Total : USD $1,398.00
42
+ Payment Status : Pay Confirmed
43
+ Shipping Status : Pending
44
+ Shipping Company : Tba
45
+ Tracking Number :
46
+
47
+
48
+ ORDER TRACKING AS OF 2016-10-27 01:30:37 -0700
49
+ ------------------------------------------------------
50
+ Order Number : 123456789012
51
+ Total : USD $1,398.00
52
+ Payment Status : Pay Confirmed
53
+ Shipping Status : Pending
54
+ Shipping Company : Tba
55
+ Tracking Number :
56
+
32
57
  ## Development
33
58
 
34
59
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,3 +1,4 @@
1
1
  require 'dji/command'
2
2
  require 'dji/commands/version/version_command'
3
+ require 'dji/commands/fedex/fedex_command'
3
4
  require 'dji/commands/track/track_command'
@@ -0,0 +1,26 @@
1
+ module DJI
2
+ module Command
3
+ class FedexCommand < Base
4
+ desc 'fedex COUNTRY_CODE POSTAL_CODE', 'track FedEx by reference'
5
+ option :country_code, required: true, aliases: :c
6
+ option :postal_code, required: true, aliases: :p
7
+ option :reference, aliases: :ref, default: 'DJIGOODS'
8
+ option :repeat, aliases: :r
9
+
10
+ def fedex
11
+ if options[:repeat].present?
12
+ interval = options[:repeat].to_i.presence || 300
13
+ puts
14
+ puts "Requesting FedEx tracking by reference #{reference} every #{interval} seconds. Press CONTROL-C to stop..."
15
+
16
+ while true
17
+ DJI::Fedex.track_by_reference(options[:country_code], options[:postal_code], options[:reference])
18
+ sleep(interval)
19
+ end
20
+ else
21
+ DJI::Fedex.track_by_reference(options[:country_code], options[:postal_code], options[:reference])
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,11 +1,14 @@
1
+ require 'json'
1
2
  require 'net/http'
2
3
  require 'uri'
3
4
 
4
5
  require 'nokogiri'
5
6
 
6
- module DJI
7
- class Fedex
7
+ require 'dji/fedex/track_packages_response'
8
8
 
9
+ module DJI
10
+ module Fedex
11
+
9
12
  class << self
10
13
 
11
14
  # Get the tracking details for an order
@@ -26,9 +29,10 @@ module DJI
26
29
  'Referer' => tracking_url
27
30
  }
28
31
 
29
- http = Net::HTTP.new uri.host, uri.port
30
- request = Net::HTTP.get(uri, headers)
31
- res = http.request(request)
32
+ http = Net::HTTP.new(uri.host, uri.port)
33
+ http.use_ssl = true
34
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
35
+ res = http.get(uri.request_uri, headers)
32
36
 
33
37
  case res
34
38
  when Net::HTTPSuccess, Net::HTTPRedirection
@@ -58,6 +62,113 @@ module DJI
58
62
  'https://www.fedex.com/apps/fedextrack/'
59
63
  end
60
64
 
65
+ def track_by_reference(country_code, postal_code, reference="DJIGOODS")
66
+ uri = URI.parse(tracking_url_json)
67
+
68
+ params = {
69
+ 'action' => 'altReferenceList',
70
+ 'locale' => 'en_US',
71
+ 'version' => 1,
72
+ 'format' => 'json'
73
+ }
74
+ params['data'] = {
75
+ 'TrackPackagesRequest' => {
76
+ 'appType' => 'WTRK',
77
+ 'appDeviceType' => 'DESKTOP',
78
+ 'uniqueKey' => '',
79
+ 'processingParameters' => {},
80
+ 'trackingInfoList' => [
81
+ {
82
+ 'referenceInfo' => {
83
+ 'referenceValueList' => [reference],
84
+ 'shipDate' => Time.now.strftime('%Y-%m-%d'),
85
+ 'postalCode' => postal_code,
86
+ 'countryCode' => country_code,
87
+ 'accountNbr' => ''
88
+ }
89
+ }
90
+ ]
91
+ }
92
+ }.to_json
93
+
94
+ headers = {
95
+ 'Content-Type' => 'application/x-www-form-urlencoded',
96
+ 'Origin' => 'https://www.fedex.com/',
97
+ 'Referer' => tracking_url,
98
+ 'Accept' => '*/*',
99
+ 'X-Requested-With' => 'XMLHttpRequest'
100
+ }
101
+
102
+ http = Net::HTTP.new uri.host, uri.port
103
+ http.use_ssl = true
104
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
105
+ request = Net::HTTP::Post.new uri.path, headers
106
+ request.set_form_data params
107
+ res = http.request(request)
108
+
109
+ case res
110
+ when Net::HTTPSuccess, Net::HTTPRedirection
111
+ # OK
112
+ # puts res.body
113
+ response = JSON.parse(res.body)['TrackPackagesResponse']
114
+ tpr = DJI::Fedex::TrackPackagesResponse.new_from_response(response)
115
+
116
+ data = { country_code: country_code, postal_code: postal_code }
117
+ if tpr.present?
118
+ data[:track_packages_response] = tpr
119
+ print_track_packages_response(data)
120
+ else
121
+ puts response
122
+ puts "Nothing parsed!"
123
+ end
124
+ # page = Nokogiri::HTML(res.body)
125
+ # content = page.at_xpath('//div[@id="main"]/div[@class="container"]/div[@class="row"]/div[@class="col-xs-9"]/div[@class="col-xs-10 well"][2]')
126
+ # # puts content
127
+ # data = {}
128
+ # data[:order_number] = content.at_xpath('div[1]').text.split(' ')[-1]
129
+ # data[:total] = content.at_xpath('div[2]').text.split(' ')[1..-1].join(' ')
130
+ # data[:payment_status] = content.at_xpath('div[3]').text.split(': ')[1]
131
+ # data[:shipping_status] = content.at_xpath('div[4]').text.split(': ')[1]
132
+ # data[:shipping_company] = content.at_xpath('div[5]/span').text
133
+ # data[:tracking_number] = content.at_xpath('div[6]/a').text
134
+
135
+ # print_tracking_details(data)
136
+ else
137
+ puts res.inspect
138
+ res.error!
139
+ end
140
+
141
+ end
142
+
143
+ def tracking_url_json
144
+ 'https://www.fedex.com/trackingCal/track'
145
+ end
146
+
147
+ def print_track_packages_response(data)
148
+ now = Time.now.to_s
149
+
150
+ puts
151
+ puts "FedEx Packages for Country #{data[:country_code]}, Postal Code #{data[:postal_code]} as of #{now}"
152
+ puts "-------------------------------------------------------------------------------------------------------"
153
+ puts
154
+
155
+ data[:track_packages_response].packages.each_with_index do |package, index|
156
+ puts "PACKAGE #{index+1}"
157
+ puts
158
+ puts "Origin : #{package.origin}"
159
+ puts "Destination : #{package.destination}"
160
+ puts "Tendered : #{package.tendered_date}"
161
+ puts "Picked Up : #{package.pickup_date}"
162
+ puts "Shipped : #{package.ship_date}"
163
+ puts "Est. Deliver : #{package.estimated_delivery_date}" if package.estimated_delivery_date.present?
164
+ puts "Delivered : #{package.delivery_date}" if package.delivery_date.present?
165
+ puts "Dimensions : #{package.dimensions}"
166
+ puts "Total Weight : #{package.total_weight[:pounds]} lbs (#{package.total_weight[:kilograms]} kgs)"
167
+ puts "Status : #{package.key_status}"
168
+ puts
169
+ end
170
+ end
171
+
61
172
  def print_tracking_details(data)
62
173
  now = Time.now.to_s
63
174
 
@@ -0,0 +1,91 @@
1
+ require 'time'
2
+
3
+ module DJI
4
+ module Fedex
5
+ class Package
6
+ attr_accessor :tracking_number, :pretty_tracking_number
7
+ attr_accessor :tracking_qualifier, :tracking_carrier_code, :tracking_carrier_description
8
+ attr_accessor :shipper_company_name, :shipper_name, :shipper_address, :shipper_address_line_2,
9
+ :shipper_city, :shipper_region_code, :shipper_postal_code, :shipper_country_code,
10
+ :shipper_phone_number, :shipped_by
11
+ attr_accessor :recipient_company_name, :recipient_name, :recipient_address, :recipient_address_line_2,
12
+ :recipient_city, :recipient_region_code, :recipient_postal_code, :recipient_country_code,
13
+ :recipient_phone_number, :shipped_to
14
+ attr_accessor :key_status, :key_status_code, :last_scan_status, :last_scan_date_time
15
+ attr_accessor :received_by, :sub_status, :main_status
16
+ attr_accessor :tendered_date, :pickup_date, :ship_date, :estimated_delivery_date, :delivery_date
17
+ attr_accessor :dimensions, :total_weight
18
+ attr_accessor :scan_events
19
+
20
+ def initialize
21
+ @scan_events = []
22
+ end
23
+
24
+ def destination
25
+ [recipient_company_name, recipient_name, recipient_address, recipient_address_line_2, recipient_city,
26
+ recipient_region_code, recipient_country_code].reject(&:blank?).join(', ')
27
+ end
28
+
29
+ def origin
30
+ [shipper_company_name, shipper_name, shipper_address, shipper_address_line_2, shipper_city,
31
+ shipper_region_code, shipper_country_code].reject(&:blank?).join(', ')
32
+ end
33
+
34
+ class << self
35
+
36
+ def new_from_response_package_item(item)
37
+ package = Package.new
38
+ package.tracking_number = item['trackingNbr']
39
+ package.pretty_tracking_number = item['displayTrackingNbr']
40
+ package.tracking_qualifier = item['trackingQualifier']
41
+ package.tracking_carrier_code = item['trackingCarrierCd']
42
+ package.tracking_carrier_description = item['trackingCarrierDesc']
43
+
44
+ package.shipper_company_name = item['shipperCmpnyName']
45
+ package.shipper_name = item['shipperName']
46
+ package.shipper_address = item['shipperAddr1']
47
+ package.shipper_address_line_2 = item['shipperAddr2']
48
+ package.shipper_city = item['shipperCity']
49
+ package.shipper_region_code = item['shipperStateCD']
50
+ package.shipper_postal_code = item['shipperZip']
51
+ package.shipper_country_code = item['shipperCntryCD']
52
+ package.shipper_phone_number = item['shipperPhoneNbr']
53
+ package.shipped_by = item['shippedBy']
54
+
55
+ package.recipient_company_name = item['recipientCmpnyName']
56
+ package.recipient_name = item['recipientName']
57
+ package.recipient_address = item['recipientAddr1']
58
+ package.recipient_address_line_2 = item['recipientAddr2']
59
+ package.recipient_city = item['recipientCity']
60
+ package.recipient_region_code = item['recipientStateCD']
61
+ package.recipient_postal_code = item['recipientZip']
62
+ package.recipient_country_code = item['recipientCntryCD']
63
+ package.recipient_phone_number = item['recipientPhoneNbr']
64
+ package.shipped_to = item['shippedTo']
65
+
66
+ package.key_status = item['keyStatus']
67
+ package.key_status_code = item['keyStatusCD']
68
+ package.last_scan_status = item['lastScanStatus']
69
+ package.last_scan_date_time = item['lastScanDateTime']
70
+
71
+ package.received_by = item['receivedByNm']
72
+ package.sub_status = item['subStatus']
73
+ package.main_status = item['mainStatus']
74
+
75
+ package.tendered_date = Time.iso8601(item['tenderedDt']) if item['tenderedDt'].present?
76
+ package.pickup_date = Time.iso8601(item['pickupDt']) if item['pickupDt'].present?
77
+ package.ship_date = Time.iso8601(item['shipDt']) if item['shipDt'].present?
78
+ package.estimated_delivery_date = Time.iso8601(item['estDeliveryDt']) if item['estDeliveryDt'].present?
79
+ package.delivery_date = Time.iso8601(item['actDeliveryDt']) if item['actDeliveryDt'].present?
80
+
81
+ package.dimensions = item['dimensions']
82
+ package.total_weight = { pounds: item['totalLbsWgt'], kilograms: item['totalKgsWgt'] }
83
+
84
+ package
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,8 @@
1
+
2
+ module DJI
3
+ module Fedex
4
+ class ScanEvent
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,30 @@
1
+ require 'dji/fedex/package'
2
+ require 'dji/fedex/scan_event'
3
+
4
+ module DJI
5
+ module Fedex
6
+ class TrackPackagesResponse
7
+ attr_accessor :packages
8
+
9
+ def initialize
10
+ @packages = []
11
+ end
12
+
13
+ class << self
14
+ def new_from_response(response)
15
+ return nil if !response.has_key?('successful') || response['successful'].blank?
16
+
17
+ tpr = TrackPackagesResponse.new
18
+
19
+ response['packageList'].each do |item|
20
+ package = Package.new_from_response_package_item(item)
21
+ tpr.packages << package if package.present?
22
+ end
23
+
24
+ tpr
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module DJI
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dji
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Elliott
@@ -111,9 +111,13 @@ files:
111
111
  - lib/dji/command/base.rb
112
112
  - lib/dji/command/behavior.rb
113
113
  - lib/dji/commands.rb
114
+ - lib/dji/commands/fedex/fedex_command.rb
114
115
  - lib/dji/commands/track/track_command.rb
115
116
  - lib/dji/commands/version/version_command.rb
116
117
  - lib/dji/fedex.rb
118
+ - lib/dji/fedex/package.rb
119
+ - lib/dji/fedex/scan_event.rb
120
+ - lib/dji/fedex/track_packages_response.rb
117
121
  - lib/dji/order_tracking.rb
118
122
  - lib/dji/version.rb
119
123
  homepage: http://github.com/kevinelliott/dji