Class: OpenIDConnect::ConnectObject

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, AttrOptional, AttrRequired
Defined in:
lib/openid_connect/connect_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ConnectObject

Returns a new instance of ConnectObject.



7
8
9
10
11
12
13
# File 'lib/openid_connect/connect_object.rb', line 7

def initialize(attributes = {})
  all_attributes.each do |_attr_|
    self.send :"#{_attr_}=", attributes[_attr_]
  end
  self.raw_attributes = attributes
  attr_missing!
end

Instance Attribute Details

#raw_attributesObject

Returns the value of attribute raw_attributes.



5
6
7
# File 'lib/openid_connect/connect_object.rb', line 5

def raw_attributes
  @raw_attributes
end

Class Method Details

.all_attributesObject



15
16
17
# File 'lib/openid_connect/connect_object.rb', line 15

def self.all_attributes
  required_attributes + optional_attributes
end

Instance Method Details

#all_attributesObject



18
19
20
# File 'lib/openid_connect/connect_object.rb', line 18

def all_attributes
  self.class.all_attributes
end

#as_json(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openid_connect/connect_object.rb', line 29

def as_json(options = {})
  options ||= {} # options can be nil when to_json is called without options
  validate! unless options[:skip_validation]
  all_attributes.inject({}) do |hash, _attr_|
    value = self.send(_attr_)
    hash.merge! _attr_ => case value
    when ConnectObject
      value.as_json options
    else
      value
    end
  end.delete_if do |key, value|
    value.nil?
  end
end

#require_at_least_one_attributesObject



22
23
24
25
26
27
# File 'lib/openid_connect/connect_object.rb', line 22

def require_at_least_one_attributes
  all_blank = all_attributes.all? do |key|
    self.send(key).blank?
  end
  errors.add :base, 'At least one attribute is required' if all_blank
end

#validate!Object



45
46
47
# File 'lib/openid_connect/connect_object.rb', line 45

def validate!
  valid? or raise ValidationFailed.new(self)
end