Class: Xively::Trigger
- Includes:
- Parsers::JSON::TriggerDefaults, Parsers::XML::TriggerDefaults, Xively::Templates::JSON::TriggerDefaults, Validations
- Defined in:
- lib/xively-rb/trigger.rb
Constant Summary collapse
- ALLOWED_KEYS =
%w(threshold_value user notified_at url trigger_type id environment_id stream_id)
Instance Attribute Summary
Attributes included from Validations
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #attributes ⇒ Object
- #attributes=(input) ⇒ Object
-
#initialize(input = {}, format = nil) ⇒ Trigger
constructor
A new instance of Trigger.
- #to_json(options = {}) ⇒ Object
- #valid? ⇒ Boolean
Methods included from Parsers::XML::TriggerDefaults
Methods included from Parsers::JSON::TriggerDefaults
Methods included from Xively::Templates::JSON::TriggerDefaults
Constructor Details
#initialize(input = {}, format = nil) ⇒ Trigger
Returns a new instance of Trigger.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/xively-rb/trigger.rb', line 28 def initialize(input = {}, format = nil) raise InvalidFormatError, "Unknown format specified, currently we can only parse JSON or XML." unless [nil,:json,:xml].include?(format) if input.is_a?(Hash) self.attributes = input elsif format == :json || (format.nil? && input.strip[0...1].to_s == "{") self.attributes = from_json(input) else self.attributes = from_xml(input) end end |
Instance Method Details
#as_json(options = {}) ⇒ Object
54 55 56 |
# File 'lib/xively-rb/trigger.rb', line 54 def as_json( = {}) generate_json end |
#attributes ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/xively-rb/trigger.rb', line 39 def attributes h = {} ALLOWED_KEYS.each do |key| value = self.send(key) h[key] = value unless value.nil? end return h end |
#attributes=(input) ⇒ Object
48 49 50 51 52 |
# File 'lib/xively-rb/trigger.rb', line 48 def attributes=(input) return if input.nil? ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) } return attributes end |
#to_json(options = {}) ⇒ Object
58 59 60 |
# File 'lib/xively-rb/trigger.rb', line 58 def to_json( = {}) MultiJson.dump as_json() end |
#valid? ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/xively-rb/trigger.rb', line 17 def valid? pass = true [:url, :stream_id, :environment_id, :user].each do |attr| if self.send(attr).blank? errors[attr] = ["can't be blank"] pass = false end end return pass end |