Commit f207c6d

mo <mo.khan@gmail.com>
2019-01-06 02:32:12
start to design resource
1 parent 81f022c
lib/scim/kit/v2/templates/meta.json.jbuilder
@@ -5,4 +5,4 @@ json.location location
 json.resource_type resource_type
 json.created created.iso8601 if created
 json.last_modified last_modified.iso8601 if last_modified
-json.version version.to_i if version
+json.version version if version
lib/scim/kit/v2/templates/resource.json.jbuilder
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+json.key_format! camelize: :lower
+json.id id
+json.external_id external_id
+json.meta do
+  render meta, json: json
+end
lib/scim/kit/v2/meta.rb
@@ -14,7 +14,8 @@ module Scim
         def initialize(resource_type, location)
           @resource_type = resource_type
           @location = location
-          @version = @created = @last_modified = Time.now
+          @created = @last_modified = Time.now
+          @version = @created.to_i
         end
       end
     end
lib/scim/kit/v2/resource.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module Scim
+  module Kit
+    module V2
+      # Represents a SCIM Schema
+      class Resource
+        include Templatable
+        attr_accessor :id, :external_id
+        attr_reader :meta
+
+        def initialize(schema:, location:)
+          @meta = Meta.new(schema.id, location)
+        end
+      end
+    end
+  end
+end
lib/scim/kit.rb
@@ -13,6 +13,7 @@ require 'scim/kit/v2/authentication_scheme'
 require 'scim/kit/v2/messages'
 require 'scim/kit/v2/meta'
 require 'scim/kit/v2/mutability'
+require 'scim/kit/v2/resource'
 require 'scim/kit/v2/resource_type'
 require 'scim/kit/v2/returned'
 require 'scim/kit/v2/schema'
spec/scim/kit/v2/resource_spec.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+RSpec.describe Scim::Kit::V2::Resource do
+  subject { described_class.new(schema: schema, location: resource_location) }
+  let(:schema) { Scim::Kit::V2::Schema.new(id: 'User', name: 'User', location: FFaker::Internet.uri('https')) }
+  let(:resource_location) { FFaker::Internet.uri('https') }
+
+  context "with common attributes" do
+    let(:id) { SecureRandom.uuid }
+    let(:external_id) { SecureRandom.uuid }
+    let(:created_at) { Time.now }
+    let(:updated_at) { Time.now }
+    let(:version) { SecureRandom.uuid }
+
+    before do
+      subject.id = id
+      subject.external_id = external_id
+      subject.meta.created = created_at
+      subject.meta.last_modified = updated_at
+      subject.meta.version = version
+    end
+
+    specify { expect(subject.id).to eql(id) }
+    specify { expect(subject.external_id).to eql(external_id) }
+    specify { expect(subject.meta.resource_type).to eql('User') }
+    specify { expect(subject.meta.location).to eql(resource_location) }
+    specify { expect(subject.meta.created).to eql(created_at) }
+    specify { expect(subject.meta.last_modified).to eql(updated_at) }
+    specify { expect(subject.meta.version).to eql(version) }
+
+    describe "#as_json" do
+      specify { expect(subject.as_json[:id]).to eql(id) }
+      specify { expect(subject.as_json[:externalId]).to eql(external_id) }
+      specify { expect(subject.as_json[:meta][:resourceType]).to eql('User') }
+      specify { expect(subject.as_json[:meta][:location]).to eql(resource_location) }
+      specify { expect(subject.as_json[:meta][:created]).to eql(created_at.iso8601) }
+      specify { expect(subject.as_json[:meta][:lastModified]).to eql(updated_at.iso8601) }
+      specify { expect(subject.as_json[:meta][:version]).to eql(version) }
+    end
+  end
+end