Sha256: 62ed66c87de711ba6de20e085eb88df5fd2f003a4337db9408d62b11673141d9

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

module OpenIDConnect
  class ResponseObject
    include ActiveModel::Validations, AttrRequired, AttrOptional

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

    def all_attributes
      required_attributes + optional_attributes
    end

    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

    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 ResponseObject
          value.as_json
        else
          value
        end
      end.delete_if do |key, value|
        value.nil?
      end
    end

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

Dir[File.dirname(__FILE__) + '/response_object/*.rb'].each do |file|
  require file
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
openid_connect-0.1.5 lib/openid_connect/response_object.rb
openid_connect-0.1.4 lib/openid_connect/response_object.rb
openid_connect-0.1.3 lib/openid_connect/response_object.rb
openid_connect-0.1.2 lib/openid_connect/response_object.rb