Class: Howard::ArrivalsResource

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/howard/arrivals_resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ArrivalsResource

Returns a new instance of ArrivalsResource.



7
8
9
# File 'lib/howard/arrivals_resource.rb', line 7

def initialize(params={})
  @params = arrivals_params(params)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/howard/arrivals_resource.rb', line 5

def params
  @params
end

Instance Method Details

#arrivals_params(params) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/howard/arrivals_resource.rb', line 27

def arrivals_params(params)
  {
    mapid: params[:mapid] || params[:station],
    stpid: params[:stpid] || params[:stop],
    max: params[:max],
    rt: params[:rt] || params[:route],
  }.reject { |_, val| val.nil? }
end

#eachObject



11
12
13
14
15
# File 'lib/howard/arrivals_resource.rb', line 11

def each
  Array(xml_response["ctatt"]["eta"])
    .map { |eta| Arrival.new(eta) }
    .each { |arrival| yield arrival }
end

#gatewayObject



17
18
19
# File 'lib/howard/arrivals_resource.rb', line 17

def gateway
  @gateway ||= TrainTrackerGateway.new(Howard.train_tracker_api_key)
end

#xml_responseObject



21
22
23
24
25
# File 'lib/howard/arrivals_resource.rb', line 21

def xml_response
  @xml_resonse ||= gateway
    .arrivals(query: params)
    .parsed_response
end