Class: ApiTransformer::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/api_transformer/endpoint.rb

Overview

Processes endpoint blocks

Instance Method Summary collapse

Constructor Details

#initialize(base_url, env, route) ⇒ Endpoint

Returns a new instance of Endpoint.



4
5
6
7
8
9
10
11
# File 'lib/api_transformer/endpoint.rb', line 4

def initialize(base_url, env, route)
  @base_url = base_url
  @env = env
  @route = route

  @backend_request_senders = []
  @backend_responses = {}
end

Instance Method Details

#completeObject



20
21
22
23
24
25
26
# File 'lib/api_transformer/endpoint.rb', line 20

def complete
  if @error
    complete_error
  else
    complete_success
  end
end

#request(name, options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_transformer/endpoint.rb', line 28

def request(name, options = {}, &block)
  return unless !options.key?(:when) || options[:when]

  @backend_request_senders << BackendRequestSender.new(
    name,
    options.merge(base_url: @base_url),
    block,
    @env["client-headers"],
    @route[:helper_blocks]
  )
end

#response(options = {}, &block) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/api_transformer/endpoint.rb', line 40

def response(options = {}, &block)
  @builder = FrontendResponseBuilder.new(
    @env,
    options,
    block,
    @route
  )
end

#runObject



13
14
15
16
17
18
# File 'lib/api_transformer/endpoint.rb', line 13

def run
  fail "Endpoints must define a response block" unless @builder

  send_requests
  send_response
end

#stream(value) ⇒ Object



49
50
51
# File 'lib/api_transformer/endpoint.rb', line 49

def stream(value)
  @stream = value
end