Class: MiOS::Interface
- Inherits:
-
Object
show all
- Defined in:
- lib/mios/interface.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(base_uri) ⇒ Interface
Returns a new instance of Interface.
5
6
7
8
9
|
# File 'lib/mios/interface.rb', line 5
def initialize(base_uri)
@base_uri = base_uri
@client = HTTPClient.new
refresh!
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
11
12
13
14
15
16
17
|
# File 'lib/mios/interface.rb', line 11
def method_missing(method, *args)
if @attributes.has_key?(method.to_s)
@attributes[method.to_s]
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/mios/interface.rb', line 3
def attributes
@attributes
end
|
#base_uri ⇒ Object
Returns the value of attribute base_uri.
3
4
5
|
# File 'lib/mios/interface.rb', line 3
def base_uri
@base_uri
end
|
Instance Method Details
#categories ⇒ Object
46
47
48
49
50
|
# File 'lib/mios/interface.rb', line 46
def categories
@devices.values.map { |device|
device.category
}.uniq.sort
end
|
#device_names ⇒ Object
54
|
# File 'lib/mios/interface.rb', line 54
def device_names; devices.map(&:name); end
|
#devices ⇒ Object
52
|
# File 'lib/mios/interface.rb', line 52
def devices; @devices.values; end
|
#inspect ⇒ Object
19
20
21
|
# File 'lib/mios/interface.rb', line 19
def inspect
"#<MiOS::Interface:0x#{'%x' % (self.object_id << 1)} @base_uri=#{@base_uri} @attributes=#{@attributes.inspect}>"
end
|
#refresh! ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/mios/interface.rb', line 23
def refresh!
data = MultiJson.load(@client.get("#{base_uri}/data_request", {:id => "user_data", :output_format => :json}).content)
@attributes = Hash[
data.select { |k, v|
!data[k].kind_of?(Hash) and !data[k].kind_of?(Array)
}.map { |k, v|
[k.downcase, v]
}
]
['loadtime', 'devicesync'].each do |attr|
@attributes[attr] = Time.at(@attributes[attr].to_i)
end
@devices = Hash[
data['devices'].map { |device|
[device['id'], Device.new(@client, @base_uri, device)]
}
]
true
end
|