Commit 3b5ea7c

mo <mo.khan@gmail.com>
2018-12-25 03:26:53
use tilt and jbuilder
1 parent 7b98c31
lib/scim/kit/v2/templates/schema.json.jbuilder
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+json.id id
+json.name name
+json.description description
+json.meta do
+  json.resourceType 'Schema'
+  json.location location
+end
+json.attributes attributes.map(&:to_h)
lib/scim/kit/v2/attribute_type.rb
@@ -36,7 +36,6 @@ module Scim
           @returned = Returned::DEFAULT
           @uniqueness = Uniqueness::NONE
           @attributes = []
-
           raise ArgumentError, :type unless DATATYPES[type.to_sym]
         end
 
lib/scim/kit/v2/schema.rb
@@ -10,7 +10,7 @@ module Scim
         RESOURCE_TYPE = 'urn:ietf:params:scim:schemas:core:2.0:ResourceType'
         USER = 'urn:ietf:params:scim:schemas:core:2.0:User'
 
-        attr_reader :id, :name, :location
+        attr_reader :id, :name, :location, :attributes
         attr_accessor :description
 
         def initialize(id:, name:, location:)
@@ -27,20 +27,7 @@ module Scim
         end
 
         def to_json
-          JSON.generate(to_h)
-        end
-
-        def to_h
-          {
-            id: id,
-            name: name,
-            description: description,
-            meta: {
-              location: location,
-              resourceType: 'Schema'
-            },
-            attributes: @attributes.map(&:to_h)
-          }
+          Template.new(self).to_json
         end
       end
     end
lib/scim/kit/template.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+module Scim
+  module Kit
+    # Represents a Jbuilder template
+    class Template
+      TEMPLATES_DIR = Pathname.new(File.join(__dir__, 'v2/templates/'))
+
+      attr_reader :target
+
+      def initialize(target)
+        @target = target
+      end
+
+      def to_json(options = {})
+        template.render(target, options)
+      end
+
+      private
+
+      def template_path
+        TEMPLATES_DIR.join(template_name)
+      end
+
+      def template_name
+        "#{target.class.name.split('::').last.underscore}.json.jbuilder"
+      end
+
+      def template
+        @template ||= Tilt.new(template_path.to_s)
+      end
+    end
+  end
+end
lib/scim/kit.rb
@@ -1,4 +1,6 @@
 # frozen_string_literal: true
+require 'tilt'
+require 'tilt/jbuilder'
 
 require 'scim/kit/version'
 require 'scim/kit/v2/attribute_type'
@@ -7,6 +9,7 @@ require 'scim/kit/v2/uniqueness'
 require 'scim/kit/v2/mutability'
 require 'scim/kit/v2/schema'
 require 'scim/kit/v2/resource_type'
+require 'scim/kit/template'
 
 module Scim
   module Kit
.rubocop.yml
@@ -11,6 +11,7 @@ AllCops:
 
 Metrics/BlockLength:
   Exclude:
+    - '*.gemspec'
     - 'spec/**/*.rb'
 
 Metrics/LineLength:
scim-kit.gemspec
@@ -28,6 +28,8 @@ Gem::Specification.new do |spec|
   end
   spec.require_paths = ['lib']
 
+  spec.add_dependency 'tilt', '~> 2.0'
+  spec.add_dependency 'tilt-jbuilder', '~> 0.7'
   spec.add_development_dependency 'bundler', '~> 1.17'
   spec.add_development_dependency 'bundler-audit', '~> 0.6'
   spec.add_development_dependency 'ffaker', '~> 2.7'