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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dji/order_tracking.rb', line 13 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 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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dji/order_tracking.rb', line 110 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
129 130 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 |
# File 'lib/dji/order_tracking.rb', line 129 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.message}" end end |
.publish_url ⇒ Object
169 170 171 172 |
# File 'lib/dji/order_tracking.rb', line 169 def publish_url # 'http://localhost:3000/orders' 'http://dji-track.herokuapp.com/orders' end |
.tracking_details(options = {}) ⇒ Object
Get the tracking details for an order
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 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 |
# File 'lib/dji/order_tracking.rb', line 35 def tracking_details( = {}) auth_token = authenticity_token(tracking_url, ) url = URI.parse(tracking_url) params = { 'number' => [:order_number], 'phone_tail' => [:phone_tail], '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 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="main"]/div[@class="container"]/div[@class="row"]/div[@class="col-xs-9"]/div[@class="col-xs-10 well"][2]') if content.text.blank? || content.text.include?('Sorry, record not found.') puts "Order #{options[:order_number]}/#{options[:phone_tail]} not found!" if [:debug] else 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] = if content.at_xpath('div[5]/span').present? content.at_xpath('div[5]/span').text else 'Tba' end data[:tracking_number] = if content.at_xpath('div[6]/a').present? content.at_xpath('div[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 [:debug] end data else puts "There was an error: #{response.message}" nil end data end |
.tracking_url ⇒ Object
The URL for order tracking
106 107 108 |
# File 'lib/dji/order_tracking.rb', line 106 def tracking_url 'http://store.dji.com/orders/tracking' end |