Module: DJI::Fedex

Defined in:
lib/dji/fedex.rb,
lib/dji/fedex/package.rb,
lib/dji/fedex/scan_event.rb,
lib/dji/fedex/track_packages_response.rb

Defined Under Namespace

Classes: Package, ScanEvent, TrackPackagesResponse

Class Method Summary collapse

Class Method Details



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dji/fedex.rb', line 147

def print_track_packages_response(data)
  now = Time.now.to_s

  puts
  puts "FedEx Packages for Country #{data[:country_code]}, Postal Code #{data[:postal_code]} as of #{now}"
  puts "-------------------------------------------------------------------------------------------------------"
  puts

  data[:track_packages_response].packages.each_with_index do |package, index|
    puts "PACKAGE #{index+1}"
    puts
    puts "Origin       : #{package.origin}"
    puts "Destination  : #{package.destination}"
    puts "Tendered     : #{package.tendered_date}"
    puts "Picked Up    : #{package.pickup_date}"
    puts "Shipped      : #{package.ship_date}"
    puts "Est. Deliver : #{package.estimated_delivery_date}" if package.estimated_delivery_date.present?
    puts "Delivered    : #{package.delivery_date}" if package.delivery_date.present?
    puts "Dimensions   : #{package.dimensions}"
    puts "Total Weight : #{package.total_weight[:pounds]} lbs (#{package.total_weight[:kilograms]} kgs)"
    puts "Status       : #{package.key_status}"
    puts
  end
end


172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dji/fedex.rb', line 172

def print_tracking_details(data)
    now = Time.now.to_s

    puts
    puts "ORDER TRACKING AS OF #{now}"
    puts "------------------------------------------------------"
    puts "Order Number     : #{data[:order_number]}"
    puts "Total            : #{data[:total]}"
    puts "Payment Status   : #{data[:payment_status]}"
    puts "Shipping Status  : #{data[:shipping_status]}"
    puts "Shipping Company : #{data[:shipping_company]}"
    puts "Tracking Number  : #{data[:tracking_number]}"
    puts
end

.track_by_reference(country_code, postal_code, reference = "DJIGOODS") ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dji/fedex.rb', line 65

def track_by_reference(country_code, postal_code, reference="DJIGOODS")
  uri = URI.parse(tracking_url_json)

  params = {
    'action' => 'altReferenceList',
    'locale' => 'en_US',
    'version' => 1,
    'format' => 'json'
  }
  params['data'] = {
    'TrackPackagesRequest' => {
      'appType' => 'WTRK',
      'appDeviceType' => 'DESKTOP',
      'uniqueKey' => '',
      'processingParameters' => {},
      'trackingInfoList' => [
        {
          'referenceInfo' => {
            'referenceValueList' => [reference],
            'shipDate' => Time.now.strftime('%Y-%m-%d'),
            'postalCode' => postal_code,
            'countryCode' => country_code,
            'accountNbr' => ''
          }
        }
      ]
    }
  }.to_json

  headers = {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Origin' => 'https://www.fedex.com/',
    'Referer' => tracking_url,
    'Accept' => '*/*',
    'X-Requested-With' => 'XMLHttpRequest'
  }

  http = Net::HTTP.new uri.host, uri.port
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new uri.path, headers
  request.set_form_data params
  res = http.request(request)

  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    # OK
    # puts res.body
    response = JSON.parse(res.body)['TrackPackagesResponse']
    tpr = DJI::Fedex::TrackPackagesResponse.new_from_response(response)

    data = { country_code: country_code, postal_code: postal_code }
    if tpr.present?
      data[:track_packages_response] = tpr
      print_track_packages_response(data)
    else
      puts response
      puts "Nothing parsed!"
    end
    # page = Nokogiri::HTML(res.body)
    # 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]')
    # # puts content
    # data = {}
    # data[:order_number] = content.at_xpath('div[1]').text.split(' ')[-1]
    # data[:total]        = content.at_xpath('div[2]').text.split(' ')[1..-1].join(' ')
    # data[:payment_status] = content.at_xpath('div[3]').text.split(': ')[1]
    # data[:shipping_status] = content.at_xpath('div[4]').text.split(': ')[1]
    # data[:shipping_company] = content.at_xpath('div[5]/span').text
    # data[:tracking_number] = content.at_xpath('div[6]/a').text
    
    # print_tracking_details(data)
  else
    puts res.inspect
    res.error!
  end

end

.tracking_details(country, postal_code) ⇒ Object

Get the tracking details for an order



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dji/fedex.rb', line 15

def tracking_details(country, postal_code)
  uri = URI.parse(tracking_url)
  params = {
    'action' => 'altref',
    'trackingnumber' => 'DJI GOODS',
    'cntry_code' => country,
    'shipdate' => Time.now.strftime('%Y-%m-%d'),
    'dest_postal' => postal_code
  }
  uri.query = URI.encode_www_form(params)

  headers = {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Origin' => 'https://www.fedex.com/',
    'Referer' => tracking_url
  }

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  res = http.get(uri.request_uri, headers)

  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    puts res.body
    # OK
    # page = Nokogiri::HTML(res.body)
    # 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]')
    # # puts content
    # data = {}
    # data[:order_number] = content.at_xpath('div[1]').text.split(' ')[-1]
    # data[:total]        = content.at_xpath('div[2]').text.split(' ')[1..-1].join(' ')
    # data[:payment_status] = content.at_xpath('div[3]').text.split(': ')[1]
    # data[:shipping_status] = content.at_xpath('div[4]').text.split(': ')[1]
    # data[:shipping_company] = content.at_xpath('div[5]/span').text
    # data[:tracking_number] = content.at_xpath('div[6]/a').text
    
    # print_tracking_details(data)
  else
    puts res.inspect
    res.error!
  end

end

.tracking_urlObject

The URL for order tracking



61
62
63
# File 'lib/dji/fedex.rb', line 61

def tracking_url
  'https://www.fedex.com/apps/fedextrack/'
end

.tracking_url_jsonObject



143
144
145
# File 'lib/dji/fedex.rb', line 143

def tracking_url_json
  'https://www.fedex.com/trackingCal/track'
end