Class: HrtBus::Bus
- Inherits:
-
Object
- Object
- HrtBus::Bus
- Includes:
- ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/hrt_bus/bus.rb
Constant Summary collapse
- ATTRIBUTES =
[ :id, :time, :direction, :lat, :lon, :route_id, :adherence ]
- DIRECTIONS =
{ "1" => "inbound", "2" => "outbound" }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(attrs) ⇒ Object
- #early? ⇒ Boolean
-
#initialize(attributes = {}) ⇒ Bus
constructor
A new instance of Bus.
- #late? ⇒ Boolean
- #ontime? ⇒ Boolean
- #read_attribute_for_validation(key) ⇒ Object
- #static_map ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Bus
Returns a new instance of Bus.
21 22 23 |
# File 'lib/hrt_bus/bus.rb', line 21 def initialize(attributes={}) self.attributes = attributes end |
Class Method Details
.active_buses ⇒ Object
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 |
# File 'lib/hrt_bus/bus.rb', line 63 def self.active_buses buses = [] curl = ::Curl::Easy.perform(HrtBus::Config.buses_uri) do |c| c.timeout = HrtBus::Config.timeout end unless curl.response_code == 226 raise curl.response_code end parsed = ::CSV.new(curl.body_str, { :headers => true, :skip_blanks => true }) parsed.each do |row| time, date, id, lat_lon, valid, adherence, route_id, direction = row[0], row[1], row[2], row[3], row[4], row[5], row[7], DIRECTIONS[row[8]] time = HrtBus::Parse.time(time, date) lat, lon = HrtBus::Parse.geo(lat_lon) bus = new(:time => time, :id => id, :lat => lat, :lon => lon, :adherence => adherence, :route_id => route_id, :route_id => route_id, :direction => direction) buses << bus if bus.valid? end buses end |
.all ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/hrt_bus/bus.rb', line 102 def self.all buses = [] curl = ::Curl::Easy.perform(HrtBus::Config.buses_uri) do |c| c.timeout = HrtBus::Config.timeout end unless curl.response_code == 226 raise curl.response_code end parsed = ::CSV.new(curl.body_str, { :headers => true, :skip_blanks => true }) parsed.each do |row| time, date, id, lat_lon, valid, adherence, route_id, direction = row[0], row[1], row[2], row[3], row[4], row[5], row[7], DIRECTIONS[row[8]] time = HrtBus::Parse.time(time, date) lat, lon = HrtBus::Parse.geo(lat_lon) bus = new(:time => time, :id => id, :lat => lat, :lon => lon, :adherence => adherence, :route_id => route_id, :route_id => route_id, :direction => direction) buses << bus end buses end |
Instance Method Details
#attributes ⇒ Object
25 26 27 28 29 30 |
# File 'lib/hrt_bus/bus.rb', line 25 def attributes ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key| result[key] = read_attribute_for_validation(key) result end end |
#attributes=(attrs) ⇒ Object
32 33 34 35 36 |
# File 'lib/hrt_bus/bus.rb', line 32 def attributes=(attrs) attrs.each_pair do |k, v| send("#{k}=", v) end end |
#early? ⇒ Boolean
42 43 44 |
# File 'lib/hrt_bus/bus.rb', line 42 def early? self.adherence < 0 end |
#late? ⇒ Boolean
50 51 52 |
# File 'lib/hrt_bus/bus.rb', line 50 def late? self.adherence > 0 end |
#ontime? ⇒ Boolean
46 47 48 |
# File 'lib/hrt_bus/bus.rb', line 46 def ontime? self.adherence == 0 end |
#read_attribute_for_validation(key) ⇒ Object
38 39 40 |
# File 'lib/hrt_bus/bus.rb', line 38 def read_attribute_for_validation(key) send(key) end |
#static_map ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/hrt_bus/bus.rb', line 54 def static_map map = GoogleStaticMap.new(:width => 960, :height => 640) map.markers << MapMarker.new(:color => "blue", :location => MapLocation.new(:latitude => self.lat, :longitude => self.lon)) map.get_map end |