Class: Xively::Permission

Inherits:
Object show all
Includes:
Validations
Defined in:
lib/xively-rb/permission.rb

Constant Summary collapse

ALLOWED_KEYS =
%w(label access_methods referer source_ip minimum_interval resources)
NESTED_KEYS =
%w(resources)

Instance Attribute Summary

Attributes included from Validations

#errors

Instance Method Summary collapse

Constructor Details

#initialize(input = {}) ⇒ Permission

Build an instance from a Hash only



29
30
31
# File 'lib/xively-rb/permission.rb', line 29

def initialize(input = {})
  self.attributes = input
end

Instance Method Details

#attributesObject



50
51
52
53
54
55
56
57
# File 'lib/xively-rb/permission.rb', line 50

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



59
60
61
62
63
64
65
# File 'lib/xively-rb/permission.rb', line 59

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}=", input["#{key}_attributes"]) unless input["#{key}_attributes"].nil? }
  return attributes
end

#resourcesObject



33
34
35
36
# File 'lib/xively-rb/permission.rb', line 33

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

#resources=(array) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xively-rb/permission.rb', line 38

def resources=(array)
  return unless array.is_a?(Array)
  @resources = []
  array.each do |resource|
    if resource.is_a?(Resource)
      @resources << resource
    elsif resource.is_a?(Hash)
      @resources << Resource.new(resource)
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/xively-rb/permission.rb', line 9

def valid?
  pass = true
  if access_methods.nil? || access_methods.empty?
    errors[:access_methods] = ["can't be blank"]
    pass = false
  end

  resources.each do |resource|
    unless resource.valid?
      resource.errors.each do |attr, resource_errors|
        errors["resources_#{attr}".to_sym] = ([*errors["resources_#{attr}".to_sym]] | [*resource_errors]).compact
      end
      pass = false
    end
  end

  return pass
end