Skip to content

Instantly share code, notes, and snippets.

@x0bandeira
Last active March 8, 2016 16:54
Show Gist options
  • Save x0bandeira/7722884c2a19c0b7ebc6 to your computer and use it in GitHub Desktop.
Save x0bandeira/7722884c2a19c0b7ebc6 to your computer and use it in GitHub Desktop.
# See https://bugsnag.com/docs/api
# Usage:
# client = BugsnagApiClient.with_token('<your account token>')
# data = client.with_error('<error id>').events(start_time: 5.days.ago, per_page: 30).map {|e| e['meta_data']['Custom']['target_id'] }
class BugsnagApiClient
attr_reader :rest_client
def self.with_token(bugsnag_auth_token)
rc = RestClient::Resource.new('https://api.bugsnag.com', headers: {'Authorization' => "token #{bugsnag_auth_token}"})
new(rc)
end
def initialize(rest_client)
@rest_client = rest_client
end
def with_account(name)
@account = JSON[@rest_client['accounts'].get].find {|account| account['name'] == name }
self
end
def with_error(error_id)
@error_id = error_id
self
end
# See https://bugsnag.com/docs/api/events#list-an-error-s-events
def events(opts = {})
JSON[resource("errors/#{@error_id}/events").get(filter_params opts)]
end
def resource(path = "/")
if @account
path = "accounts/#{@account['id']}/#{path}"
end
@rest_client[path]
end
def filter_params(params)
params.tap do |hash|
hash[:start_time] = hash[:start_time].iso8601 if hash[:start_time]
hash[:end_time] = hash[:end_time].iso8601 if hash[:end_time]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment