Method: ACTV::Client#request

Defined in:
lib/actv/client.rb

#request(method, path, params, options) ⇒ Object

Perform an HTTP Request



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/actv/client.rb', line 334

def request(method, path, params, options)
  uri = options[:endpoint] || @endpoint
  uri = URI(uri) unless uri.respond_to?(:host)
  uri += path
  request_headers = {}
  params[:api_key] = @api_key unless @api_key.nil?

  if self.credentials?
    # When posting a file, don't sign any params
    signature_params = if [:post, :put].include?(method.to_sym) && params.values.any?{|value| value.is_a?(File) || (value.is_a?(Hash) && (value[:io].is_a?(IO) || value[:io].is_a?(StringIO)))}
      {}
    else
      params
    end
    authorization = SimpleOAuth::Header.new(method, uri, signature_params, credentials)
    request_headers[:authorization] = authorization.to_s.sub('OAuth', "Bearer")
  end
  connection.url_prefix = options[:endpoint] || @endpoint
  connection.run_request(method.to_sym, path, nil, request_headers) do |request|
    unless params.empty?
      case request.method
      when :post, :put
        request.body = params
      else
        request.params.update(params)
      end
    end
    yield request if block_given?
  end.env
rescue Faraday::Error::ClientError
  raise ACTV::Error::ClientError
end