Module: Clientura::Client::ClassMethods

Defined in:
lib/clientura/client.rb

Instance Method Summary collapse

Instance Method Details

#middleware(name, callable) ⇒ Object



58
59
60
# File 'lib/clientura/client.rb', line 58

def middleware(name, callable)
  registered_middleware[name] = callable
end

#normalize_mapper(mapper) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/clientura/client.rb', line 80

def normalize_mapper(mapper)
  case mapper
  when Array
    name, *config = mapper
    { name: name, config: config }
  else
    { name: mapper, config: [] }
  end
end

#normalize_path(path) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/clientura/client.rb', line 31

def normalize_path(path)
  if path.respond_to?(:call)
    path
  else
    -> (_) { path }
  end
end

#pipe(name, callable) ⇒ Object



54
55
56
# File 'lib/clientura/client.rb', line 54

def pipe(name, callable)
  registered_pipes[name] = callable
end

#pipe_through(*pipes) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/clientura/client.rb', line 62

def pipe_through(*pipes)
  pipes = pipes.map { |o| normalize_mapper o }

  @pipes_context ||= []
  @pipes_context.push(*pipes)
  yield
  @pipes_context.pop pipes.count
end

#register_endpoint(name, verb:, path:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clientura/client.rb', line 39

def register_endpoint(name, verb:, path:)
  registered_endpoints[name] = Endpoint.new verb,
                                            normalize_path(path),
                                            [*@middleware_context],
                                            [*@pipes_context]

  define_method name do |args = {}|
    call_endpoint name, args
  end

  define_method "#{name}_promise" do |args = {}|
    Concurrent::Promise.execute { send(name, args) }
  end
end

#registered_endpointsObject



13
14
15
# File 'lib/clientura/client.rb', line 13

def registered_endpoints
  @registered_endpoints ||= {}
end

#registered_middlewareObject



21
22
23
# File 'lib/clientura/client.rb', line 21

def registered_middleware
  @registered_middleware ||= {}
end

#registered_pipesObject



17
18
19
# File 'lib/clientura/client.rb', line 17

def registered_pipes
  @registered_pipes ||= {}
end

#use_middleware(*middleware) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/clientura/client.rb', line 71

def use_middleware(*middleware)
  middleware = middleware.map { |o| normalize_mapper o }

  @middleware_context ||= []
  @middleware_context.push(*middleware)
  yield
  @middleware_context.pop middleware.count
end