Class: ApiMaker::Deserializer

Inherits:
ApplicationService show all
Defined in:
app/services/api_maker/deserializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#api_maker_json

Constructor Details

#initialize(arg:) ⇒ Deserializer

Returns a new instance of Deserializer.



4
5
6
# File 'app/services/api_maker/deserializer.rb', line 4

def initialize(arg:)
  @arg = arg
end

Instance Attribute Details

#argObject (readonly)

Returns the value of attribute arg.



2
3
4
# File 'app/services/api_maker/deserializer.rb', line 2

def arg
  @arg
end

Instance Method Details

#deserializeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/api_maker/deserializer.rb', line 12

def deserialize
  if arg.is_a?(Array)
    arg.map { |value| ApiMaker::Deserializer.execute!(arg: value) }
  elsif arg.is_a?(Hash) || arg.is_a?(ActionController::Parameters)
    if arg["api_maker_type"] == "resource"
      "Resources::#{arg.fetch("name")}Resource".safe_constantize
    elsif arg["api_maker_type"] == "datetime"
      Time.zone.parse(arg.fetch("value"))
    else
      new_hash = arg.class.new
      arg.each do |key, value|
        deserialized_key = ApiMaker::Deserializer.execute!(arg: key)
        deserialized_value = ApiMaker::Deserializer.execute!(arg: value)

        new_hash[deserialized_key] = deserialized_value
      end

      new_hash
    end
  else
    arg
  end
end

#performObject



8
9
10
# File 'app/services/api_maker/deserializer.rb', line 8

def perform
  succeed! deserialize
end