Class: RestAPIBuilder::RequestOptions

Inherits:
Object
  • Object
show all
Includes:
UrlHelper
Defined in:
lib/rest_api_builder/request_options.rb

Instance Method Summary collapse

Methods included from UrlHelper

#full_url

Instance Method Details

#compose(base_url:, method:, path: nil, body: nil, headers: {}, query: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rest_api_builder/request_options.rb', line 7

def compose(base_url:, method:, path: nil, body: nil, headers: {}, query: nil)
  if method == :get && body
    raise ArgumentError, 'GET requests do not support body'
  end

  headers = headers.merge(params: query) if query

  {
    method: method,
    url: full_url(base_url, path),
    payload: body,
    headers: headers
  }
end

#compose_json(**options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/rest_api_builder/request_options.rb', line 22

def compose_json(**options)
  result = compose(**options)
  headers = result[:headers]
  payload = result[:payload]

  result.merge(
    headers: headers.merge(content_type: :json),
    payload: payload && JSON.generate(payload)
  )
end