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 ]
- DIRECTIONS =
{ "1" => "inbound", "2" => "outbound" }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(attrs) ⇒ Object
-
#initialize(attributes = {}) ⇒ Bus
constructor
A new instance of Bus.
- #read_attribute_for_validation(key) ⇒ Object
- #static_map ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Bus
Returns a new instance of Bus.
20 21 22 |
# File 'lib/hrt_bus/bus.rb', line 20 def initialize(attributes={}) self.attributes = attributes end |
Class Method Details
.active_buses ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hrt_bus/bus.rb', line 50 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, route_id, direction = row[0], row[1], row[2], row[3], row[4], row[7], DIRECTIONS[row[8]] time = HrtBus::Parse.time(time, date) lat, lon = HrtBus::Parse.geo(lat_lon) bus = new(:id => id, :time => time, :direction => direction, :route_id => route_id, :lat => lat, :lon => lon) buses << bus if bus.valid? end buses end |
.all ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/hrt_bus/bus.rb', line 81 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, route_id, direction = row[0], row[1], row[2], row[3], row[4], row[7], DIRECTIONS[row[8]] time = HrtBus::Parse.time(time, date) lat, lon = HrtBus::Parse.geo(lat_lon) buses << new(:id => id, :time => time, :direction => direction, :route_id => route_id, :lat => lat, :lon => lon) end buses end |
Instance Method Details
#attributes ⇒ Object
24 25 26 27 28 29 |
# File 'lib/hrt_bus/bus.rb', line 24 def attributes ATTRIBUTES.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, key| result[key] = read_attribute_for_validation(key) result end end |
#attributes=(attrs) ⇒ Object
31 32 33 34 35 |
# File 'lib/hrt_bus/bus.rb', line 31 def attributes=(attrs) attrs.each_pair do |k, v| send("#{k}=", v) end end |
#read_attribute_for_validation(key) ⇒ Object
37 38 39 |
# File 'lib/hrt_bus/bus.rb', line 37 def read_attribute_for_validation(key) send(key) end |
#static_map ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/hrt_bus/bus.rb', line 41 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 |