Class: Geotrigger::Device
- Includes:
- Taggable
- Defined in:
- lib/geotrigger/device.rb
Overview
Device objects offer ORM-ish access to all attributes of a Device.
device. 'foo'
device.save
device. 'bar'
device.properties = { foo: 'bar', baz: true, bat: 123 }
device.save
Instance Attribute Summary
Attributes inherited from Model
Instance Method Summary collapse
-
#default_tag ⇒ Object
Return the
Stringof this device’s default tag. -
#grok_self_from(data, id = nil) ⇒ Object
Reads the data specific to this
Devicefrom the API response and sets it in @data. -
#initialize(opts = {}) ⇒ Device
constructor
Create a new
Deviceinstance and load @data from the API given aHashwith options:. -
#post_update ⇒ Object
(also: #save)
POST the device’s @data to the API via ‘device/update’, and return the same object with the new @data returned from API call.
Methods inherited from Model
#==, from_api, #method_missing, #post_list
Constructor Details
#initialize(opts = {}) ⇒ Device
Create a new Device instance and load @data from the API given a Hash with options:
- device_id
-
Stringid of the device - tags
-
Arrayname(s) of tag(s) to filter devices by
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/geotrigger/device.rb', line 21 def initialize opts = {} super opts case session.type when :application if opts[:device_id] and @data.nil? grok_self_from post('device/list', deviceIds: opts[:device_id]), opts[:device_id] end when :device if @data.nil? grok_self_from post('device/list'), opts[:device_id] || :first end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Geotrigger::Model
Instance Method Details
#default_tag ⇒ Object
Return the String of this device’s default tag.
37 38 39 |
# File 'lib/geotrigger/device.rb', line 37 def default_tag 'device:%s' % deviceId end |
#grok_self_from(data, id = nil) ⇒ Object
Reads the data specific to this Device from the API response and sets it in @data.
- data
-
Hashthe API response - id
-
Stringthe id of the Device to pull out (first if nil)
66 67 68 69 70 71 72 |
# File 'lib/geotrigger/device.rb', line 66 def grok_self_from data, id = nil if id == :first @data = data['devices'].first else @data = data['devices'].select {|t| t['deviceId'] == (id || @data['deviceId'])}.first end end |
#post_update ⇒ Object Also known as: save
POST the device’s @data to the API via ‘device/update’, and return the same object with the new @data returned from API call.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/geotrigger/device.rb', line 44 def post_update post_data = @data.dup case @session.type when :application post_data['deviceIds'] = post_data.delete 'deviceId' when :device post_data.delete 'deviceId' end post_data.delete 'tags' post_data.delete 'lastSeen' grok_self_from post 'device/update', post_data self end |