Comparing changes

v0.2.13 v0.2.14
3 commits 9 files changed

Commits

97cbc87 extract scim error class mokha 2019-01-17 18:11:54
ee7326f produce errors using error schema mokha 2019-01-17 18:00:42
0cd6c95 add raw_attributes to resource mokha 2019-01-16 19:05:20
lib/scim/kit/v2/error.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+module Scim
+  module Kit
+    module V2
+      # Represents a SCIM Error
+      class Error < Resource
+        SCIM_TYPES = %w[
+          invalidPath
+          invalidSyntax
+          invalidSyntax
+          invalidValue
+          invalidVers
+          mutability
+          noTarget
+          sensitive
+          tooMany
+          uniqueness
+        ].freeze
+
+        def initialize(schemas: [self.class.default_schema])
+          super(schemas: schemas)
+        end
+
+        def template_name
+          'resource.json.jbuilder'
+        end
+
+        def self.default_schema
+          Schema.new(id: Messages::ERROR, name: 'Error', location: nil) do |x|
+            x.add_attribute(name: :scim_type) do |attribute|
+              attribute.canonical_values = SCIM_TYPES
+            end
+            x.add_attribute(name: :detail)
+            x.add_attribute(name: :status, type: :integer)
+          end
+        end
+      end
+    end
+  end
+end
lib/scim/kit/v2/resource.rb
@@ -12,6 +12,7 @@ module Scim
         attr_accessor :id, :external_id
         attr_reader :meta
         attr_reader :schemas
+        attr_reader :raw_attributes
 
         validate :schema_validations
 
@@ -19,6 +20,7 @@ module Scim
           @meta = Meta.new(schemas[0].name, location)
           @meta.disable_timestamps
           @schemas = schemas
+          @raw_attributes = attributes
           schemas.each do |schema|
             define_attributes_for(self, schema.attributes)
           end
lib/scim/kit/v2/schema.rb
@@ -17,6 +17,7 @@ module Scim
           @meta = Meta.new('Schema', location)
           @meta.created = @meta.last_modified = @meta.version = nil
           @attributes = []
+          yield self if block_given?
         end
 
         def add_attribute(name:, type: :string)
@@ -26,7 +27,7 @@ module Scim
         end
 
         def core?
-          id.include?(Schemas::CORE)
+          id.include?(Schemas::CORE) || id.include?(Messages::CORE)
         end
 
         def self.build(*args)
lib/scim/kit/templatable.rb
@@ -19,6 +19,10 @@ module Scim
       def render(model, options)
         Template.new(model).to_json(options)
       end
+
+      def template_name
+        "#{self.class.name.split('::').last.underscore}.json.jbuilder"
+      end
     end
   end
 end
lib/scim/kit/template.rb
@@ -19,11 +19,7 @@ module Scim
       private
 
       def template_path
-        TEMPLATES_DIR.join(template_name)
-      end
-
-      def template_name
-        "#{target.class.name.split('::').last.underscore}.json.jbuilder"
+        TEMPLATES_DIR.join(target.template_name)
       end
 
       def template
lib/scim/kit/v2.rb
@@ -9,6 +9,7 @@ require 'scim/kit/v2/messages'
 require 'scim/kit/v2/meta'
 require 'scim/kit/v2/mutability'
 require 'scim/kit/v2/resource'
+require 'scim/kit/v2/error'
 require 'scim/kit/v2/resource_type'
 require 'scim/kit/v2/returned'
 require 'scim/kit/v2/schema'
lib/scim/kit/version.rb
@@ -2,6 +2,6 @@
 
 module Scim
   module Kit
-    VERSION = '0.2.13'
+    VERSION = '0.2.14'
   end
 end
spec/scim/kit/v2/error_spec.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+RSpec.describe Scim::Kit::V2::Error do
+  subject { described_class.new }
+
+  before do
+    subject.scim_type = :invalidSyntax
+    subject.detail = 'error'
+    subject.status = 400
+  end
+
+  specify { expect(subject.to_h[:schemas]).to match_array([Scim::Kit::V2::Messages::ERROR]) }
+  specify { expect(subject.to_h[:scimType]).to eql('invalidSyntax') }
+  specify { expect(subject.to_h[:detail]).to eql('error') }
+  specify { expect(subject.to_h[:status]).to be(400) }
+end
spec/scim/kit/v2/resource_spec.rb
@@ -461,6 +461,7 @@ RSpec.describe Scim::Kit::V2::Resource do
         end
       end
 
+      specify { expect(subject.raw_attributes).to eql(attributes) }
       specify { expect(subject.user_name).to eql(user_name) }
       specify { expect(subject.age).to be(34) }
       specify { expect(subject.colours).to match_array(%w[red green blue]) }
@@ -470,4 +471,21 @@ RSpec.describe Scim::Kit::V2::Resource do
       specify { expect(subject.emails[0][:primary]).to be(true) }
     end
   end
+
+  describe 'Errors' do
+    subject { described_class.new(schemas: schemas) }
+
+    let(:schemas) { [Scim::Kit::V2::Error.default_schema] }
+
+    before do
+      subject.scim_type = :invalidSyntax
+      subject.detail = 'error'
+      subject.status = 400
+    end
+
+    specify { expect(subject.to_h[:schemas]).to match_array([Scim::Kit::V2::Messages::ERROR]) }
+    specify { expect(subject.to_h[:scimType]).to eql('invalidSyntax') }
+    specify { expect(subject.to_h[:detail]).to eql('error') }
+    specify { expect(subject.to_h[:status]).to be(400) }
+  end
 end