Class: Geotrigger::Tag
Overview
Tag objects offer ORM-ish access to all attributes of a Tag.
Instance Attribute Summary
Attributes inherited from Model
Class Method Summary collapse
-
.create(session, opts) ⇒ Object
Create a Tag with the given
Sessionand options.
Instance Method Summary collapse
-
#devices(params = {}) ⇒ Object
Return an
ArrayofDeviceobjects in this Application that have this tag applied to them. -
#grok_self_from(data, name = nil) ⇒ Object
Reads the data specific to this
Tagfrom the API response and sets it in @data. -
#initialize(opts = {}) ⇒ Tag
constructor
Create a new
Taginstance and load @data from the API given aHashwith options:. -
#post_create ⇒ Object
Creates a tag by POSTing to tag/permissions/update with @data.
-
#post_update ⇒ Object
(also: #save)
POST the tag’s @data to the API via ‘tag/permissions/update’, and return the same object with the new @data returned from API call.
-
#triggers(params = {}) ⇒ Object
Return an
ArrayofTriggerobjects in this Application that have this tag applied to them.
Methods inherited from Model
#==, from_api, #method_missing, #post_list
Constructor Details
#initialize(opts = {}) ⇒ Tag
Create a new Tag instance and load @data from the API given a Hash with options:
- name
-
Stringname of the tag
28 29 30 31 32 33 |
# File 'lib/geotrigger/tag.rb', line 28 def initialize opts = {} super opts if opts[:name] and @data.nil? grok_self_from post('tag/list', tags: opts[:name]), opts[:name] end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Geotrigger::Model
Class Method Details
.create(session, opts) ⇒ Object
Create a Tag with the given Session and options. Note that Tags are automatically created by the API, if needed, when added to a Trigger or Device. This offers a way to create the Tag before applying it to anything.
s = Geotrigger::Session.new
tag = Geotrigger::Tag.create s, name: 'foo', deviceTagging: false
#=> <Geotrigger::Tag ... >
16 17 18 19 20 21 |
# File 'lib/geotrigger/tag.rb', line 16 def self.create session, opts t = ::Geotrigger::Tag.new session: session t.data = opts t.data[:tags] = t.data.delete :name if t.data[:name] t.post_create end |
Instance Method Details
#devices(params = {}) ⇒ Object
Return an Array of Device objects in this Application that have this tag applied to them.
- params
-
Hashany additional parameters to include in the request (device/list)
49 50 51 |
# File 'lib/geotrigger/tag.rb', line 49 def devices params = {} post_list 'devices', params, tags: name end |
#grok_self_from(data, name = nil) ⇒ Object
Reads the data specific to this Tag from the API response and sets it in @data.
- data
-
Hashthe API response - name
-
Stringthe name of the Tag to pull out
79 80 81 |
# File 'lib/geotrigger/tag.rb', line 79 def grok_self_from data, name = nil @data = data['tags'].select {|t| t['name'] == (name || @data['name'])}.first end |
#post_create ⇒ Object
Creates a tag by POSTing to tag/permissions/update with @data.
55 56 57 58 59 |
# File 'lib/geotrigger/tag.rb', line 55 def post_create post_data = @data.dup grok_self_from post('tag/permissions/update', post_data), @data[:tags] self end |
#post_update ⇒ Object Also known as: save
POST the tag’s @data to the API via ‘tag/permissions/update’, and return the same object with the new @data returned from API call.
64 65 66 67 68 69 70 |
# File 'lib/geotrigger/tag.rb', line 64 def post_update raise StateError.new 'device access_token prohibited' if @session.device? post_data = @data.dup post_data['tags'] = post_data.delete 'name' grok_self_from post 'tag/permissions/update', post_data self end |
#triggers(params = {}) ⇒ Object
Return an Array of Trigger objects in this Application that have this tag applied to them.
- params
-
Hashany additional parameters to include in the request (trigger/list)
40 41 42 |
# File 'lib/geotrigger/tag.rb', line 40 def triggers params = {} post_list 'triggers', params, tags: name end |