Class: Xively::Key
- 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
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
- #attributes ⇒ Object
- #attributes=(input) ⇒ Object
- #id ⇒ Object
-
#initialize(input = {}, format = nil) ⇒ Key
constructor
Build an instance from either a Hash, a JSON string, or an XML document.
- #permissions ⇒ Object
- #permissions=(array) ⇒ Object
- #private_access? ⇒ Boolean
- #to_json(options = nil) ⇒ Object
- #valid? ⇒ Boolean
Methods included from Parsers::XML::KeyDefaults
Methods included from Parsers::JSON::KeyDefaults
Methods included from Templates::JSON::KeyDefaults
Constructor Details
#initialize(input = {}, format = nil) ⇒ Key
Build an instance from either a Hash, a JSON string, or an XML document
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( = nil) generate_json( || {}) end |
#attributes ⇒ Object
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 |
#id ⇒ Object
95 96 97 |
# File 'lib/xively-rb/key.rb', line 95 def id @id.nil? ? @key : @id end |
#permissions ⇒ Object
74 75 76 77 |
# File 'lib/xively-rb/key.rb', line 74 def 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 (array) return unless array.is_a?(Array) @permissions = [] array.each do || if .is_a?(Permission) @permissions << elsif .is_a?(Hash) @permissions << Permission.new() end end end |
#private_access? ⇒ 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( = nil) MultiJson.dump(as_json()) end |
#valid? ⇒ 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 .each do || unless .valid? .errors.each do |attr, | errors["permissions_#{attr}".to_sym] = ([*errors["permissions_#{attr}".to_sym]] | [*]).compact end pass = false end end return pass end |