Class: Ratis::NextBus
- Inherits:
-
Object
- Object
- Ratis::NextBus
- Defined in:
- lib/ratis/next_bus.rb
Instance Attribute Summary collapse
-
#services ⇒ Object
Returns the value of attribute services.
-
#stop ⇒ Object
Returns the value of attribute stop.
-
#success ⇒ Object
Returns the value of attribute success.
Class Method Summary collapse
Instance Method Summary collapse
-
#create_trip(trip) ⇒ Object
TODO: turn into real classes.
-
#initialize(response) ⇒ NextBus
constructor
A new instance of NextBus.
- #parse_stop(single_stop) ⇒ Object
- #parse_trip_info(trips) ⇒ Object
- #stop_description ⇒ Object
- #success? ⇒ Boolean
-
#to_hash_for_xml ⇒ Object
Used to create an XML response for the NextBus SMS services which hits /nextride.xml?stop_id=<STOPID>.
Constructor Details
#initialize(response) ⇒ NextBus
Returns a new instance of NextBus.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ratis/next_bus.rb', line 7 def initialize(response) @success = response.success? @stop = [] @services = [] if @success @stop = response.body[:nextbus_response][:atstop] # Only Light Rail stops will return 2 stops from ATIS for a single NextRide id if @stop.is_a?(Array) @stop.each do |_stop| parse_stop(_stop) end else parse_stop(@stop) end end end |
Instance Attribute Details
#services ⇒ Object
Returns the value of attribute services.
5 6 7 |
# File 'lib/ratis/next_bus.rb', line 5 def services @services end |
#stop ⇒ Object
Returns the value of attribute stop.
5 6 7 |
# File 'lib/ratis/next_bus.rb', line 5 def stop @stop end |
#success ⇒ Object
Returns the value of attribute success.
5 6 7 |
# File 'lib/ratis/next_bus.rb', line 5 def success @success end |
Class Method Details
.where(conditions) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ratis/next_bus.rb', line 80 def self.where(conditions) stop_id = conditions.delete(:stop_id) type = conditions.delete(:type) || 'N' # N for Next Bus if datetime = conditions.delete(:datetime) raise ArgumentError.new('If datetime supplied it should be a Time or DateTime instance, otherwise it defaults to Time.now') unless datetime.is_a?(DateTime) || datetime.is_a?(Time) else datetime = Time.now end raise ArgumentError.new('You must provide a stop ID') unless stop_id Ratis.all_conditions_used?(conditions) response = Request.get 'Nextbus', {'Stopid' => stop_id, 'Date' => datetime.strftime("%m/%d/%Y"), 'Time' => datetime.strftime("%H%M"), 'Type' => type } NextBus.new(response) end |
Instance Method Details
#create_trip(trip) ⇒ Object
TODO: turn into real classes
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ratis/next_bus.rb', line 61 def create_trip(trip) OpenStruct.new(:triptime => trip[:triptime], :block => trip[:block], :tripid => trip[:tripid], :exception => trip[:exception], :skedtripid => trip[:skedtripid], :realtime => OpenStruct.new(:valid => trip[:realtime][:valid], :adherence => trip[:realtime][:adherence], :estimatedtime => trip[:realtime][:estimatedtime], :estimatedminutes => trip[:realtime][:estimatedminutes], :polltime => trip[:realtime][:polltime], :trend => trip[:realtime][:trend], :speed => trip[:realtime][:speed], :reliable => trip[:realtime][:reliable], :stopped => trip[:realtime][:stopped], :lat => trip[:realtime][:lat], :long => trip[:realtime][:long] )) end |
#parse_stop(single_stop) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ratis/next_bus.rb', line 28 def parse_stop(single_stop) _services = single_stop.delete(:service) _services = [_services] unless _services.is_a?(Array) @services << _services.map do |service| OpenStruct.new(:status => service[:status], :sign => service[:sign], :routetype => service[:routetype], :times => service[:times], :direction => service[:direction], :servicetype => service[:servicetype], :route => service[:route], :operator => service[:operator], :trips => parse_trip_info(service[:tripinfo]) ) end @services.flatten! end |
#parse_trip_info(trips) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ratis/next_bus.rb', line 49 def parse_trip_info(trips) # can come back as an Array or single Hash... if trips.is_a?(Array) trips.map do |ti| create_trip(ti) end else # Hash [create_trip(trips)] end end |
#stop_description ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/ratis/next_bus.rb', line 106 def stop_description if @stop.is_a?(Array) @stop.first[:description] else @stop[:description] end end |
#success? ⇒ Boolean
102 103 104 |
# File 'lib/ratis/next_bus.rb', line 102 def success? @success end |
#to_hash_for_xml ⇒ Object
Used to create an XML response for the NextBus SMS services which hits /nextride.xml?stop_id=<STOPID>
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ratis/next_bus.rb', line 117 def to_hash_for_xml { :stopname => stop_description, :runs => @services.map do |service| service.trips.map do |trip| { :time => trip.realtime.estimatedtime, :sign => service.sign, :adherence => trip.realtime.adherence, :route => service.route } end end.flatten } end |