Class: CollectPlus::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/collect_plus/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracking_no) ⇒ Tracker

Returns a new instance of Tracker.



8
9
10
11
# File 'lib/collect_plus/tracker.rb', line 8

def initialize(tracking_no)
  @tracking_no = tracking_no
  track
end

Instance Attribute Details

#tracking_noObject

Returns the value of attribute tracking_no.



6
7
8
# File 'lib/collect_plus/tracker.rb', line 6

def tracking_no
  @tracking_no
end

Instance Method Details

#current_statusObject



20
21
22
23
24
25
26
27
28
# File 'lib/collect_plus/tracker.rb', line 20

def current_status
  # Parse the status info from the appropriate HTML element, if it exists
  # If not, parse the error message from the HTML and return it instead
  if status_node = @html_doc.at_css(".current_tracking_status")
    status_node.text.gsub(/\n/, " ").strip
  else
    @html_doc.at_css(".tracking_events").text.gsub(/\n/, " ").strip
  end
end

#status_historyObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/collect_plus/tracker.rb', line 30

def status_history
  # Parse the list of statuses from the appropriate HTML element, if it exists
  # If not, parse the error message from the HTML and return it instead
  if status_list = @html_doc.css(".tracking_events > li")
    status_list.map do |status|
      status.css("span").map(&:text).join(" - ")
    end
  else
    @html_doc.at_css(".tracking_events").text.gsub(/\n/, " ").strip
  end
end

#status_summaryObject



42
43
44
45
# File 'lib/collect_plus/tracker.rb', line 42

def status_summary
  summary = "CURRENT STATUS\n" << current_status << "\n\nSTATUS HISTORY\n"
  summary << status_history.join("\n\n")
end

#trackObject

Retrieves the HTML page that contains tracking info, ready for parsing



15
16
17
18
# File 'lib/collect_plus/tracker.rb', line 15

def track
  # Only need to extract the fragment of HTML that contains the tracking info
  @html_doc = Nokogiri::HTML(open("https://www.collectplus.co.uk/track/#{@tracking_no}")) { |config| config.nonet }.at_css("#left_pane")
end