Class: DJI::OrderTracking
- Inherits:
-
Object
- Object
- DJI::OrderTracking
- Defined in:
- lib/dji/order_tracking.rb
Class Method Summary collapse
-
.authenticity_token(url, options = {}) ⇒ Object
Retrieve an authenticity_token.
- .print_tracking_details(data) ⇒ Object
- .publish(data) ⇒ Object
- .publish_url ⇒ Object
-
.tracking_details(options = {}) ⇒ Object
Get the tracking details for an order.
-
.tracking_url ⇒ Object
The URL for order tracking.
Class Method Details
.authenticity_token(url, options = {}) ⇒ Object
Retrieve an authenticity_token
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dji/order_tracking.rb', line 18 def authenticity_token(url, = {}) uri = URI(url) http = Net::HTTP.new uri.host, uri.port request = Net::HTTP::Get.new "#{uri.path}?www=v1" response = http.request request if [:debug].present? puts "DEBUG: -------------------------------------------------------------------" puts "DEBUG: Authenticity Token Body" puts "DEBUG: #{response.body}" puts "DEBUG: -------------------------------------------------------------------" end page = Nokogiri::HTML(response.body) token_dom = page.at_xpath('//meta[@name="csrf-token"]/@content') token = token_dom.text if token_dom.present? = response['set-cookie'] { param: 'authenticity_token', token: token, cookie: } end |
.print_tracking_details(data) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dji/order_tracking.rb', line 112 def print_tracking_details(data) now = Time.now.to_s puts puts "ORDER TRACKING AS OF #{now}" puts "------------------------------------------------------" puts "DJI Forum Username : #{data[:dji_username]}" if data[:dji_username].present? puts "Email Address : #{data[:email_address]}" if data[:email_address].present? puts "Order Number : #{data[:order_number]}" puts "Order Time : #{data[:order_time]}" if data[:order_time].present? puts "Total : #{data[:total]}" puts "Payment Status : #{data[:payment_status]}" puts "Country : #{data[:shipping_country]}" if data[:shipping_country].present? puts "Shipping Status : #{data[:shipping_status]}" puts "Shipping Company : #{data[:shipping_company]}" puts "Tracking Number : #{data[:tracking_number]}" puts end |
.publish(data) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/dji/order_tracking.rb', line 131 def publish(data) url = URI.parse(publish_url) params = { format: :json, order: { merchant: 'DJI', order_id: data[:order_number], order_time: data[:order_time], payment_status: data[:payment_status], payment_total: data[:total], phone_tail: data[:phone_tail], shipping_country: data[:shipping_country], shipping_status: data[:shipping_status], shipping_company: data[:shipping_company], tracking_number: data[:tracking_number], dji_username: data[:dji_username], email_address: data[:email_address], } } headers = { 'Accepts' => 'application/json', 'Content-Type' => 'application/json' } http = Net::HTTP.new url.host, url.port request = Net::HTTP::Post.new url.path, headers request.body = params.to_json response = http.request(request) case response when Net::HTTPSuccess, Net::HTTPRedirection puts "You have successfully published your latest order status." puts "See order statuses reported by others at #{publish_url}" else puts "There was an error trying to publish your order status: #{response.}" end end |
.publish_url ⇒ Object
171 172 173 174 |
# File 'lib/dji/order_tracking.rb', line 171 def publish_url # 'http://localhost:3000/orders' 'http://www.dronehome.io/dji_track/orders' end |
.tracking_details(options = {}) ⇒ Object
Get the tracking details for an order
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 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 |
# File 'lib/dji/order_tracking.rb', line 40 def tracking_details( = {}) auth_token = authenticity_token(tracking_url, ) url = URI.parse(tracking_url) params = { 'www' => 'v1', 'number' => [:order_number], 'phone_tail' => [:phone_tail] } params[auth_token[:param]] = auth_token[:token] headers = { 'Content-Type' => 'application/x-www-form-urlencoded', 'Cookie' => auth_token[:cookie], 'Origin' => 'http://m.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 response = http.request(request) if [:debug].present? puts "DEBUG: -------------------------------------------------------------------" puts "DEBUG: Tracking Page Body" puts "DEBUG: #{response.body}" puts "DEBUG: -------------------------------------------------------------------" end data = case response when Net::HTTPSuccess, Net::HTTPRedirection # OK page = Nokogiri::HTML(response.body) content = page.at_xpath('//div[@id="custom-info"]/div[@class="order-tracking"]') puts content.to_html if [:debug] if content.text.blank? || content.text.include?('Sorry, record not found.') puts "Order #{[:order_number]}/#{[:phone_tail]} not found!" if [:output] else data = {} data[:order_number] = content.at_xpath('p[1]').text.split(' ')[-1] data[:total] = content.at_xpath('p[2]').text.split(' ')[1..-1].join(' ') data[:payment_status] = content.at_xpath('p[3]').text.split(': ')[1] data[:shipping_status] = content.at_xpath('p[4]').text.split(': ')[1] data[:shipping_company] = if content.at_xpath('p[5]').present? content.at_xpath('p[5]').text.split(': ')[1].downcase else 'tba' end data[:tracking_number] = if content.at_xpath('p[6]/a').present? content.at_xpath('p[6]/a').text else nil end data[:debug] = [:debug] if [:debug].present? data[:dji_username] = [:dji_username] if [:dji_username].present? data[:email_address] = [:email_address] if [:email_address].present? data[:phone_tail] = [:phone_tail] if [:phone_tail].present? data[:order_time] = [:order_time] if [:order_time].present? data[:shipping_country] = [:country] if [:country].present? print_tracking_details(data) if [:output] end data else puts "There was an error: #{response.}" nil end data end |
.tracking_url ⇒ Object
The URL for order tracking
13 14 15 |
# File 'lib/dji/order_tracking.rb', line 13 def tracking_url 'http://m.dji.com/orders/tracking' end |