Class: DJI::OrderTracking
- Inherits:
-
Object
- Object
- DJI::OrderTracking
- Defined in:
- lib/dji/order_tracking.rb
Class Method Summary collapse
-
.authenticity_token(url) ⇒ Object
Retrieve an authenticity_token.
- .print_tracking_details(data) ⇒ Object
-
.tracking_details(order_number, phone) ⇒ Object
Get the tracking details for an order.
-
.tracking_url ⇒ Object
The URL for order tracking.
Class Method Details
.authenticity_token(url) ⇒ Object
Retrieve an authenticity_token
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dji/order_tracking.rb', line 12 def authenticity_token(url) uri = URI(url) http = Net::HTTP.new uri.host, uri.port request = Net::HTTP::Get.new uri.path response = http.request request page = Nokogiri::HTML(response.body) token = page.at_xpath('//meta[@name="csrf-token"]/@content').text = response['set-cookie'] { param: 'authenticity_token', token: token, cookie: } end |
.print_tracking_details(data) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dji/order_tracking.rb', line 72 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(order_number, phone) ⇒ Object
Get the tracking details for an order
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 59 60 61 62 63 64 65 |
# File 'lib/dji/order_tracking.rb', line 26 def tracking_details(order_number, phone) auth_token = authenticity_token(tracking_url) url = URI.parse(tracking_url) params = { 'number' => order_number, 'phone_tail' => phone, 'utf8' => '✓' } params[auth_token[:param]] = auth_token[:token] headers = { 'Content-Type' => 'application/x-www-form-urlencoded', 'Cookie' => auth_token[:cookie], 'Origin' => 'http://store.dji.com', 'Referer' => tracking_url } http = Net::HTTP.new url.host, url.port request = Net::HTTP::Post.new url.path, headers request.set_form_data params res = http.request(request) case res when Net::HTTPSuccess, Net::HTTPRedirection # 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_url ⇒ Object
The URL for order tracking
68 69 70 |
# File 'lib/dji/order_tracking.rb', line 68 def tracking_url 'http://store.dji.com/orders/tracking' end |