Commit 93effe8

mokha <mokha@cisco.com>
2019-02-12 23:17:01
parse the resource type json
1 parent 2cffcd0
Changed files (2)
lib
scim
spec
lib/scim/kit/v2/resource_type.rb
@@ -13,7 +13,7 @@ module Scim
         attr_accessor :endpoint
         attr_accessor :schema
         attr_reader :schema_extensions
-        attr_reader :meta
+        attr_accessor :meta
 
         def initialize(location:)
           @meta = Meta.new('ResourceType', location)
@@ -25,10 +25,29 @@ module Scim
           @schema_extensions.push(schema: schema, required: required)
         end
 
-        def self.build(*args)
-          item = new(*args)
-          yield item
-          item
+        class << self
+          def build(*args)
+            item = new(*args)
+            yield item
+            item
+          end
+
+          def parse(json, hash = JSON.parse(json, symbolize_names: true))
+            x = new(location: hash[:location])
+            x.meta = Meta.from(hash[:meta])
+            x.id = hash[:id]
+            x.name = hash[:name]
+            x.description = hash[:description]
+            x.endpoint = hash[:endpoint]
+            x.schema = hash[:schema]
+            hash[:schemaExtensions].each do |ext|
+              x.add_schema_extension(
+                schema: ext[:schema],
+                required: ext[:required]
+              )
+            end
+            x
+          end
         end
       end
     end
spec/scim/kit/v2/resource_type_spec.rb
@@ -30,4 +30,20 @@ RSpec.describe Scim::Kit::V2::ResourceType do
 
     specify { expect(subject.to_h[:schemaExtensions]).to match_array([{ schema: extension, required: false }]) }
   end
+
+  describe '.parse' do
+    let(:extension) { 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User' }
+    let(:result) { described_class.parse(subject.to_json) }
+
+    before { subject.add_schema_extension(schema: extension, required: false) }
+
+    specify { expect(result.id).to eql(subject.id) }
+    specify { expect(result.name).to eql(subject.name) }
+    specify { expect(result.description).to eql(subject.description) }
+    specify { expect(result.endpoint).to eql(subject.endpoint) }
+    specify { expect(result.schema).to eql(subject.schema) }
+    specify { expect(result.schema_extensions).to eql(subject.schema_extensions) }
+    specify { expect(result.to_h).to eql(subject.to_h) }
+    specify { expect(result.to_json).to eql(subject.to_json) }
+  end
 end