class RouteC::Query
Public Class Methods
boundify(direction)
click to toggle source
# File lib/route_c/query.rb, line 55 def self.boundify direction case direction when /^n/ 'northbound' when /^s/ 'southbound' else raise RouteCException.new "Invalid direction '#{direction}'" end end
new(station, direction, datetime = nil)
click to toggle source
# File lib/route_c/query.rb, line 3 def initialize(station, direction, datetime = nil) @station = Query.validate_station(station) @direction = Query.boundify(direction) @datetime = set_datetime(datetime) @config = Config.new end
num_elements(average, elements = Config.new.lights.count)
click to toggle source
# File lib/route_c/query.rb, line 38 def self.num_elements average, elements = Config.new.lights.count (elements * (average / 100.0)).round end
validate_station(station)
click to toggle source
# File lib/route_c/query.rb, line 66 def self.validate_station station s = station.downcase.gsub(' ', '_') return s if Config.new.stations.include? s raise RouteCException.new "Unknown station '#{station}'" end
Public Instance Methods
average_occupancy()
click to toggle source
# File lib/route_c/query.rb, line 32 def average_occupancy loads = json.first.last average = loads.values.inject{ |sum, el| sum + el }.to_f / loads.size average.round end
json()
click to toggle source
# File lib/route_c/query.rb, line 24 def json request = open(url, http_basic_authentication: [ ENV['SIR_HANDEL_USERNAME'], ENV['SIR_HANDEL_PASSWORD'] ]) JSON.parse request.read end
lights()
click to toggle source
# File lib/route_c/query.rb, line 51 def lights @lights ||= Lights.new(to_a) end
set_datetime(datetime)
click to toggle source
# File lib/route_c/query.rb, line 10 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()
click to toggle source
# File lib/route_c/query.rb, line 42 def to_a num = Query.num_elements average_occupancy Array.new(@config.lights.count).each_with_index.map { |k,v| v + 1 <= num ? 1 : 0 } end
to_lights()
click to toggle source
# File lib/route_c/query.rb, line 47 def to_lights lights.turn_on end
url()
click to toggle source
# File lib/route_c/query.rb, line 20 def url "#{@config.base_url}#{@direction}/#{@station}/#{@datetime}.json" end