Class: InfinumAzure::Resources::Params

Inherits:
Object
  • Object
show all
Defined in:
app/services/infinum_azure/resources/params.rb

Constant Summary collapse

NORMALIZATIONS =
{
  uid: :propagate,
  email: :propagate,
  first_name: :propagate,
  last_name: :propagate,
  avatar_url: :propagate,
  deactivated_at: {
    procedure: ->(value) { [false, nil].include?(value) ? nil : Time.zone.now },
    target_name: :deactivated
  },
  employee: {
    procedure: ->(value) { value&.include?('employees') },
    target_name: :groups
  },
  groups: :propagate
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Params

Returns a new instance of Params.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/infinum_azure/resources/params.rb', line 27

def initialize(params = {})
  NORMALIZATIONS.each do |attribute, operation|
    value = if operation == :propagate
              params[attribute]
            elsif operation.is_a?(Hash)
              operation[:procedure].call(params[operation[:target_name]])
            else
              raise 'unsupported normalization'
            end

    instance_variable_set(:"@#{attribute}", value)
  end
end

Class Method Details

.normalize(payload) ⇒ Object



23
24
25
# File 'app/services/infinum_azure/resources/params.rb', line 23

def self.normalize(payload)
  new(payload).as_json
end

Instance Method Details

#as_jsonObject



41
42
43
44
45
# File 'app/services/infinum_azure/resources/params.rb', line 41

def as_json
  NORMALIZATIONS.keys.index_with do |key|
    instance_variable_get(:"@#{key}")
  end
end