Sha256: 0dc4249f1d3028898ba5917bc85498944bf28d4957d3036dc9aab02c4aa9e5ce

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

require 'active_model'
require 'tzinfo'
require 'validate_url'
require 'validate_email'
require 'attr_required'
require 'attr_optional'

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

    class ValidationFailed < Exception
      attr_reader :errors

      def initialize(errors)
        super errors.full_messages.to_sentence
        @errors = errors
      end
    end

    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 = {})
      validate!
      all_attributes.inject({}) do |hash, _attr_|
        hash.merge! _attr_ => self.send(_attr_)
      end.delete_if do |key, value|
        value.nil?
      end
    end

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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
openid_connect-0.0.22 lib/openid_connect/response_object.rb
openid_connect-0.0.21 lib/openid_connect/response_object.rb
openid_connect-0.0.20 lib/openid_connect/response_object.rb
openid_connect-0.0.19 lib/openid_connect/response_object.rb
openid_connect-0.0.18 lib/openid_connect/response_object.rb
openid_connect-0.0.17 lib/openid_connect/response_object.rb
openid_connect-0.0.16 lib/openid_connect/response_object.rb
openid_connect-0.0.15 lib/openid_connect/response_object.rb
openid_connect-0.0.14 lib/openid_connect/response_object.rb
openid_connect-0.0.13 lib/openid_connect/response_object.rb