class ApiBlueprint::Blueprint

Public Instance Methods

all_request_options(options = {}) click to toggle source
# 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
connection() click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

build(from:, headers: {}, status: nil) click to toggle source
# File lib/api-blueprint/blueprint.rb, line 66
def build(from:, headers: {}, status: nil)
  builder_options = {
    body: parser.parse(from),
    headers: headers,
    status: status,
    replacements: replacements,
    creates: creates
  }

  builder.new(builder_options).build.tap do |built|
    set_errors built, builder_options[:body]
  end
end
call_api(options) click to toggle source
# File lib/api-blueprint/blueprint.rb, line 80
def call_api(options)
  connection.send options[:http_method] do |req|
    req.url options[:url]
    req.headers.merge!({ "Content-Type": "application/json" }.merge(options[:headers]))
    req.params = options[:params]
    req.body = options[:body].to_json if options[:body].present?
    req.options.timeout = timeout.to_i
    req.options.params_encoder = Faraday::FlatParamsEncoder
  end
end
enable_response_logging?() click to toggle source
# File lib/api-blueprint/blueprint.rb, line 110
def enable_response_logging?
  if defined?(Rails) && Rails.env.production?
    log_responses && ENV["ENABLE_PRODUCTION_RESPONSE_LOGGING"]
  else
    log_responses
  end
end
set_error(obj, field, messages) click to toggle source
# File lib/api-blueprint/blueprint.rb, line 106
def set_error(obj, field, messages)
  obj.errors.add field.to_sym, messages
end
set_errors(obj, body) click to toggle source
# File lib/api-blueprint/blueprint.rb, line 91
def set_errors(obj, body)
  if obj.respond_to?(:errors) && body.is_a?(Hash)
    errors = body.with_indifferent_access.fetch :errors, {}
    errors.each do |field, messages|
      if messages.is_a? Array
        messages.each do |message|
          set_error obj, field, message
        end
      else
        set_error obj, field, messages
      end
    end
  end
end