Class: GPS::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gpsd/response.rb', line 8

def initialize(response)
  if response.is_a?(String)
    @payload = MultiJson.load(response)
  elsif response.is_a?(Hash)
    @payload = response
  else
    raise GPS::ProtocolError.new("Invalid response data")
  end

  @payload['time'] = Time.parse(@payload['time']) if @payload.has_key?('time')

  case @payload['class']
  when 'POLL'
    @payload['tpv'] = @payload['tpv'].collect{|i|
      GPS::Response.new(i)
    }

    @payload['sky'] = @payload['sky'].collect{|i|
      GPS::Response.new(i)
    }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/gpsd/response.rb', line 31

def method_missing(method, *args)
  if @payload.has_key?(method.to_s)
    return @payload[method.to_s]
  else
    super
  end
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



6
7
8
# File 'lib/gpsd/response.rb', line 6

def payload
  @payload
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
42
43
# File 'lib/gpsd/response.rb', line 39

def [](key)
  return @payload[key.to_s]
rescue
  return nil
end

#classnameObject



45
46
47
48
49
# File 'lib/gpsd/response.rb', line 45

def classname()
  return @payload['class'].to_sym
rescue
  return nil
end

#to_hashObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gpsd/response.rb', line 51

def to_hash()
  Hash[payload().collect{|k,v|
    if v.is_a?(GPS::Response)
      [k, v.to_hash()]
    elsif v.is_a?(Array) and v.first.is_a?(GPS::Response)
      [k, v.collect{|i| i.to_hash() }]
    else
      [k, v]
    end
  }]
end