Class: Xively::Key

Inherits:
Object show all
Includes:
Parsers::JSON::KeyDefaults, Parsers::XML::KeyDefaults, Templates::JSON::KeyDefaults, Validations
Defined in:
lib/xively-rb/key.rb

Constant Summary collapse

ALLOWED_KEYS =
%w(id key label user expires_at permissions private_access)
NESTED_KEYS =
%w(permissions)

Instance Attribute Summary

Attributes included from Validations

#errors

Instance Method Summary collapse

Methods included from Parsers::XML::KeyDefaults

#from_xml

Methods included from Parsers::JSON::KeyDefaults

#from_json

Methods included from Templates::JSON::KeyDefaults

#generate_json

Constructor Details

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

Build an instance from either a Hash, a JSON string, or an XML document

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/xively-rb/key.rb', line 36

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 = nil) ⇒ Object



66
67
68
# File 'lib/xively-rb/key.rb', line 66

def as_json(options = nil)
  generate_json(options || {})
end

#attributesObject



47
48
49
50
51
52
53
54
# File 'lib/xively-rb/key.rb', line 47

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



56
57
58
59
60
61
62
63
64
# File 'lib/xively-rb/key.rb', line 56

def attributes=(input)
  return if input.nil?
  input.deep_stringify_keys!
  ALLOWED_KEYS.each { |key| self.send("#{key}=", input[key]) }
  NESTED_KEYS.each { |key|
    self.send("#{key}=".to_sym, input["#{key}_attributes"]) unless input["#{key}_attributes"].nil?
  }
  return attributes
end

#idObject



95
96
97
# File 'lib/xively-rb/key.rb', line 95

def id
  @id.nil? ? @key : @id
end

#permissionsObject



74
75
76
77
# File 'lib/xively-rb/key.rb', line 74

def permissions
  return [] if @permissions.nil?
  @permissions
end

#permissions=(array) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/xively-rb/key.rb', line 79

def permissions=(array)
  return unless array.is_a?(Array)
  @permissions = []
  array.each do |permission|
    if permission.is_a?(Permission)
      @permissions << permission
    elsif permission.is_a?(Hash)
      @permissions << Permission.new(permission)
    end
  end
end

#private_access?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/xively-rb/key.rb', line 91

def private_access?
  @private_access || false
end

#to_json(options = nil) ⇒ Object



70
71
72
# File 'lib/xively-rb/key.rb', line 70

def to_json(options = nil)
  MultiJson.dump(as_json(options))
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/xively-rb/key.rb', line 14

def valid?
  pass = true
  [:label, :permissions, :user].each do |attr|
    if self.send(attr).blank?
      errors[attr] = ["can't be blank"]
      pass = false
    end
  end

  permissions.each do |permission|
    unless permission.valid?
      permission.errors.each do |attr, permission_errors|
        errors["permissions_#{attr}".to_sym] = ([*errors["permissions_#{attr}".to_sym]] | [*permission_errors]).compact
      end
      pass = false
    end
  end

  return pass
end