Commit 80767b2

mo <mo.khan@gmail.com>
2018-01-14 19:46:45
parse the user resource.
1 parent 4c7efd1
lib/scim/shady/builders/resource.rb
@@ -0,0 +1,27 @@
+module Scim
+  module Shady
+    module Builders
+      class Resource
+        attr_accessor :id
+        attr_accessor :created_at
+        attr_accessor :updated_at
+        attr_accessor :location
+        attr_accessor :version
+
+        def to_h
+          {
+            'schemas' => [],
+            'id' => id,
+            'meta' => {
+              'resourceType' => self.class.name.split(/::/).last,
+              'created' => created_at.utc.iso8601,
+              'lastModified' => updated_at.utc.iso8601,
+              'location' => location,
+              'version' => version,
+            },
+          }
+        end
+      end
+    end
+  end
+end
lib/scim/shady/builders/user.rb
@@ -0,0 +1,24 @@
+module Scim
+  module Shady
+    module Builders
+      class User < Resource
+        attr_accessor :username
+
+        def build
+          Scim::Shady::User.new(to_json)
+        end
+
+        def to_json
+          JSON.generate(to_h)
+        end
+
+        def to_h
+          super.merge({
+            'schemas' => [Schemas::USER],
+            'userName' => username,
+          })
+        end
+      end
+    end
+  end
+end
lib/scim/shady/builders.rb
@@ -0,0 +1,2 @@
+require "scim/shady/builders/resource"
+require "scim/shady/builders/user"
lib/scim/shady/resource.rb
@@ -1,25 +1,42 @@
 module Scim
   module Shady
     class Resource
-      attr_accessor :id
-      attr_accessor :created_at
-      attr_accessor :updated_at
-      attr_accessor :location
-      attr_accessor :version
+      def initialize(json)
+        @json = json
+      end
+
+      def id
+        to_h['id']
+      end
+
+      def username
+        to_h['userName']
+      end
+
+      def created
+        DateTime.parse(to_h['meta']['created'])
+      end
+
+      def last_modified
+        DateTime.parse(to_h['meta']['lastModified'])
+      end
+
+      def version
+        to_h['meta']['version']
+      end
+
+      def location
+        to_h['meta']['location']
+      end
 
       def to_h
-        {
-          'schemas' => [],
-          'id' => id,
-          'meta' => {
-            'resourceType' => self.class.name.split(/::/).last,
-            'created' => created_at.utc.iso8601,
-            'lastModified' => updated_at.utc.iso8601,
-            'location' => location,
-            'version' => version,
-          },
-        }
+        JSON.parse(to_json)
+      end
+
+      def to_json
+        @json
       end
+
     end
   end
 end
lib/scim/shady/user.rb
@@ -1,27 +1,21 @@
 module Scim
   module Shady
     class User < Resource
-      attr_accessor :username
-
-      def to_h
-        super.merge({
-          'schemas' => [Schemas::USER],
-          'userName' => username,
-        })
-      end
-
       class << self
         def build
-          resource = new
-          yield resource
-          resource.to_h
+          builder do |builder|
+            yield builder if block_given?
+          end.build
+        end
+
+        def builder
+          builder = builder_class.new
+          yield builder if block_given?
+          builder
         end
 
-        def build_json
-          hash = build do |resource|
-            yield resource
-          end
-          JSON.dump(hash)
+        def builder_class
+          Scim::Shady::Builders::User
         end
       end
     end
lib/scim/shady.rb
@@ -1,6 +1,7 @@
 require "json"
 require "time"
 
+require "scim/shady/builders"
 require "scim/shady/resource"
 require "scim/shady/schemas"
 require "scim/shady/user"
spec/scim/user_spec.rb
@@ -1,12 +1,13 @@
 RSpec.describe Scim::Shady::User do
+  let(:id) { SecureRandom.uuid }
+  let(:created_at) { Time.now }
+  let(:updated_at) { Time.now }
+  let(:user_url) { FFaker::Internet.uri("https") }
+  let(:user_version) { SecureRandom.uuid }
+  let(:username) { FFaker::Internet.user_name }
+
   describe ".build" do
     subject { described_class }
-    let(:id) { SecureRandom.uuid }
-    let(:created_at) { Time.now }
-    let(:updated_at) { Time.now }
-    let(:user_url) { FFaker::Internet.uri("https") }
-    let(:user_version) { SecureRandom.uuid }
-    let(:username) { FFaker::Internet.user_name }
 
     it 'builds a scim user' do
       result = subject.build do |builder|
@@ -16,7 +17,7 @@ RSpec.describe Scim::Shady::User do
         builder.updated_at = updated_at
         builder.location = user_url
         builder.version = user_version
-      end
+      end.to_h
 
       expect(result['schemas']).to match_array([Scim::Shady::Schemas::USER])
       expect(result['id']).to eql(id)
@@ -27,16 +28,20 @@ RSpec.describe Scim::Shady::User do
       expect(result['meta']['location']).to eql(user_url)
       expect(result['meta']['version']).to eql(user_version)
     end
+  end
+
+  describe ".build_json" do
+    subject { described_class }
 
     it 'produces valid json' do
-      result = subject.build_json do |builder|
+      result = subject.build do |builder|
         builder.id = id
         builder.username = username
         builder.created_at = created_at
         builder.updated_at = updated_at
         builder.location = user_url
         builder.version = user_version
-      end
+      end.to_json
       result = JSON.parse(result)
 
       expect(result['schemas']).to match_array([Scim::Shady::Schemas::USER])
@@ -49,4 +54,29 @@ RSpec.describe Scim::Shady::User do
       expect(result['meta']['version']).to eql(user_version)
     end
   end
+
+  describe ".new" do
+    it 'parses json' do
+      json = JSON.dump({
+        schemas: [Scim::Shady::Schemas::USER],
+        id: id,
+        userName: username,
+        meta: {
+          resourceType: "User",
+          created: created_at.iso8601,
+          lastModified: updated_at.iso8601,
+          version: user_version,
+          location: user_url,
+        }
+      })
+      subject = described_class.new(json)
+
+      expect(subject.id).to eql(id)
+      expect(subject.username).to eql(username)
+      expect(subject.created.iso8601).to eql(created_at.iso8601)
+      expect(subject.last_modified.iso8601).to eql(updated_at.iso8601)
+      expect(subject.version).to eql(user_version)
+      expect(subject.location).to eql(user_url)
+    end
+  end
 end