class Bugsnag::Api::Client

Client for the Bugsnag API

@see docs.bugsnagapiv2.apiary.io/

Constants

CONVENIENCE_HEADERS

Header keys that can be passed in options hash to {#get},{#head}

Public Class Methods

new(options = {}) { |configuration| ... } click to toggle source
# File lib/bugsnag/api/client.rb, line 41
def initialize(options = {}, &block)
  configuration.load(options)
  yield(configuration) if block_given?
end

Public Instance Methods

basic_authenticated?() click to toggle source

Indicates if the client was supplied Basic Auth username and password

@see bugsnag.com/docs/api#user-authentication @return [Boolean]

# File lib/bugsnag/api/client.rb, line 140
def basic_authenticated?
  !!(configuration.email && configuration.password)
end
configuration() click to toggle source

Get client’s configuration options

@return [Bugsnag::Api::Configuration] configuration wrapper

# File lib/bugsnag/api/client.rb, line 55
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source

Set configuration options using a block

# File lib/bugsnag/api/client.rb, line 47
def configure
  yield(configuration) if block_given?
  reset_agent
end
deep_merge(l_hash, r_hash) click to toggle source

Merges hashes together cleanly, favouring RHS values

@return [Hash]

# File lib/bugsnag/api/client.rb, line 155
def deep_merge(l_hash, r_hash)
  l_hash.merge(r_hash) do |_key, l_val, r_val|
    if l_val.is_a?(Hash) && r_val.is_a?(Hash)
      deep_merge(l_val, r_val)
    else
      r_val
    end
  end
end
delete(url, options = {}) click to toggle source

Make a HTTP DELETE request

@param url [String] The path, relative to {#endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/bugsnag/api/client.rb, line 91
def delete(url, options = {})
  request :delete, url, options
end
get(url, options = {}) click to toggle source

Make a HTTP GET request

@param url [String] The path, relative to {#endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/bugsnag/api/client.rb, line 64
def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end
last_response() click to toggle source

Response for last HTTP request

@return [Sawyer::Response]

# File lib/bugsnag/api/client.rb, line 131
def last_response
  @last_response if defined? @last_response
end
paginate(url, options = {}) { |data, last_response| ... } click to toggle source

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in {#auto_paginate}.

@param url [String] The path, relative to {#endpoint} @param options [Hash] Query and header params for request @param block [Block] Block to perform the data concatination of the

multiple requests. The block is called with two parameters, the first
contains the contents of the requests so far and the second parameter
contains the latest response.

@return [Sawyer::Resource]

# File lib/bugsnag/api/client.rb, line 106
def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options.dup)
  if configuration.auto_paginate || configuration.per_page
    opts[:query][:per_page] ||=  configuration.per_page || (configuration.auto_paginate ? 100 : nil)
  end

  data = request(:get, url, opts)

  if configuration.auto_paginate
    while @last_response.rels[:next]
      @last_response = @last_response.rels[:next].get
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end
  end

  data
end
patch(url, options = {}) click to toggle source

Make a HTTP PATCH request

@param url [String] The path, relative to {#endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/bugsnag/api/client.rb, line 82
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source

Make a HTTP POST request

@param url [String] The path, relative to {#endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/bugsnag/api/client.rb, line 73
def post(url, options = {})
  request :post, url, options
end
token_authenticated?() click to toggle source

Indicates if the client was supplied an auth token

@see bugsnag.com/docs/api#account-authentication @return [Boolean]

# File lib/bugsnag/api/client.rb, line 148
def token_authenticated?
  !!configuration.auth_token
end

Private Instance Methods

agent() click to toggle source
# File lib/bugsnag/api/client.rb, line 166
def agent
  @agent ||= Sawyer::Agent.new(configuration.endpoint, sawyer_options) do |http|
    http.headers[:content_type] = "application/json"
    http.headers[:'X-Version'] = "2"
    http.headers[:'X-Bugsnag-Api'] = "true"
    http.headers[:user_agent] = configuration.user_agent

    if basic_authenticated?
      credentials = Base64.strict_encode64("#{configuration.email}:#{configuration.password}")

      http.headers[:Authorization] = "Basic #{credentials}"
    elsif token_authenticated?
      http.headers[:Authorization] = "token #{configuration.auth_token}"
    end
  end
end
boolean_from_response(method, path, options = {}) click to toggle source
# File lib/bugsnag/api/client.rb, line 200
def boolean_from_response(method, path, options = {})
  request(method, path, options)
  @last_response.status == 204
rescue Bugsnag::Api::NotFound
  false
end
parse_query_and_convenience_headers(options) click to toggle source
# File lib/bugsnag/api/client.rb, line 219
def parse_query_and_convenience_headers(options)
  headers = options.fetch(:headers, {})
  CONVENIENCE_HEADERS.each do |h|
    if header = options.delete(h)
      headers[h] = header
    end
  end
  query = options.delete(:query)
  opts = {:query => options}
  opts[:query].merge!(query) if query && query.is_a?(Hash)
  opts[:headers] = headers unless headers.empty?

  opts
end
request(method, path, data, options = {}) click to toggle source
# File lib/bugsnag/api/client.rb, line 187
def request(method, path, data, options = {})
  if data.is_a?(Hash)
    options[:query]   = data.delete(:query) || {}
    options[:headers] = data.delete(:headers) || {}
    if accept = data.delete(:accept)
      options[:headers][:accept] = accept
    end
  end

  @last_response = response = agent.call(method, path.to_s, data, options)
  response.data
end
reset_agent() click to toggle source
# File lib/bugsnag/api/client.rb, line 183
def reset_agent
  @agent = nil
end
sawyer_options() click to toggle source
# File lib/bugsnag/api/client.rb, line 207
def sawyer_options
  opts = {
    :links_parser => Sawyer::LinkParsers::Simple.new
  }
  conn_opts = configuration.connection_options
  conn_opts[:builder] = configuration.middleware if configuration.middleware
  conn_opts[:proxy] = configuration.proxy if configuration.proxy
  opts[:faraday] = Faraday.new(conn_opts)

  opts
end