class Geotrigger::Model
Superclass for Geotrigger
“objects” - Application
, Trigger
, Device
, Tag
.
Contains the base logic for interacting with the Geotrigger
API in an ORM-like fashion. Never instantiated directly by the user.
Attributes
The data behind the model object.
The Session
the model object uses to talk to the API.
Public Class Methods
Create an instance of the subclassed model object from data retrieved from the API.
# File lib/geotrigger/model.rb, line 27 def self.from_api data, session i = self.new session: session i.data = data return i end
Create an instance and from given options Hash
.
- :session
-
Session
underlying session to use when talking to the API
# File lib/geotrigger/model.rb, line 37 def initialize opts = {} @session = opts[:session] || Session.new(opts) end
Public Instance Methods
Compares underlying data for equality.
# File lib/geotrigger/model.rb, line 87 def == obj if Model === obj self.data == obj.data else false end end
Allows snake_case accessor to top-level data values keyed by their camelCase counterparts. An attempt to be moar Rubyish.
device.tracking_profile #=> 'adaptive' device.trackingProfile #=> 'adaptive'
# File lib/geotrigger/model.rb, line 66 def method_missing meth, *args meth_s = meth.to_s if meth_s =~ /=$/ and args.length == 1 key = meth_s.sub(/=$/,'').camelcase if @data and @data.key? key @data[key] = args[0] else super meth, *args end else key = meth_s.camelcase if @data and @data.key? key @data[key] else super meth, *args end end end
POST a request to this model’s /list route, passing parameters. Returns a new instance of the model object with populated data via Model.from_api
.
- models
-
String
name of the model to request listed data for - params
-
Hash
parameters to send with the request - default_params
-
Hash
default parameters to mergeparams
into
# File lib/geotrigger/model.rb, line 49 def post_list models, params = {}, default_params = {} model = models.sub /s$/, '' params = default_params.merge params post(model + '/list', params)[models].map do |data| Geotrigger.const_get(model.capitalize).from_api data, @session end end