scim-kit 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac439680a326531f8bb77df0501d11f8d36b3b5cb6f3a26623c21729fd20cee1
4
- data.tar.gz: 0fa1382a12a178b1e2f9e3ee4e9ba1f4ea2b7457515a6511832e937a318ea90c
3
+ metadata.gz: 7c3b973655a3edf5be7d0be213b847b21b7d92a8262a20f9f832f2a10be7097b
4
+ data.tar.gz: d9da0db47fd0e17964961cee7cc22d8a013d66466c2f47b8fd6dd913c2b0a233
5
5
  SHA512:
6
- metadata.gz: 9411c5db41027d8ad20065ac76d7e6d9bab7c20dd31b42f2c5600bf1ca1952946c55970a90be1688f835e79fc4c37ef1a5d05d69c2c7b538696a63e810d61e43
7
- data.tar.gz: 07a69f09dc046a851cd3380b011b7d637b772af360209d78f4a2c7fd87eca764ff5026e0decece19baf78e1c46530c7c015e7913fbdee9d4386e75171ce926e9
6
+ metadata.gz: c846c9afa7135dace4e4f88659a6a5d6a77c3ca981ca1261087eebfd4315c1517558e05560f4ffa1c5ccdf169feede591f213e8ddde69e8cca6f4e4a61668b49
7
+ data.tar.gz: e8ac0b13d604ab5ee0546a116092b409777b8dbc23d16892798d16e0aba232752c638ccb91b99f36c256a30c6ee89beb56a61bf1d910fa45908a991d7e70bf0c
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,11 @@
1
+ image: ruby:2.6
2
+
3
+ before_script:
4
+ - apt-get update && apt-get install -y locales
5
+ - echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
6
+ - locale-gen
7
+ - export LC_ALL=en_US.UTF-8
8
+
9
+ rspec:
10
+ script:
11
+ - bin/cibuild
data/README.md CHANGED
@@ -83,7 +83,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
83
83
 
84
84
  ## Contributing
85
85
 
86
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/scim-kit.
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mokha/scim-kit.
87
87
 
88
88
  ## License
89
89
 
data/bin/console CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'bundler/setup'
5
- require 'saml/kit'
5
+ require 'scim/kit'
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
data/bin/setup CHANGED
@@ -3,4 +3,4 @@ set -euo pipefail
3
3
  IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
- bundle check || bundle install --jobs $(nproc)
6
+ bundle check || bundle install --jobs "$(sysctl -n hw.ncpu || nproc)"
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Scim
4
4
  module Kit
5
- # Allows dynamic assignment of attributes.
5
+ # Allows dynamic creation of attributes.
6
6
  module DynamicAttributes
7
7
  def method_missing(method, *args)
8
8
  return super unless respond_to_missing?(method)
@@ -13,7 +13,7 @@ module Scim
13
13
  end
14
14
 
15
15
  def to_h
16
- JSON.parse(to_json, symbolize_names: true)
16
+ JSON.parse(to_json, symbolize_names: true).with_indifferent_access
17
17
  end
18
18
 
19
19
  def render(model, options)
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scim
4
+ module Kit
5
+ module V2
6
+ # Represents a dynamic attribute that corresponds to a SCIM type
7
+ module Attributable
8
+ attr_reader :dynamic_attributes
9
+
10
+ def define_attributes_for(types)
11
+ @dynamic_attributes = {}.with_indifferent_access
12
+ types.each do |type|
13
+ dynamic_attributes[type.name] = Attribute.new(type: type)
14
+ extend(create_module_for(type))
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def attribute_for(name)
21
+ dynamic_attributes[name]
22
+ end
23
+
24
+ def read_attribute(name)
25
+ attribute = attribute_for(name)
26
+ return attribute._value if attribute.type.multi_valued
27
+
28
+ attribute.type.complex? ? attribute : attribute._value
29
+ end
30
+
31
+ def write_attribute(name, value)
32
+ attribute_for(name)._value = value
33
+ end
34
+
35
+ def create_module_for(type)
36
+ name = type.name.to_sym
37
+ Module.new do
38
+ define_method(name) do |*_args|
39
+ read_attribute(name)
40
+ end
41
+
42
+ define_method("#{name}=") do |*args|
43
+ write_attribute(name, args[0])
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scim
4
+ module Kit
5
+ module V2
6
+ # Represents a SCIM Attribute
7
+ class Attribute
8
+ include Attributable
9
+ include Templatable
10
+ attr_reader :type
11
+ attr_reader :_value
12
+
13
+ def initialize(type:, value: nil)
14
+ @type = type
15
+ @_value = value
16
+ define_attributes_for(type.attributes)
17
+ end
18
+
19
+ def _value=(new_value)
20
+ @_value = type.coerce(new_value)
21
+
22
+ if type.canonical_values &&
23
+ !type.canonical_values.empty? &&
24
+ !type.canonical_values.include?(new_value)
25
+ raise ArgumentError, new_value
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -16,6 +16,13 @@ module Scim
16
16
  reference: 'reference',
17
17
  complex: 'complex'
18
18
  }.freeze
19
+ COERCION = {
20
+ string: ->(x) { x.to_s },
21
+ decimal: ->(x) { x.to_f },
22
+ integer: ->(x) { x.to_i },
23
+ datetime: ->(x) { x.is_a?(::String) ? DateTime.parse(x) : x },
24
+ binary: ->(x) { Base64.strict_encode64(x) }
25
+ }.freeze
19
26
  attr_accessor :canonical_values
20
27
  attr_accessor :case_exact
21
28
  attr_accessor :description
@@ -28,8 +35,8 @@ module Scim
28
35
  attr_reader :uniqueness
29
36
 
30
37
  def initialize(name:, type: :string)
31
- @name = name
32
- @type = type
38
+ @name = name.to_s.underscore
39
+ @type = type.to_sym
33
40
  @description = ''
34
41
  @multi_valued = false
35
42
  @required = false
@@ -37,7 +44,7 @@ module Scim
37
44
  @mutability = Mutability::READ_WRITE
38
45
  @returned = Returned::DEFAULT
39
46
  @uniqueness = Uniqueness::NONE
40
- raise ArgumentError, :type unless DATATYPES[type.to_sym]
47
+ raise ArgumentError, :type unless DATATYPES[@type]
41
48
  end
42
49
 
43
50
  def mutability=(value)
@@ -53,9 +60,9 @@ module Scim
53
60
  end
54
61
 
55
62
  def add_attribute(name:, type: :string)
56
- @type = :complex
57
63
  attribute = AttributeType.new(name: name, type: type)
58
64
  yield attribute if block_given?
65
+ @type = :complex
59
66
  attributes << attribute
60
67
  end
61
68
 
@@ -64,8 +71,6 @@ module Scim
64
71
  @reference_types = value
65
72
  end
66
73
 
67
- private
68
-
69
74
  def attributes
70
75
  @attributes ||= []
71
76
  end
@@ -74,6 +79,18 @@ module Scim
74
79
  type_is?(:complex)
75
80
  end
76
81
 
82
+ def coerce(value)
83
+ if type_is?(:boolean) && ![true, false].include?(value)
84
+ raise ArgumentError, value
85
+ end
86
+ return value if multi_valued
87
+
88
+ coercion = COERCION[type]
89
+ coercion ? coercion.call(value) : value
90
+ end
91
+
92
+ private
93
+
77
94
  def string?
78
95
  type_is?(:string)
79
96
  end
@@ -14,7 +14,8 @@ module Scim
14
14
  def initialize(resource_type, location)
15
15
  @resource_type = resource_type
16
16
  @location = location
17
- @version = @created = @last_modified = Time.now
17
+ @created = @last_modified = Time.now
18
+ @version = @created.to_i
18
19
  end
19
20
  end
20
21
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Scim
4
+ module Kit
5
+ module V2
6
+ # Represents a SCIM Resource
7
+ class Resource
8
+ include Attributable
9
+ include Templatable
10
+
11
+ attr_accessor :id, :external_id
12
+ attr_reader :meta
13
+ attr_reader :schemas
14
+
15
+ def initialize(schemas:, location:)
16
+ @meta = Meta.new(schemas[0].name, location)
17
+ @schemas = schemas
18
+ schemas.each do |schema|
19
+ define_attributes_for(schema.attributes)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -20,7 +20,11 @@ module Scim
20
20
  def add_attribute(name:, type: :string)
21
21
  attribute = AttributeType.new(name: name, type: type)
22
22
  yield attribute if block_given?
23
- @attributes << attribute
23
+ attributes << attribute
24
+ end
25
+
26
+ def core?
27
+ id.include?(Schemas::CORE)
24
28
  end
25
29
 
26
30
  def self.build(*args)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ json.key_format! camelize: :lower
4
+ if type.complex? && !type.multi_valued
5
+ json.set! type.name do
6
+ dynamic_attributes.values.each do |attribute|
7
+ render attribute, json: json
8
+ end
9
+ end
10
+ else
11
+ json.set! type.name, _value
12
+ end
@@ -5,4 +5,4 @@ json.location location
5
5
  json.resource_type resource_type
6
6
  json.created created.iso8601 if created
7
7
  json.last_modified last_modified.iso8601 if last_modified
8
- json.version version.to_i if version
8
+ json.version version if version
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ json.key_format! camelize: :lower
4
+ json.schemas schemas.map(&:id)
5
+ json.id id
6
+ json.external_id external_id
7
+ json.meta do
8
+ render meta, json: json
9
+ end
10
+ schemas.each do |schema|
11
+ if schema.core?
12
+ schema.attributes.each do |type|
13
+ render dynamic_attributes[type.name], json: json
14
+ end
15
+ else
16
+ json.set! schema.id do
17
+ schema.attributes.each do |type|
18
+ render dynamic_attributes[type.name], json: json
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Scim
4
4
  module Kit
5
- VERSION = '0.2.1'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  end
data/lib/scim/kit.rb CHANGED
@@ -2,17 +2,21 @@
2
2
 
3
3
  require 'tilt'
4
4
  require 'tilt/jbuilder'
5
+ require 'active_support/core_ext/hash/indifferent_access'
5
6
 
6
7
  require 'scim/kit/dynamic_attributes'
7
8
  require 'scim/kit/templatable'
8
9
  require 'scim/kit/template'
9
10
  require 'scim/kit/version'
10
11
 
12
+ require 'scim/kit/v2/attributable'
13
+ require 'scim/kit/v2/attribute'
11
14
  require 'scim/kit/v2/attribute_type'
12
15
  require 'scim/kit/v2/authentication_scheme'
13
16
  require 'scim/kit/v2/messages'
14
17
  require 'scim/kit/v2/meta'
15
18
  require 'scim/kit/v2/mutability'
19
+ require 'scim/kit/v2/resource'
16
20
  require 'scim/kit/v2/resource_type'
17
21
  require 'scim/kit/v2/returned'
18
22
  require 'scim/kit/v2/schema'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scim-kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-06 00:00:00.000000000 Z
11
+ date: 2019-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tilt
@@ -159,6 +159,7 @@ extensions: []
159
159
  extra_rdoc_files: []
160
160
  files:
161
161
  - ".gitignore"
162
+ - ".gitlab-ci.yml"
162
163
  - ".rspec"
163
164
  - ".rubocop.yml"
164
165
  - ".travis.yml"
@@ -177,21 +178,26 @@ files:
177
178
  - lib/scim/kit/dynamic_attributes.rb
178
179
  - lib/scim/kit/templatable.rb
179
180
  - lib/scim/kit/template.rb
181
+ - lib/scim/kit/v2/attributable.rb
182
+ - lib/scim/kit/v2/attribute.rb
180
183
  - lib/scim/kit/v2/attribute_type.rb
181
184
  - lib/scim/kit/v2/authentication_scheme.rb
182
185
  - lib/scim/kit/v2/messages.rb
183
186
  - lib/scim/kit/v2/meta.rb
184
187
  - lib/scim/kit/v2/mutability.rb
188
+ - lib/scim/kit/v2/resource.rb
185
189
  - lib/scim/kit/v2/resource_type.rb
186
190
  - lib/scim/kit/v2/returned.rb
187
191
  - lib/scim/kit/v2/schema.rb
188
192
  - lib/scim/kit/v2/schemas.rb
189
193
  - lib/scim/kit/v2/service_provider_configuration.rb
190
194
  - lib/scim/kit/v2/supportable.rb
195
+ - lib/scim/kit/v2/templates/attribute.json.jbuilder
191
196
  - lib/scim/kit/v2/templates/attribute_type.json.jbuilder
192
197
  - lib/scim/kit/v2/templates/authentication_scheme.json.jbuilder
193
198
  - lib/scim/kit/v2/templates/meta.json.jbuilder
194
199
  - lib/scim/kit/v2/templates/nil_class.json.jbuilder
200
+ - lib/scim/kit/v2/templates/resource.json.jbuilder
195
201
  - lib/scim/kit/v2/templates/resource_type.json.jbuilder
196
202
  - lib/scim/kit/v2/templates/schema.json.jbuilder
197
203
  - lib/scim/kit/v2/templates/service_provider_configuration.json.jbuilder