Class: Browserbeam::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/browserbeam/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ Http

Returns a new instance of Http.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/browserbeam/http.rb', line 6

def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT)
  @conn = Faraday.new(url: base_url) do |f|
    f.request :json
    f.response :json, content_type: /\bjson$/
    f.adapter Faraday.default_adapter
    f.headers["Authorization"] = "Bearer #{api_key}"
    f.headers["User-Agent"] = "browserbeam-ruby/#{VERSION}"
    f.options.timeout = timeout
    f.options.open_timeout = 10
  end
end

Instance Method Details

#delete(path) ⇒ Object



33
34
35
36
37
# File 'lib/browserbeam/http.rb', line 33

def delete(path)
  response = @conn.delete(path)
  return {} if response.status == 204
  handle_response(response)
end

#get(path, params = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/browserbeam/http.rb', line 26

def get(path, params = {})
  response = @conn.get(path) do |req|
    params.each { |k, v| req.params[k.to_s] = v unless v.nil? }
  end
  handle_response(response)
end

#post(path, body = {}, extra_headers = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/browserbeam/http.rb', line 18

def post(path, body = {}, extra_headers = {})
  response = @conn.post(path) do |req|
    req.body = body
    extra_headers.each { |k, v| req.headers[k] = v }
  end
  handle_response(response)
end