Commit 97cbc87

mokha <mokha@cisco.com>
2019-01-17 18:11:54
extract scim error class tag: v0.2.14
1 parent ee7326f
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/schemas.rb
@@ -13,27 +13,6 @@ module Scim
         RESOURCE_TYPE = "#{CORE}:ResourceType"
         SERVICE_PROVIDER_CONFIGURATION = "#{CORE}:ServiceProviderConfig"
         USER = "#{CORE}:User"
-
-        def self.error
-          Schema.new(id: Messages::ERROR, name: 'Error', location: nil) do |schema|
-            schema.add_attribute(name: :scim_type) do |attribute|
-              attribute.canonical_values = [
-                'invalidPath',
-                'invalidSyntax',
-                'invalidSyntax',
-                'invalidValue',
-                'invalidVers',
-                'mutability',
-                'noTarget',
-                'sensitive',
-                'tooMany',
-                'uniqueness',
-              ]
-            end
-            schema.add_attribute(name: :detail)
-            schema.add_attribute(name: :status, type: :integer)
-          end
-        end
       end
     end
   end
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'
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
@@ -472,13 +472,14 @@ RSpec.describe Scim::Kit::V2::Resource do
     end
   end
 
-  describe "Errors" do
+  describe 'Errors' do
     subject { described_class.new(schemas: schemas) }
-    let(:schemas) { [Scim::Kit::V2::Schemas.error] }
+
+    let(:schemas) { [Scim::Kit::V2::Error.default_schema] }
 
     before do
       subject.scim_type = :invalidSyntax
-      subject.detail = "error"
+      subject.detail = 'error'
       subject.status = 400
     end