Module: HttpClientGenerator

Defined in:
lib/http_client_generator.rb,
lib/http_client_generator/plugs.rb,
lib/http_client_generator/request.rb,
lib/http_client_generator/version.rb,
lib/http_client_generator/resource.rb,
lib/http_client_generator/plugs/camelize_body.rb,
lib/http_client_generator/plugs/set_request_id.rb,
lib/http_client_generator/resources_definition.rb,
lib/http_client_generator/plugs/underscore_response.rb

Defined Under Namespace

Classes: Plugs, Request, Resource, ResourcesDefinition

Constant Summary collapse

VERSION =
'0.1.2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/http_client_generator.rb', line 8

def self.included(base)
  super

  base.extend(self)
  base.const_set(:Error, Class.new(StandardError))
  base.const_set(:RequestError, Class.new(base::Error))
end

Instance Method Details

#configure(&configure) ⇒ Object



34
35
36
37
38
39
# File 'lib/http_client_generator.rb', line 34

def configure(&configure)
  @config = self::Configuration.new
    .tap(&configure)
    .tap { |c| user_process_config(c) if respond_to?(:user_process_config) }
    .tap(&:freeze)
end

#process_config(&block) ⇒ Object



41
42
43
# File 'lib/http_client_generator.rb', line 41

def process_config(&block)
  define_singleton_method(:user_process_config, block)
end

#resources(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/http_client_generator.rb', line 20

def resources(&block)
  resources_definition = ResourcesDefinition.new
  resources_definition.instance_eval(&block)
  resources = resources_definition.resources

  resources.each do |resource|
    method_name = :"#{resource.name}_#{resource.verb}"

    define_singleton_method(method_name) do |url_options = {}, body: nil, **rest_args|
      resource.perform_request(@url_helper, @base, url_options, body, rest_args)
    end
  end
end

#url_helper(module_name) ⇒ Object



16
17
18
# File 'lib/http_client_generator.rb', line 16

def url_helper(module_name)
  @url_helper = module_name
end