module ScormEngine::Faraday::Request

Public Instance Methods

api_v2(without_tenant: false) { || ... } click to toggle source
# File lib/scorm_engine/faraday/request.rb, line 20
def api_v2(without_tenant: false)
  @api_version = 2
  @without_tenant = without_tenant

  yield
ensure
  @api_version = 1
end
delete(path, options = {}) click to toggle source
# File lib/scorm_engine/faraday/request.rb, line 16
def delete(path, options = {})
  request(:delete, path, options)
end
get(path, options = {}) click to toggle source
# File lib/scorm_engine/faraday/request.rb, line 4
def get(path, options = {})
  request(:get, path, options)
end
post(path, options = {}, body = nil) click to toggle source
# File lib/scorm_engine/faraday/request.rb, line 8
def post(path, options = {}, body = nil)
  request(:post, path, options, body)
end
put(path, options = {}, body = nil) click to toggle source
# File lib/scorm_engine/faraday/request.rb, line 12
def put(path, options = {}, body = nil)
  request(:put, path, options, body)
end

Private Instance Methods

coerce_options(options = {}) click to toggle source
# File lib/scorm_engine/faraday/request.rb, line 56
def coerce_options(options = {})
  options.dup.each do |k, v|
    case k
    when :before, :since
      options[k] = v.iso8601 if v.respond_to?(:iso8601)
    end
  end
  options
end
request(method, path, options, body = nil) click to toggle source
# File lib/scorm_engine/faraday/request.rb, line 31
def request(method, path, options, body = nil)
  connection(version: @api_version).send(method) do |request|
    if @api_version == 2
      request.headers["engineTenantName"] = tenant unless @without_tenant
    else
      # "more" pagination urls are fully or relatively qualified
      path = "#{tenant}/#{path}" unless path =~ %r{\Ahttps?://} || path.start_with?(base_uri.path)
    end

    options = coerce_options(options)

    case method
    when :get, :delete
      request.url(path, options)
    when :post, :put
      if body.nil?
        body = options.dup
        options = {}
      end
      request.url(path, options)
      request.body = body unless body.empty?
    end
  end
end