Class: Geotrigger::Trigger
- Includes:
- Taggable
- Defined in:
- lib/geotrigger/trigger.rb
Overview
Trigger objects offer ORM-ish access to all attributes of a Trigger.
trigger. 'foo'
trigger.save
trigger. 'bar'
trigger.properties = { foo: 'bar', baz: true, bat: 123 }
trigger.save
Constant Summary collapse
- CIRCLE_KEYS =
%w[latitude longitude distance]
Instance Attribute Summary
Attributes inherited from Model
Class Method Summary collapse
-
.create(session, opts) ⇒ Object
Create a Trigger with the given
Sessionand options.
Instance Method Summary collapse
-
#circle? ⇒ Boolean
True if trigger is a “circle” type, meaning it has a point(longitude,latitude) and radius(distance) in its condition, rather than only a geojson or esrijson geometry.
-
#default_tag ⇒ Object
Return the
Stringof this trigger’s default tag. -
#grok_self_from(data, id = nil) ⇒ Object
Reads the data specific to this
Triggerfrom the API response and sets it in @data. -
#initialize(opts = {}) ⇒ Trigger
constructor
Create a new
Triggerinstance and load @data from the API given aHashwith options:. -
#post_create ⇒ Object
Creates a trigger by POSTing to trigger/create with @data.
-
#post_update(opts = {}) ⇒ Object
(also: #save)
POST the trigger’s @data to the API via ‘trigger/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 = {}) ⇒ Trigger
Create a new Trigger instance and load @data from the API given a Hash with options:
- tags
-
Arrayname(s) of tag(s)
34 35 36 37 38 39 |
# File 'lib/geotrigger/trigger.rb', line 34 def initialize opts = {} super opts if opts[:trigger_id] and @data.nil? grok_self_from post('trigger/list', triggerIds: opts[:trigger_id]), opts[:trigger_id] 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 Trigger with the given Session and options.
s = Geotrigger::Session.new
t = Geotrigger::Trigger.create s, condition: { ... }, action: { ... }, tags: ['foo']
#=> <Geotrigger::Trigger ... >
23 24 25 26 27 |
# File 'lib/geotrigger/trigger.rb', line 23 def self.create session, opts t = Trigger.new session: session t.data = opts t.post_create end |
Instance Method Details
#circle? ⇒ Boolean
True if trigger is a “circle” type, meaning it has a point(longitude,latitude) and radius(distance) in its condition, rather than only a geojson or esrijson geometry.
86 87 88 |
# File 'lib/geotrigger/trigger.rb', line 86 def circle? not CIRCLE_KEYS.map {|k| @data['condition']['geo'].keys.include? k}.select {|e| e}.empty? end |
#default_tag ⇒ Object
Return the String of this trigger’s default tag.
43 44 45 |
# File 'lib/geotrigger/trigger.rb', line 43 def default_tag 'trigger:%s' % triggerId end |
#grok_self_from(data, id = nil) ⇒ Object
Reads the data specific to this Trigger from the API response and sets it in @data.
- data
-
Hashthe API response - triggerId
-
Stringthe id of the trigger to pull out (first if nil)
79 80 81 |
# File 'lib/geotrigger/trigger.rb', line 79 def grok_self_from data, id = nil @data = data['triggers'].select {|t| t['triggerId'] == (id || @data['triggerId'])}.first end |
#post_create ⇒ Object
Creates a trigger by POSTing to trigger/create with @data.
49 50 51 52 53 |
# File 'lib/geotrigger/trigger.rb', line 49 def post_create post_data = @data.dup @data = post 'trigger/create', post_data self end |
#post_update(opts = {}) ⇒ Object Also known as: save
POST the trigger’s @data to the API via ‘trigger/update’, and return the same object with the new @data returned from API call.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/geotrigger/trigger.rb', line 58 def post_update opts = {} post_data = @data.dup post_data['triggerIds'] = post_data.delete 'triggerId' post_data.delete 'tags' if circle? post_data['condition']['geo'].delete 'geojson' post_data['condition']['geo'].delete 'esrijson' end grok_self_from post 'trigger/update', post_data.merge(opts) self end |