Module: PacTrac::Carrier::Ems
- Defined in:
- lib/pactrac/carrier/ems.rb
Class Method Summary collapse
- .parse_tracking_data(response) ⇒ Object
- .start_session ⇒ Object
- .title ⇒ Object
- .tracking_number_relevant?(tracking_number) ⇒ Boolean
- .tracking_request(tracking_number, session) ⇒ Object
- .user_agent ⇒ Object
- .verify(tracking_number, verification, session) ⇒ Object
Class Method Details
.parse_tracking_data(response) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pactrac/carrier/ems.rb', line 58 def parse_tracking_data(response) doc = Nokogiri::HTML(response.raw.body) table = doc.css('.mailnum_result_box table') if table.nil? return Err.new(false, 'unable to find tracking data table') end tracking_data = { :updates => [] } table.css('tr').each do |row| cells = row.css('td') next if cells.nil? or cells.length < 2 tracking_data[:updates] << { :at => DateTime.parse(cells[0].content.to_s), :location => cells[1].content.to_s.strip, :message => cells[2].content.to_s.strip, } end return Err.new(true), tracking_data end |
.start_session ⇒ Object
77 78 79 |
# File 'lib/pactrac/carrier/ems.rb', line 77 def start_session Http::Session.start('www.ems.com.cn', 80) end |
.title ⇒ Object
16 17 18 |
# File 'lib/pactrac/carrier/ems.rb', line 16 def title 'EMS' end |
.tracking_number_relevant?(tracking_number) ⇒ Boolean
20 21 22 |
# File 'lib/pactrac/carrier/ems.rb', line 20 def tracking_number_relevant?(tracking_number) tracking_number.strip.match(/^EE\d+CN$/) end |
.tracking_request(tracking_number, session) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pactrac/carrier/ems.rb', line 24 def tracking_request(tracking_number, session) err, raw = Http.request(Net::HTTP::Get.new('/english.html', 'User-Agent' => user_agent), session) return err unless err.valid # Get validation image err, raw = Http.request(Net::HTTP::Get.new('/ems/rand', 'User-Agent' => user_agent), session) unless err.valid return Err.new(false, "there was a problem connecting to the EMS server, #{err.msg}") end f = Verify::File.create('ems.jpg', raw.body) return Err.new(true), Response.new(raw, true, "file://#{f}") end |
.user_agent ⇒ Object
81 82 83 84 |
# File 'lib/pactrac/carrier/ems.rb', line 81 def user_agent 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like' + ' Gecko) Chrome/21.0.1180.75 Safari/537.1' end |
.verify(tracking_number, verification, session) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pactrac/carrier/ems.rb', line 39 def verify(tracking_number, verification, session) req = Net::HTTP::Post.new('/ems/order/singleQuery_e', 'User-Agent' => user_agent) req.set_form_data({ :mailNum => tracking_number, :checkCode => verification }) if session. req['Cookie'] = Http::Cookie.to_request_header_value(session.) end err, raw = Http.request(req, session) unless err.valid return Err.new(false, "there was a problem connecting to the EMS server, #{err.msg}") end if raw.body.match(/failure/i) return Err.new(false, 'failure to verify using given code') end return Err.new(true), Response.new(raw, false) end |