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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Request.



10
11
12
13
14
# 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

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#stop_codeObject

Returns the value of attribute stop_code.



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

def stop_code
  @stop_code
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#executeObject

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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 = visit['MonitoredVehicleJourney']['MonitoredCall']['AimedDepartureTime']
		services.push service
	end
	
	services
end