Class: NextBuses::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/next_buses/request.rb

Constant Summary collapse

API_URL_STRING =
"http://nextbus.mxdata.co.uk/nextbuses/1.0/1"

Instance Method Summary collapse

Constructor Details

#initialize(stop_code, username = nil, password = nil) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
# File 'lib/next_buses/request.rb', line 8

def initialize(stop_code, username=nil, password=nil)
  @stop_code = stop_code
  @username = username == nil ? NextBuses.configuration.username : username
  @password = password == nil ? NextBuses.configuration.password : password
  @timestamp = Time.now.to_s
end

Instance Method Details

#executeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/next_buses/request.rb', line 15

def execute
  response = HTTParty.post(API_URL_STRING, basic_auth: auth_hash, body: http_body)
  
  services = Array.new
  response['Siri']['ServiceDelivery']['StopMonitoringDelivery']['MonitoredStopVisit'].each do |visit|
    services.push Service.new do |service|
      service.vehicle_mode = visit['MonitoredVehicleJourney']['VehicleMode']
      service.published_line_name = visit['MonitoredVehicleJourney']['PublishedLineName']
      service.direction_name = visit['MonitoredVehicleJourney']['DirectionName']
      service.operator_ref = visit['MonitoredVehicleJourney']['OperatorRef']
      service.aimed_departure_time = visit['MonitoredVehicleJourney']['MonitoredCall']['AimedDepartureTime']
    end
  end
  
  services
end