Module: OCTranspo::MobileRouteSchedule

Extended by:
MobileRouteSchedule
Includes:
MobileResourceMethods
Included in:
MobileRouteSchedule
Defined in:
lib/octranspo/mobile_route_schedule.rb

Instance Method Summary collapse

Methods included from MobileResourceMethods

#date_stamp, #site

Methods included from RemoteResourceMethods

#cache, #cache_path, #cache_path_for, #create_fixture, #fixture_path_for, #fixtures_path, #logger, #read, #read_from_cache, #read_from_fixture, #read_from_remote, #reset!, #site, #use_fixtures!

Instance Method Details

#convert_options(options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/octranspo/mobile_route_schedule.rb', line 105

def convert_options (options={})
  if options[:direction]
    route_data = MobileRouteData.find(options[:route], :date => options[:date])
    direction_data = route_data.detect do |dataset|
      dataset[:direction] === options[:direction]
    end
    if direction_data
      options[:direction_index] = direction_data[:direction_index]
    end
  end
  return options
end

#decode(source_code, date) ⇒ Object



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
# File 'lib/octranspo/mobile_route_schedule.rb', line 56

def decode (source_code, date)
  document = interpret(source_code)

  route_and_direction_text = document.at("#SchedulesTimesSelectedTable").search(:tr)[2].search(:td)[1].inner_text
  location_text            = document.at("#SchedulesTimesListTable td.header").inner_text
  departure_texts          = Array(document.search('#SchedulesTimesListTable td'))[1..-1].map do |cell| cell.inner_text.strip end
  
  schedule = {:date => date, :legend => {}}
  schedule.merge! extract_route_and_direction_from(route_and_direction_text)
  schedule[:destinations] = Hash.new(schedule[:direction])
  
  Array(document.search("#SchedulesTimesNotesTable td.notes")).in_groups_of(2) do |flag_cell, value_cell|
    flag = flag_cell.inner_text.sub('[', '').sub(']', '').strip
    text = value_cell.inner_text.strip
    if text.include?("Destination")
      schedule[:destinations][flag] = text.sub("Destination ", "")
    else
      schedule[:legend][flag] = text
    end
  end
  
  schedule[:location] = extract_location_from(location_text)
  schedule[:departures] = departure_texts.inject([]) do |departures, text|
    departures << extract_departure_from(text, schedule)
  end
  
  schedule[:headsigns] = schedule[:departures].map do |d| d[:destination] end.uniq
  
  return schedule
end

#default_optionsObject



87
88
89
# File 'lib/octranspo/mobile_route_schedule.rb', line 87

def default_options
  {:direction_index => 1, :location_index => 0, :date => Date.current}
end

#find(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/octranspo/mobile_route_schedule.rb', line 38

def find (options={})
  options[:date] = if options[:date]
    options[:date].to_date          
  else
    Date.current
  end
  decode(read(options), options[:date])        
end

#find_one(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/octranspo/mobile_route_schedule.rb', line 47

def find_one (options={})
  options[:date] = if options[:date]
    options[:date].to_date          
  else
    Date.current
  end
  decode(read(options), options[:date])
end

#interpret(source_code) ⇒ Object

Return a Hpricot DOM representing source_code.



34
35
36
# File 'lib/octranspo/mobile_route_schedule.rb', line 34

def interpret (source_code)
  Hpricot(convert_to_utf_8(source_code))
end

#path_for(options = {}) ⇒ Object



100
101
102
103
# File 'lib/octranspo/mobile_route_schedule.rb', line 100

def path_for (options={})
  options = default_options.merge(convert_options(options))
  "#{date_stamp(options[:date])}/route_#{options[:route]}_direction_#{options[:direction_index]}_location_#{options[:location_index]}.html"
end

#url_for(options = {}) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/octranspo/mobile_route_schedule.rb', line 91

def url_for (options={})
  options = default_options.merge(convert_options(options))
  url = "#{site}/jnot/post.stoplist.schedules.oci?"
  url << "rangeIndex=5&day=#{date_stamp(options[:date])}"
  url << "&route=#{options[:route]}"
  url << "&direction=Direction#{options[:direction_index]}"
  url << "&check#{options[:location_index]}=on"
end