main
 1# frozen_string_literal: true
 2
 3module Scim
 4  module Kit
 5    module V2
 6      # Represents a scim Service Provider Configuration
 7      class ServiceProviderConfiguration
 8        include Templatable
 9        attr_accessor :bulk, :filter
10        attr_accessor :etag, :sort, :change_password, :patch
11        attr_accessor :meta, :documentation_uri
12        attr_accessor :authentication_schemes
13
14        def initialize(
15          location:,
16          meta: Meta.new('ServiceProviderConfig', location)
17        )
18          @meta = meta
19          @authentication_schemes = []
20          @etag = Supportable.new
21          @sort = Supportable.new
22          @change_password = Supportable.new
23          @patch = Supportable.new
24          @bulk = Supportable.new(:max_operations, :max_payload_size)
25          @filter = Supportable.new(:max_results)
26        end
27
28        def add_authentication(type, primary: nil)
29          scheme = AuthenticationScheme.build_for(type, primary: primary)
30          yield scheme if block_given?
31          @authentication_schemes << scheme
32        end
33
34        class << self
35          def parse(json, hash = JSON.parse(json, symbolize_names: true))
36            x = new(location: hash[:location], meta: Meta.from(hash[:meta]))
37            x.documentation_uri = hash[:documentationUri]
38            %i[patch changePassword sort etag filter bulk].each do |key|
39              x.send("#{key.to_s.underscore}=", Supportable.from(hash[key]))
40            end
41            schemes = hash[:authenticationSchemes]
42            x.authentication_schemes = schemes&.map do |auth|
43              AuthenticationScheme.from(auth)
44            end
45            x
46          end
47        end
48      end
49    end
50  end
51end