Class: Geotrigger::Tag

Inherits:
Model
  • Object
show all
Defined in:
lib/geotrigger/tag.rb

Overview

Tag objects offer ORM-ish access to all attributes of a Tag.

Instance Attribute Summary

Attributes inherited from Model

#data, #session

Class Method Summary collapse

Instance Method Summary collapse

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

String name 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

Hash any 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

Hash the API response

name

String the 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_createObject

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_updateObject 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

Hash any 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