Class: TreatanyoneCommonApi::ResponseBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/treatanyone_common_api/response_builder.rb

Constant Summary collapse

ACCEPTED_RESPONSE_CLASSES =
["Faraday::Response"].freeze

Instance Method Summary collapse

Constructor Details

#initialize(resp) ⇒ ResponseBuilder

Returns a new instance of ResponseBuilder.



5
6
7
# File 'lib/treatanyone_common_api/response_builder.rb', line 5

def initialize(resp)
  @resp = resp
end

Instance Method Details

#build(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/treatanyone_common_api/response_builder.rb', line 9

def build(&block)
  unless ACCEPTED_RESPONSE_CLASSES.include?(@resp.class.name)
    return {
      status: 400,
      body: "Response class not supported"
    }
  end

  hash = {
    statusCode: @resp.status,
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Credentials": true

    }
  }

  hash[:body] = if block_given?
                  yield(@resp, @resp.body)
                else
                  @resp.body
                end

  hash
end