Class: Dryer::Clients::GeneratedClients::Resources::Create

Inherits:
Services::SimpleService
  • Object
show all
Defined in:
lib/dryer/clients/generated_clients/resources/create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Create

Returns a new instance of Create.



11
12
13
# File 'lib/dryer/clients/generated_clients/resources/create.rb', line 11

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



65
66
67
# File 'lib/dryer/clients/generated_clients/resources/create.rb', line 65

def config
  @config
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dryer/clients/generated_clients/resources/create.rb', line 15

def call
  config[:actions].inject(Class.new(Resource)) do |resource, (action_name, action_config)|

    # if I don't set a variable to the config it is not in scope
    # for the method definition
    resource_config = config
    request_contract = get_contract(action_config[:request_contract])
    headers_contract = get_contract(action_config[:headers_contract])
    url_parameters_contract = get_contract(action_config[:url_parameters_contract])
    response_contracts = get_contracts(action_config[:response_contracts])

    resource.send(
      :define_method,
      action_name.to_sym
    ) do |*path_variables, **options|
      headers = options[:headers] || {}
      body = options[:body] || {}
      url_parameters = options[:url_parameters] || {}

      Requests::Validate.call(
        path_variables: path_variables,
        headers: headers,
        body: body,
        url_parameters: url_parameters,
        request_contract: request_contract,
        headers_contract: headers_contract,
        url_parameters_contract: url_parameters_contract,
        path: action_config[:url] || resource_config[:url]
      ).bind do |_|
        raw_response = Request.new(
          base_url: self.base_url,
          method: action_config[:method],
          path: action_config[:url] || resource_config[:url],
          path_variables: path_variables,
          headers: headers,
          body: body,
        ).send

        Responses::Create.call(
          raw_response: raw_response,
          response_contracts: action_config[:response_contracts]
        )
      rescue URI::InvalidURIError => e
        Dry::Monads::Failure(e)
      end
    end
    resource
  end
end

#get_contract(contract) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/dryer/clients/generated_clients/resources/create.rb', line 73

def get_contract(contract)
  case contract
  when String
    Module.const_get(contract)
  when Class
    contract
  end
end

#get_contracts(hash) ⇒ Object



67
68
69
70
71
# File 'lib/dryer/clients/generated_clients/resources/create.rb', line 67

def get_contracts(hash)
  hash.inject({}) do |acc, (k,v)|
    acc[k] = get_contract(v)
  end
end