Class: CtaTrack::Vehicles

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Vehicles

Returns a new instance of Vehicles.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vehicles.rb', line 6

def initialize(args)
  raise ArgumentError, "must enter api key" if API.apikey.nil?
  @result = {} 
  API.vehicles(args).root.xpath("vehicle").each do |node|
    time = node.xpath('tmstmp').text
    year = time[(0..3)].to_i
    month = time[(4..5)].to_i
    day = time[(6..7)].to_i
    hour = time[(9..10)].to_i
    minute = time[(12..13)].to_i
    
    @result[node.xpath('vid').text] = {
      :vid => node.xpath('vid').text.to_i,
      :lat => node.xpath('lat').text.to_f,
      :lon => node.xpath('lon').text.to_f,
      :des => node.xpath('des').text,
      :heading => node.xpath('hdg').text.to_i,
      :pattern_id => node.xpath('pid').text.to_i,
      :pattern_distance => node.xpath('pdist').text.to_i, 
      :rt => node.xpath('rt').text.to_i, 
      :delay => node.xpath('dly').text, 
      :timestamp => DateTime.new(year, month, day, hour, minute)
    }
  end
  def keys
    @result.keys
  end
  def method_missing(method_name, vid, *arguments, &block)
    @result[vid][method_name.to_sym]
  end
  def respond_to?(method_name, include_private = false)
    @result[@result.keys[0]].include?("#{method_name}".to_sym)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, vid, *arguments, &block) ⇒ Object



33
34
35
# File 'lib/vehicles.rb', line 33

def method_missing(method_name, vid, *arguments, &block)
  @result[vid][method_name.to_sym]
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



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

def result
  @result
end

Instance Method Details

#keysObject



30
31
32
# File 'lib/vehicles.rb', line 30

def keys
  @result.keys
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/vehicles.rb', line 36

def respond_to?(method_name, include_private = false)
  @result[@result.keys[0]].include?("#{method_name}".to_sym)
end