rollbar-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b174a393dd39338a440f851db212dd871e8ad4a5
4
+ data.tar.gz: 958b809533fa494ff7729ae0fb1fa882e8cc439a
5
+ SHA512:
6
+ metadata.gz: ef6be010762ed92e51e895aa5d849032d883acbcf18fb0296dcc1d05d3b9665d9cafd7fdc5be37340deb20ebc94012395fb0604f523640270ac8d1c604c2a70b
7
+ data.tar.gz: 335a1d5139c03af4e730bb875b239a991db54a1044bcf6e65cc8925f11b5f3f6c126450a244a31e0785dae5dfc537e66e123d70a71ce7076bc28846f208e5b89
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ .env
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rollbar-api.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # rollbar-api [![CircleCI](https://circleci.com/gh/wealthsimple/rollbar-api.svg?style=svg)](https://circleci.com/gh/wealthsimple/rollbar-api)
2
+
3
+ Rubygem for accessing Rollbar's full REST and RQL APIs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile and run `bundle` to install:
8
+
9
+ ```ruby
10
+ gem 'rollbar-api'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ First, generate read-only access tokens for each project you need access to by navigating to **Settings** > **Project Access Tokens** and clicking **Add new access token**.
16
+
17
+ Next, configure each project:
18
+
19
+ ```ruby
20
+ # config/initializers/rollbar-api.rb in a Rails project
21
+ require 'rollbar-api'
22
+
23
+ # Add as many projects as you need. Each should have a unique access token.
24
+ RollbarApi::Project.add("my-project", ENV["MY_PROJECT_ACCESS_TOKEN"])
25
+ RollbarApi::Project.add("other-project", ENV["OTHER_PROJECT_ACCESS_TOKEN"])
26
+ ```
27
+
28
+ ### REST API
29
+
30
+ You can make HTTP `GET` calls to fetch items, deploys, occurrences, and so on by finding any project you added in the configuration and calling `.get` with the API endpoint:
31
+
32
+ ```ruby
33
+ items = RollbarApi::Project.find("my-project").get("/api/1/items/")
34
+ ```
35
+
36
+ Specify query parameters by passing them in as a hash:
37
+
38
+ ```ruby
39
+ top_items = RollbarApi::Project.find("my-project").get("/api/1/reports/top_active_items", {
40
+ hours: "24",
41
+ environments: "production,staging",
42
+ })
43
+ ```
44
+
45
+ ### RQL Queries
46
+
47
+ You can also run RQL queries:
48
+
49
+ ```ruby
50
+ # Create a job
51
+ rql_job = RollbarApi::Project.find("my-project").post("/api/1/rql/jobs", {
52
+ query_string: "select * from item_occurrence where item.counter=1",
53
+ })
54
+
55
+ # Check its status
56
+ rql_job = RollbarApi::Project.find("my-project").get("/api/1/rql/job/#{rql_job.result.id}")
57
+
58
+ # If it succeeded, get the RQL result
59
+ if rql_job.result.status == "success"
60
+ rql_result = RollbarApi::Project.find("my-project").get("/api/1/rql/job/#{rql_job.result.id}/result")
61
+ p rql_result
62
+ end
63
+ ```
64
+
65
+ See https://rollbar.com/docs/api/ for a full reference of API requests and responses.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rollbar-api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ require "dotenv/load"
2
+ require "pp"
3
+ require "bundler/setup"
4
+ require "rollbar-api"
5
+
6
+ project_name = ENV["ROLLBAR_PROJECT_NAME"] or raise "Must specify ROLLBAR_PROJECT_NAME in .env"
7
+ project_access_token = ENV["ROLLBAR_PROJECT_ACCESS_TOKEN"] or raise "Must specify ROLLBAR_PROJECT_ACCESS_TOKEN in .env"
8
+
9
+ RollbarApi::Project.add(project_name, project_access_token)
10
+
11
+ # Fetch all items
12
+ items = RollbarApi::Project.find(project_name).get("/api/1/items")
13
+
14
+ # Print out the first 5 items
15
+ items.result.items.first(5).each do |item|
16
+ pp item
17
+ puts "=" * 80
18
+ end
@@ -0,0 +1,23 @@
1
+ require "dotenv/load"
2
+ require "pp"
3
+ require "bundler/setup"
4
+ require "rollbar-api"
5
+
6
+ project_name = ENV["ROLLBAR_PROJECT_NAME"] or raise "Must specify ROLLBAR_PROJECT_NAME in .env"
7
+ project_access_token = ENV["ROLLBAR_PROJECT_ACCESS_TOKEN"] or raise "Must specify ROLLBAR_PROJECT_ACCESS_TOKEN in .env"
8
+
9
+ RollbarApi::Project.add(project_name, project_access_token)
10
+
11
+ # Create the job
12
+ rql_job = RollbarApi::Project.find(project_name).post("/api/1/rql/jobs", {
13
+ query_string: "select * from item_occurrence where item.counter=1",
14
+ })
15
+
16
+ # Fetch job status
17
+ rql_job = RollbarApi::Project.find(project_name).get("/api/1/rql/job/#{rql_job.result.id}")
18
+ pp rql_job
19
+
20
+ if rql_job.result.status == "success"
21
+ # Print out succeeded job result
22
+ pp RollbarApi::Project.find(project_name).get("/api/1/rql/job/#{rql_job.result.id}/result")
23
+ end
@@ -0,0 +1,19 @@
1
+ require "dotenv/load"
2
+ require "pp"
3
+ require "bundler/setup"
4
+ require "rollbar-api"
5
+
6
+ project_name = ENV["ROLLBAR_PROJECT_NAME"] or raise "Must specify ROLLBAR_PROJECT_NAME in .env"
7
+ project_access_token = ENV["ROLLBAR_PROJECT_ACCESS_TOKEN"] or raise "Must specify ROLLBAR_PROJECT_ACCESS_TOKEN in .env"
8
+
9
+ RollbarApi::Project.add(project_name, project_access_token)
10
+
11
+ top_active_items = RollbarApi::Project.find(project_name).get("/api/1/reports/top_active_items", {
12
+ hours: "24",
13
+ environments: "production,staging",
14
+ })
15
+
16
+ top_active_items.result.each_with_index do |item, i|
17
+ pp i, item
18
+ puts "=" * 80
19
+ end
@@ -0,0 +1,13 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "faraday"
4
+ require "json"
5
+ require "recursive-open-struct"
6
+
7
+ require "rollbar-api/project"
8
+ require "rollbar-api/request"
9
+ require "rollbar-api/resource"
10
+ require "rollbar-api/version"
11
+
12
+ module RollbarApi
13
+ end
@@ -0,0 +1,40 @@
1
+ module RollbarApi
2
+ class Project
3
+ @@projects = {}
4
+
5
+ def self.add(project_name, project_access_token)
6
+ @@projects[project_name] = project_access_token
7
+ end
8
+
9
+ def self.find(project_name)
10
+ project_access_token = @@projects[project_name]
11
+ new(project_name, project_access_token) if project_access_token.present?
12
+ end
13
+
14
+ def self.all
15
+ @@projects.map { |project_name, _| find(project_name) }
16
+ end
17
+
18
+ def self.delete_all
19
+ @@projects = {}
20
+ end
21
+
22
+ attr_reader :name, :access_token
23
+ def initialize(name, access_token)
24
+ @name = name
25
+ @access_token = access_token
26
+ end
27
+
28
+ %i(get post put delete head patch).each do |http_method|
29
+ define_method(http_method) do |path, params = {}|
30
+ params[:access_token] = access_token
31
+ response = Request.new(method: http_method, path: path, params: params).execute
32
+ if response.is_a?(Array)
33
+ response.map { |r| Resource.new(r) }
34
+ else
35
+ Resource.new(response)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,46 @@
1
+ module RollbarApi
2
+ class Request
3
+ attr_reader :method, :path, :params
4
+ def initialize(method:, path:, params:)
5
+ @method = method
6
+ path = "/#{path}" unless path.start_with?("/")
7
+ @path = path
8
+ @params = params
9
+ end
10
+
11
+ def execute
12
+ connection.send(method) do |request|
13
+ if method == :get
14
+ request.url("#{path}?#{params.to_param}")
15
+ else
16
+ request.url(path)
17
+ request.body = body_to_json(params) if params.present?
18
+ end
19
+
20
+ request.headers.merge!({
21
+ "Accept" => "application/json",
22
+ "Content-Type" => "application/json",
23
+ })
24
+ request
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def connection
31
+ Faraday.new(url: "https://api.rollbar.com") do |faraday|
32
+ faraday.response :logger, ::Logger.new(STDOUT), bodies: false
33
+ faraday.adapter Faraday.default_adapter
34
+ faraday.use Faraday::Response::RaiseError
35
+ end
36
+ end
37
+
38
+ def body_to_json(body)
39
+ if body
40
+ body.is_a?(Array) || body.is_a?(Hash) ? body.to_json : body
41
+ else
42
+ nil
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ module RollbarApi
2
+ class Resource
3
+ attr_reader :response_json
4
+
5
+ def initialize(response)
6
+ response = response.body if response.is_a?(Faraday::Response)
7
+ @response_json = if response.is_a?(String)
8
+ JSON.parse(response)
9
+ else
10
+ response
11
+ end
12
+ @struct = RecursiveOpenStruct.new(@response_json, {recurse_over_arrays: true})
13
+ end
14
+
15
+ def as_json(options = {})
16
+ @response_json
17
+ end
18
+
19
+ def method_missing(name, *args)
20
+ @struct.send(name, *args)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module RollbarApi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rollbar-api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rollbar-api"
8
+ spec.version = RollbarApi::VERSION
9
+ spec.authors = ["Peter Graham"]
10
+ spec.email = ["peter@wealthsimple.com"]
11
+
12
+ spec.summary = %q{Rubygem for Rollbar REST and RQL APIs}
13
+ spec.description = %q{Rubygem for accessing Rollbar's full REST and RQL APIs.}
14
+ spec.homepage = "https://github.com/wealthsimple/rollbar-api"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+ spec.license = "MIT"
23
+
24
+ spec.add_dependency "activesupport", ">= 4"
25
+ spec.add_dependency "faraday", "0.12.1"
26
+ spec.add_dependency "recursive-open-struct"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.14"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "rspec-its"
32
+ spec.add_development_dependency "rspec-collection_matchers"
33
+ spec.add_development_dependency "rspec_junit_formatter", "~> 0.2"
34
+ spec.add_development_dependency "webmock", "~> 3.0"
35
+ spec.add_development_dependency "dotenv"
36
+ end
metadata ADDED
@@ -0,0 +1,214 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollbar-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Graham
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: recursive-open-struct
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-its
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-collection_matchers
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec_junit_formatter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.2'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.2'
139
+ - !ruby/object:Gem::Dependency
140
+ name: webmock
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: dotenv
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Rubygem for accessing Rollbar's full REST and RQL APIs.
168
+ email:
169
+ - peter@wealthsimple.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - Gemfile
177
+ - README.md
178
+ - Rakefile
179
+ - bin/console
180
+ - bin/setup
181
+ - examples/list_items.rb
182
+ - examples/rql_query.rb
183
+ - examples/top_active_items.rb
184
+ - lib/rollbar-api.rb
185
+ - lib/rollbar-api/project.rb
186
+ - lib/rollbar-api/request.rb
187
+ - lib/rollbar-api/resource.rb
188
+ - lib/rollbar-api/version.rb
189
+ - rollbar-api.gemspec
190
+ homepage: https://github.com/wealthsimple/rollbar-api
191
+ licenses:
192
+ - MIT
193
+ metadata: {}
194
+ post_install_message:
195
+ rdoc_options: []
196
+ require_paths:
197
+ - lib
198
+ required_ruby_version: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ requirements: []
209
+ rubyforge_project:
210
+ rubygems_version: 2.5.1
211
+ signing_key:
212
+ specification_version: 4
213
+ summary: Rubygem for Rollbar REST and RQL APIs
214
+ test_files: []