Class: RouteC::Query
- Inherits:
-
Object
- Object
- RouteC::Query
- Defined in:
- lib/route_c/query.rb
Class Method Summary collapse
Instance Method Summary collapse
- #average_occupancy ⇒ Object
-
#initialize(station, direction, datetime = nil) ⇒ Query
constructor
A new instance of Query.
- #json ⇒ Object
- #lights ⇒ Object
- #set_datetime(datetime) ⇒ Object
- #to_a ⇒ Object
- #to_lights ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(station, direction, datetime = nil) ⇒ Query
Returns a new instance of Query.
3 4 5 |
# File 'lib/route_c/query.rb', line 3 def initialize(station, direction, datetime = nil) @station, @direction, @datetime = station, direction, set_datetime(datetime) end |
Class Method Details
.config ⇒ Object
21 22 23 |
# File 'lib/route_c/query.rb', line 21 def self.config YAML.load_file 'config/config.yaml' end |
Instance Method Details
#average_occupancy ⇒ Object
33 34 35 36 37 |
# File 'lib/route_c/query.rb', line 33 def average_occupancy loads = json.first.last average = loads.values.inject{ |sum, el| sum + el }.to_f / loads.size average.round end |
#json ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/route_c/query.rb', line 25 def json request = open(url, http_basic_authentication: [ ENV['SIR_HANDEL_USERNAME'], ENV['SIR_HANDEL_PASSWORD'] ]) JSON.parse request.read end |
#lights ⇒ Object
52 53 54 |
# File 'lib/route_c/query.rb', line 52 def lights @lights ||= Lights.new(to_a) end |
#set_datetime(datetime) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/route_c/query.rb', line 7 def set_datetime(datetime) if datetime.nil? hour = Time.now.hour minute = Time.now.min "2015-09-23T#{hour}:#{minute}:00" else datetime end end |
#to_a ⇒ Object
43 44 45 46 |
# File 'lib/route_c/query.rb', line 43 def to_a num = Query.num_elements average_occupancy Array.new(Lights.pins.count).each_with_index.map { |k,v| v + 1 <= num ? 1 : 0 } end |
#to_lights ⇒ Object
48 49 50 |
# File 'lib/route_c/query.rb', line 48 def to_lights lights.turn_on end |