Class: DJI::Fedex

Inherits:
Object
  • Object
show all
Defined in:
lib/dji/fedex.rb

Class Method Summary collapse

Class Method Details



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dji/fedex.rb', line 61

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

.tracking_details(country, postal_code) ⇒ Object

Get the tracking details for an order



12
13
14
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
# File 'lib/dji/fedex.rb', line 12

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
  request = Net::HTTP.get(uri, headers)
  res = http.request(request)

  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



57
58
59
# File 'lib/dji/fedex.rb', line 57

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