class NextBuses::Request

Constants

API_URL_STRING

Attributes

password[RW]
stop_code[RW]
username[RW]

Public Class Methods

new(stop_code, username=nil, password=nil) click to toggle source
# File lib/next_buses/request.rb, line 10
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
end

Public Instance Methods

execute() click to toggle source
# File lib/next_buses/request.rb, line 16
def execute
        raise NoStopCodeError, 'A stop_code is required' if @stop_code.nil? || @stop_code.empty?
        raise InvalidCredentialsError, 'A username is required' if @username.nil? || @username.empty?
        raise InvalidCredentialsError, 'A password is required' if @password.nil? || @password.empty?
        
        response = HTTParty.post(API_URL_STRING, basic_auth: auth_hash, body: http_body)
        
        services = Array.new
        response['Siri']['ServiceDelivery']['StopMonitoringDelivery']['MonitoredStopVisit'].each do |visit|
                service = Service.new
                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 = Time.parse(visit['MonitoredVehicleJourney']['MonitoredCall']['AimedDepartureTime'])
                services.push service
        end
        
        services
end

Private Instance Methods

auth_hash() click to toggle source
# File lib/next_buses/request.rb, line 39
def auth_hash
        { username: @username, password: @password }
end
http_body() click to toggle source
# File lib/next_buses/request.rb, line 43
def http_body
        timestamp = Time.now.to_s
        xml = Builder::XmlMarkup.new(indent: 2)
        xml.instruct! :xml, version: '1.0', encoding: 'UTF-8', standalone: 'yes'
        xml.Siri version: '1.0', xmlns: 'http://www.siri.org.uk/' do
                xml.ServiceRequest do |sr|
                        sr.RequestTimestamp timestamp
                        sr.RequestorRef @username
                        sr.StopMonitoringRequest version: 1.0 do
                                sr.RequestTimestamp timestamp
                                sr.MonitoringRef @stop_code
                        end
                end
        end  
end