Class: Xively::Trigger

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

#errors

Instance Method Summary collapse

Methods included from Parsers::XML::TriggerDefaults

#from_xml

Methods included from Parsers::JSON::TriggerDefaults

#from_json

Methods included from Xively::Templates::JSON::TriggerDefaults

#generate_json

Constructor Details

#initialize(input = {}, format = nil) ⇒ Trigger

Returns a new instance of Trigger.

Raises:



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(options = {})
  generate_json
end

#attributesObject



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(options = {})
  MultiJson.dump as_json(options)
end

#valid?Boolean

Returns:

  • (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