class Geotrigger::Trigger

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

trigger.add_tags 'foo'
trigger.save

trigger.remove_tags 'bar'
trigger.properties = { foo: 'bar', baz: true, bat: 123 }
trigger.save

Constants

CIRCLE_KEYS

Public Class Methods

create(session, opts) click to toggle source

Create a Trigger with the given Session and options.

s = Geotrigger::Session.new
t = Geotrigger::Trigger.create s, condition: { ... }, action: { ... }, tags: ['foo']
#=> <Geotrigger::Trigger ... >
# File lib/geotrigger/trigger.rb, line 23
def self.create session, opts
  t = Trigger.new session: session
  t.data = opts
  t.post_create
end
new(opts = {}) click to toggle source

Create a new Trigger instance and load +@data+ from the API given a Hash with options:

tags

Array name(s) of tag(s)

Calls superclass method Geotrigger::Model::new
# 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

Public Instance Methods

circle?() click to toggle source

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.

# 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() click to toggle source

Return the String of this trigger’s default tag.

# File lib/geotrigger/trigger.rb, line 43
def default_tag
  'trigger:%s' % triggerId
end
grok_self_from(data, id = nil) click to toggle source

Reads the data specific to this Trigger from the API response and sets it in +@data+.

data

Hash the API response

triggerId

String the id of the trigger to pull out (first if nil)

# 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() click to toggle source

Creates a trigger by POSTing to trigger/create with +@data+.

# 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 = {}) click to toggle source

POST the trigger’s +@data+ to the API via ‘trigger/update’, and return the same object with the new +@data+ returned from API call.

# 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
Also aliased as: save
save(opts = {})
Alias for: post_update