Class: HttpClientGenerator::ResourcesDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client_generator/resources_definition.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  content_type: :json
}.freeze
HTTP_VERBS =
i[get post put patch].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req_plugs = [], resp_plugs = []) ⇒ ResourcesDefinition

Returns a new instance of ResourcesDefinition.



13
14
15
16
17
# File 'lib/http_client_generator/resources_definition.rb', line 13

def initialize(req_plugs = [], resp_plugs = [])
  @resources = []
  @req_plugs = req_plugs
  @resp_plugs = resp_plugs
end

Instance Attribute Details

#resourcesObject (readonly)

Returns the value of attribute resources.



5
6
7
# File 'lib/http_client_generator/resources_definition.rb', line 5

def resources
  @resources
end

Instance Method Details

#namespace(_name, &block) ⇒ Object



47
48
49
50
51
# File 'lib/http_client_generator/resources_definition.rb', line 47

def namespace(_name, &block)
  namespaced_definition = ResourcesDefinition.new(req_plugs, resp_plugs)
  namespaced_definition.instance_eval(&block)
  @resources << namespaced_definition.resources
end

#req_plug(plug, *args, **kwargs) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/http_client_generator/resources_definition.rb', line 25

def req_plug(plug, *args, **kwargs)
  @req_plugs <<
    if plug.respond_to?(:call)
      plug
    elsif plug.respond_to?(:new)
      plug.new(*args, **kwargs)
    else
      Plugs.read(plug).new(*args, **kwargs)
    end
end

#resp_plug(plug, *args, **kwargs) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/http_client_generator/resources_definition.rb', line 36

def resp_plug(plug, *args, **kwargs)
  @resp_plugs <<
    if plug.respond_to?(:call)
      plug
    elsif plug.respond_to?(:new)
      plug.new(*args, **kwargs)
    else
      Plugs.read(plug).new(*args, **kwargs)
    end
end