Class: ApiBlueprint::Blueprint

Inherits:
Struct
  • Object
show all
Defined in:
lib/api-blueprint/blueprint.rb

Instance Method Summary collapse

Methods inherited from Struct

new

Instance Method Details

#all_request_options(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/api-blueprint/blueprint.rb', line 16

def all_request_options(options = {})
  {
    http_method: http_method,
    url: url,
    headers: headers.merge(options.fetch(:headers, {})),
    params: params.merge(options.fetch(:params, {})),
    body: body.merge(options.fetch(:body, {}))
  }
end

#connectionObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/api-blueprint/blueprint.rb', line 47

def connection
  Faraday.new do |conn|
    conn.use ApiBlueprint::ResponseMiddleware
    conn.response :json, content_type: /\bjson$/
    conn.use :instrumentation, name: "api-blueprint.request"

    if enable_response_logging?
      conn.response :detailed_logger, ApiBlueprint.config.logger, "API-BLUEPRINT"
    end

    conn.adapter Faraday.default_adapter
    conn.headers = {
      "User-Agent": "ApiBlueprint"
    }
  end
end

#run(options = {}, runner = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/api-blueprint/blueprint.rb', line 26

def run(options = {}, runner = nil)
  if options.delete :validate
    result = build from: options[:body]
    return result.errors if result.invalid?
  end

  response = call_api all_request_options(options)

  if creates.present?
    created = build from: response.body, headers: response.headers, status: response.status
  else
    created = response
  end

  after_build.present? ? after_build.call(runner, created) : created
rescue Faraday::ConnectionFailed
  raise ApiBlueprint::ConnectionFailed
rescue Faraday::TimeoutError
  raise ApiBlueprint::TimeoutError
end