reliquary 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd7b4983647c7844274ae58e7c83edd4f79ea230
4
+ data.tar.gz: 8e1720e417896b6fbdcd40e2c2b903da3b063f54
5
+ SHA512:
6
+ metadata.gz: da305a3cb590bf6bb501f13935b47132f80b49e414babd5740a0ae53553f4f3a32ba325438b1407585e8491e518a3c2d9264c7c10192ebe190a96a06df9093f1
7
+ data.tar.gz: 667f4704fd9645da4b88ca490010e1a4a6524d0c177a544339a3c1e4ee020a5a303450b44932f26ac1b7f8ee613dadc1706354372614c766cc54bfb2741cfcef
@@ -0,0 +1,52 @@
1
+ ### https://raw.github.com/github/gitignore/94043d7fc7adf554c69355daeb75e2c97ccd7671/Ruby.gitignore
2
+
3
+ *.gem
4
+ *.rbc
5
+ /.config
6
+ /coverage/
7
+ /InstalledFiles
8
+ /pkg/
9
+ /spec/reports/
10
+ /spec/examples.txt
11
+ /test/tmp/
12
+ /test/version_tmp/
13
+ /tmp/
14
+
15
+ # Used by dotenv library to load environment variables.
16
+ .env
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
37
+
38
+
39
+ ### https://raw.github.com/github/gitignore/94043d7fc7adf554c69355daeb75e2c97ccd7671/Global/Vim.gitignore
40
+
41
+ # swap
42
+ [._]*.s[a-w][a-z]
43
+ [._]s[a-w][a-z]
44
+ # session
45
+ Session.vim
46
+ # temporary
47
+ .netrwhist
48
+ *~
49
+ # auto-generated tag files
50
+ tags
51
+
52
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ 2.2.4
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1 @@
1
+ --no-private lib/**/*.rb - CODE_OF_CONDUCT.md LICENSE.txt
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at steve.huff@runkeeper.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in reliquary.gemspec
4
+ group :production do
5
+ gem "chronic", "~> 0.10"
6
+ gem "multi_json", "~> 1.11"
7
+ gem "oj", "~> 2.15"
8
+ gem "rest-client", "~> 1.8"
9
+ end
10
+
11
+ group :development,:test do
12
+ gem "awesome_print", "~> 1.6"
13
+ gem "bond", "~> 0.5"
14
+ gem "binding_of_caller", "~> 0.7"
15
+ gem "bundler", "~> 1.11"
16
+ gem "dotenv", "~> 2"
17
+ gem "irbtools", require: "irbtools/binding"
18
+ gem "rake", "~> 10.0"
19
+ gem "rspec", "~> 3.0"
20
+ gem "yard", "~> 0.8"
21
+ end
@@ -0,0 +1,116 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ awesome_print (1.6.1)
5
+ binding.repl (3.0.0)
6
+ binding_of_caller (0.7.2)
7
+ debug_inspector (>= 0.0.1)
8
+ bond (0.5.1)
9
+ cd (1.0.1)
10
+ chronic (0.10.2)
11
+ clipboard (1.1.0)
12
+ code (0.9.0)
13
+ coderay (~> 1.1)
14
+ method_source (~> 0.8, >= 0.8.2)
15
+ coderay (1.1.1)
16
+ debug_inspector (0.0.2)
17
+ debugging (1.1.0)
18
+ binding.repl (~> 3.0)
19
+ paint (>= 0.9, < 2.0)
20
+ diff-lcs (1.2.5)
21
+ domain_name (0.5.20160310)
22
+ unf (>= 0.0.5, < 1.0.0)
23
+ dotenv (2.1.1)
24
+ every_day_irb (2.0.0)
25
+ cd (~> 1.0)
26
+ fancy_irb (1.0.2)
27
+ paint (>= 0.9, < 2.0)
28
+ unicode-display_width (~> 1.0)
29
+ ffi (1.9.10)
30
+ hirb (0.7.3)
31
+ http-cookie (1.0.2)
32
+ domain_name (~> 0.5)
33
+ instance (0.2.0)
34
+ interactive_editor (0.0.10)
35
+ spoon (>= 0.0.1)
36
+ irbtools (2.0.1)
37
+ binding.repl (~> 3.0)
38
+ clipboard (~> 1.0, >= 1.0.6)
39
+ code (~> 0.9)
40
+ coderay (~> 1.1)
41
+ debugging (~> 1.1)
42
+ every_day_irb (~> 2.0)
43
+ fancy_irb (~> 1.0)
44
+ hirb (~> 0.7, >= 0.7.3)
45
+ instance (~> 0.2)
46
+ interactive_editor (~> 0.0, >= 0.0.10)
47
+ method_locator (~> 0.0, >= 0.0.4)
48
+ methodfinder (~> 2.0)
49
+ ori (~> 0.1.0)
50
+ os (~> 0.9)
51
+ paint (>= 0.9, < 2.0)
52
+ ruby_engine (~> 1.0)
53
+ ruby_info (~> 1.0)
54
+ ruby_version (~> 1.0)
55
+ wirb (~> 2.0)
56
+ method_locator (0.0.4)
57
+ method_source (0.8.2)
58
+ methodfinder (2.0.0)
59
+ mime-types (2.99.1)
60
+ multi_json (1.11.3)
61
+ netrc (0.11.0)
62
+ oj (2.15.0)
63
+ ori (0.1.0)
64
+ os (0.9.6)
65
+ paint (1.0.1)
66
+ rake (10.5.0)
67
+ rest-client (1.8.0)
68
+ http-cookie (>= 1.0.2, < 2.0)
69
+ mime-types (>= 1.16, < 3.0)
70
+ netrc (~> 0.7)
71
+ rspec (3.4.0)
72
+ rspec-core (~> 3.4.0)
73
+ rspec-expectations (~> 3.4.0)
74
+ rspec-mocks (~> 3.4.0)
75
+ rspec-core (3.4.4)
76
+ rspec-support (~> 3.4.0)
77
+ rspec-expectations (3.4.0)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.4.0)
80
+ rspec-mocks (3.4.1)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.4.0)
83
+ rspec-support (3.4.1)
84
+ ruby_engine (1.0.1)
85
+ ruby_info (1.0.1)
86
+ ruby_version (1.0.1)
87
+ spoon (0.0.4)
88
+ ffi
89
+ unf (0.1.4)
90
+ unf_ext
91
+ unf_ext (0.0.7.2)
92
+ unicode-display_width (1.0.3)
93
+ wirb (2.0.0)
94
+ paint (>= 0.9, < 2.0)
95
+ yard (0.8.7.6)
96
+
97
+ PLATFORMS
98
+ ruby
99
+
100
+ DEPENDENCIES
101
+ awesome_print (~> 1.6)
102
+ binding_of_caller (~> 0.7)
103
+ bond (~> 0.5)
104
+ bundler (~> 1.11)
105
+ chronic (~> 0.10)
106
+ dotenv (~> 2)
107
+ irbtools
108
+ multi_json (~> 1.11)
109
+ oj (~> 2.15)
110
+ rake (~> 10.0)
111
+ rest-client (~> 1.8)
112
+ rspec (~> 3.0)
113
+ yard (~> 0.8)
114
+
115
+ BUNDLED WITH
116
+ 1.11.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Steve Huff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,88 @@
1
+ # Reliquary [![Gem Version](https://badge.fury.io/rb/reliquary.svg)](https://badge.fury.io/rb/reliquary)
2
+
3
+ Reliquary is a client for the [New Relic REST API v2](https://docs.newrelic.com/docs/apis/rest-api-v2). It provides an alternative to the deprecated [newrelic_api](https://github.com/newrelic/newrelic_api) gem.
4
+
5
+ ## TL;DR
6
+
7
+ ```shell
8
+ $ export NEWRELIC_API_KEY='<your API key>'
9
+ ```
10
+
11
+ ```ruby
12
+ apps = Reliquary::API::Applications.new
13
+
14
+ my_app = apps.list(name: 'My App')
15
+
16
+ my_app[0][:id]
17
+ #=> 123467
18
+
19
+ apps.list(lang: :java).collect {|app| app[:name]}.sort
20
+ #=> <sorted list of your Java apps' names>
21
+ ```
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'reliquary'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install reliquary
38
+
39
+ ## Usage
40
+
41
+ The New Relic REST API v2 has the following sections (after each section is the minimum version of Reliquary implementing that section):
42
+
43
+ * Alerts Channels
44
+ * Alerts Events
45
+ * Alerts External Service Conditions
46
+ * Alerts Incidents
47
+ * Alerts Plugins Conditions
48
+ * Alerts Policies
49
+ * Alerts Policy Channels
50
+ * Alerts Synthetics Conditions
51
+ * Alerts Violations
52
+ * Application Hosts
53
+ * Application Instances
54
+ * Applications (0.1.0)
55
+ * Browser Applications
56
+ * Components
57
+ * Key Transactions (0.1.0)
58
+ * Labels
59
+ * Legacy Alert Policies
60
+ * Mobile Applications
61
+ * Notification Channels
62
+ * Plugins
63
+ * Servers
64
+ * Usages
65
+ * Users
66
+
67
+ For some reason the [API Explorer](https://rpm.newrelic.com/api/explore) does not sort these categories lexically. I'll update this document as I implement additional sections of the API.
68
+
69
+ Each section of the API is implemented as a class under the `Reliquary::API` namespace. Each of these classes exposes methods corresponding to the API methods described by the API Explorer. When methods take parameters (required or optional), pass them in as a typical params hash.
70
+
71
+ Access to the API requires an [API key](https://docs.newrelic.com/docs/apis/rest-api-v2/requirements/api-keys). Reliquary reads your API key at runtime from the `NEWRELIC_API_KEY` environment variable. If you really want to provide your API key via some other method, or change the API key while the program is running, then you'll need to create your own instance of `Reliquary::Client` with a different API key and then pass it in when initializing instances of the API section classes.
72
+
73
+ ## Development
74
+
75
+ * write tests
76
+ * handle HTTP error codes
77
+ * implement additional API sections
78
+ * fix YARDoc errors
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hakamadare/reliquary. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
+
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
88
+
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "yard"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ YARD::Rake::YardocTask.new do |t|
8
+ t.files = ['lib/**/*.rb', 'lib/**/**/*.rb'] # optional
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ Bundler.setup(:default,:production,:development)
5
+
6
+ require "dotenv"
7
+ Dotenv.load
8
+
9
+ require "debugging/all"
10
+
11
+ loaddirs = [
12
+ ['.', 'lib'],
13
+ ]
14
+
15
+ loaddirs.each do |path|
16
+ libdir = File.join(path)
17
+ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
18
+ end
19
+
20
+ require "reliquary"
21
+
22
+ # You can add fixtures and/or initialization code here to make experimenting
23
+ # with your gem easier. You can also use a different console, if you like.
24
+
25
+ # (If you use this, don't forget to add pry to your Gemfile!)
26
+ # require "pry"
27
+ # Pry.start
28
+
29
+ require "irb"
30
+ IRB.start
@@ -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,6 @@
1
+ require "reliquary/version"
2
+
3
+ module Reliquary
4
+ autoload :API, 'reliquary/api'
5
+ autoload :Client, 'reliquary/client'
6
+ end
@@ -0,0 +1,9 @@
1
+ module Reliquary
2
+ module API
3
+
4
+ autoload :Applications, 'reliquary/api/applications'
5
+ autoload :KeyTransactions, 'reliquary/api/key_transactions'
6
+ autoload :Base, 'reliquary/api/base'
7
+
8
+ end
9
+ end
@@ -0,0 +1,206 @@
1
+ # @markup markdown
2
+ # @title Reliquary::API::Applications
3
+ # @author Steve Huff
4
+ #
5
+
6
+ module Reliquary
7
+ module API
8
+ class Applications < Reliquary::API::Base
9
+
10
+ # URI fragment for Applications API endpoint
11
+ URI_FRAGMENT = 'applications.json'
12
+
13
+ # URI method for Applications API endpoint
14
+ URI_METHOD = :get
15
+
16
+ # How to parameterize queries against API endpoint
17
+ # These are for parameters to be added to the query; some endpoints
18
+ # require additional parameters to build the URI fragment.
19
+ METHOD_PARAMS = {
20
+ :list => {
21
+ :name => {
22
+ :key => 'filter[name]',
23
+ },
24
+ :ids => {
25
+ :key => 'filter[ids]',
26
+ :transform => lambda {|x| x.join(',')},
27
+ },
28
+ :host => {
29
+ :key => 'filter[host]',
30
+ },
31
+ :lang => {
32
+ :key => 'filter[language]',
33
+ },
34
+ :page => {},
35
+ },
36
+ :show => {},
37
+ :metric_names => {
38
+ :name => {},
39
+ :page => {},
40
+ },
41
+ :metric_data => {
42
+ :name => {},
43
+ :page => {},
44
+ :values => {
45
+ :key => 'values[]',
46
+ :transform => lambda {|x| x.join(',')},
47
+ },
48
+ :from => {
49
+ :munge => lambda {|x| self.parse_time(x)},
50
+ :transform => lambda {|x| self.format_time(x)},
51
+ },
52
+ :to => {
53
+ :munge => lambda {|x| self.parse_time(x)},
54
+ :transform => lambda {|x| self.format_time(x)},
55
+ },
56
+ :period => {
57
+ :transform => lambda {|x| x.to_i},
58
+ },
59
+ :summarize => {
60
+ :transform => lambda {|x| x ? 'true' : 'false'},
61
+ },
62
+ :raw => {
63
+ :transform => lambda {|x| x ? 'true' : 'false'},
64
+ },
65
+ },
66
+ :update => {},
67
+ :delete => {},
68
+ }
69
+
70
+ # @!method list
71
+ # List applications, optionally filtering by name or ID
72
+ # @param [Hash] params parameters for listing
73
+ # @option [String] :name New Relic application name to select
74
+ # @option [Array<String>] :ids New Relic application IDs to select
75
+ # @option [String] :host New Relic application host to select
76
+ # @option [String] :lang New Relic application language to select
77
+ #
78
+ def list(params = {})
79
+ begin
80
+ # this is the "default" Applications method, no overrides
81
+ api_params = {}
82
+
83
+ execute(api_params, {:params => process_request_params(__method__, params)})
84
+
85
+ rescue StandardError => e
86
+ raise e
87
+ end
88
+ end
89
+
90
+ # @!method show
91
+ # Show summary for a single application
92
+ # @param [Hash] params parameters for listing
93
+ # @option params [Integer] :id New Relic application ID
94
+ def show(params = {})
95
+ begin
96
+ id = retrieve_id(params)
97
+
98
+ # HTTP method is the default GET
99
+ # override the URI fragment
100
+ api_params = { :uri_fragment => "applications/#{id}.json" }
101
+
102
+ execute(api_params, {:params => process_request_params(__method__, params)})
103
+
104
+ rescue StandardError => e
105
+ raise e
106
+ end
107
+ end
108
+
109
+ # @!method metric_names
110
+ # List metric names for a single application
111
+ # @param [Hash] params parameters for listing
112
+ # @option params [Integer] :id New Relic application ID
113
+ def metric_names(params = {})
114
+ begin
115
+ id = retrieve_id(params)
116
+
117
+ # HTTP method is the default GET
118
+ # override the URI fragment
119
+ api_params = { :uri_fragment => "applications/#{id}/metrics.json" }
120
+
121
+ execute(api_params, {:params => process_request_params(__method__, params)})
122
+
123
+ rescue StandardError => e
124
+ raise e
125
+ end
126
+ end
127
+
128
+ # @!method metric_data
129
+ # List metric date for a single application
130
+ # @param [Hash] params parameters for listing
131
+ # @option params [Integer] :id New Relic application ID
132
+ # @option params [Array<String>] :names Names of metrics to retrieve
133
+ # @option params [Array<String>] :values Names of metric values to retrieve
134
+ # @option params [Time] :from Retrieve metrics after this time
135
+ # @option params [Time] :to Retrieve metrics before this time
136
+ # @option params [Time] :period Period of timeslices in seconds
137
+ # @option params [Boolean] :summarize Return summarized data or all the samples
138
+ # @option params [Boolean] :raw Return unformatted data
139
+ def metric_data(params = {})
140
+ begin
141
+ id = retrieve_id(params)
142
+
143
+ names_param = params.fetch(:names).collect {|x| x.to_s}.join("\n")
144
+
145
+ raise "you must supply one or more New Relic metric names" if names_param.nil?
146
+
147
+ # HTTP method is the default GET
148
+ # override the URI fragment
149
+ api_params = { :uri_fragment => "applications/#{id}/metrics/data.json" }
150
+
151
+ execute(api_params, {:params => process_request_params(__method__, params).merge({'names[]' => names_param})})
152
+
153
+ rescue StandardError => e
154
+ raise e
155
+ end
156
+ end
157
+
158
+ # @!method update
159
+ # Update certain parameters of an application
160
+ # @param [Hash] params parameters for update
161
+ def update(params = {})
162
+ begin
163
+ id = retrieve_id(params)
164
+
165
+ raise "not implemented yet"
166
+
167
+ # FIXME build the JSON payload
168
+
169
+ # HTTP method is PUT
170
+ # override the URI fragment
171
+ api_params = {
172
+ :uri_method => :put,
173
+ :uri_fragment => "applications/#{id}.json",
174
+ }
175
+
176
+ execute(api_params, {:params => process_request_params(__method__, params)})
177
+
178
+ rescue StandardError => e
179
+ raise e
180
+ end
181
+ end
182
+
183
+ # @!method delete
184
+ # Delete an application and all data
185
+ # @param [Hash] params parameters for delete
186
+ def delete(params = {})
187
+ begin
188
+ id = retrieve_id(params)
189
+
190
+ # HTTP method is PUT
191
+ # override the URI fragment
192
+ api_params = {
193
+ :uri_method => :delete,
194
+ :uri_fragment => "applications/#{id}.json",
195
+ }
196
+
197
+ execute(api_params, {:params => process_request_params(__method__, params)})
198
+
199
+ rescue StandardError => e
200
+ raise e
201
+ end
202
+ end
203
+
204
+ end
205
+ end
206
+ end
@@ -0,0 +1,241 @@
1
+ # @markup markdown
2
+ # @title Reliquary::API::Base
3
+ # @author Steve Huff
4
+ #
5
+ require 'chronic'
6
+
7
+ module Reliquary
8
+ module API
9
+ # @abstract
10
+ class Base
11
+
12
+ # @!attribute [r] client
13
+ # @return [Reliquary::Client] the API client to be used for requests
14
+ attr_reader :client
15
+
16
+ # @!attribute [r] uri_fragment
17
+ # @return [String] URI fragment defining the API endpoint
18
+ attr_reader :uri_fragment
19
+
20
+ # @!attribute [r] uri_method
21
+ # @return [Symbol] default URI method to be used for requests to the API endpoint
22
+ attr_reader :uri_method
23
+
24
+ # @!method initialize(params = {})
25
+ # Constructor method for base API component
26
+ # @param [Hash] params parameters for component
27
+ # @option params [Reliquary::Client] :client (see client)
28
+ # @option params [String] :uri_fragment (see uri_fragment)
29
+ # @option params [String] :uri_method (see uri_method)
30
+ def initialize(params = {})
31
+ begin
32
+ client = params[:client]
33
+ client = Reliquary::Client.new unless client.kind_of? Reliquary::Client
34
+ @client = client
35
+
36
+ uri_fragment = params[:uri_fragment]
37
+ uri_fragment = self.class::URI_FRAGMENT unless uri_fragment.kind_of? String
38
+ @uri_fragment = uri_fragment
39
+
40
+ uri_method = params[:uri_method]
41
+ uri_method = self.class::URI_METHOD unless uri_method.kind_of? String
42
+ @uri_method = uri_method
43
+
44
+ rescue StandardError => e
45
+ raise e
46
+ end
47
+ end
48
+
49
+ # @!method execute(*args, &block)
50
+ # Execute a HTTP request using the API client
51
+ # @param [Hash] params parameters for request
52
+ # @option params [Symbol] :uri_method (see uri_method)
53
+ # @option params [String] :uri_fragment (see uri_fragment)
54
+ # @param [Array] args parameters for request
55
+ # @param [Proc] block block to which request will yield
56
+ def execute(params, *args, &block)
57
+ begin
58
+ raise "params must be a Hash" unless params.kind_of? Hash
59
+
60
+ uri_fragment = params.fetch(:uri_fragment, self.uri_fragment)
61
+ uri_method = params.fetch(:uri_method, self.uri_method)
62
+
63
+ resp = self.client[uri_fragment].send(uri_method, *args, &block)
64
+
65
+ # FIXME check HTTP response code here
66
+
67
+ self.client.parse(resp)
68
+
69
+ rescue MultiJson::ParseError => e
70
+ raise "unable to parse JSON: #{e.message}"
71
+
72
+ rescue StandardError => e
73
+ raise e
74
+ end
75
+ end
76
+
77
+ # @!method method_params
78
+ # Return the data structure describing the parameters for modifying a
79
+ # particular API method's queries
80
+ # @param [Symbol] method_name API method
81
+ # @return [Hash] immutable hash of query params for the API method
82
+ # @class
83
+ def self.method_params(method_name)
84
+ begin
85
+ self::METHOD_PARAMS.fetch(method_name.to_sym).freeze
86
+
87
+ rescue KeyError => e
88
+ raise "'#{method_name}' does not look like an API method implemented by #{self}: #{e.message}"
89
+
90
+ rescue NameError => e
91
+ raise "#{self} does not appear to have implemented query params: #{e.message}"
92
+
93
+ rescue StandardError => e
94
+ raise e
95
+ end
96
+ end
97
+
98
+ protected
99
+
100
+ # @!method retrieve_id
101
+ # Retrieve New Relic ID from a params hash
102
+ # @param [Hash] params Hash of parameters
103
+ # @param [Symbol] id_key Hash key associated with ID value (default `:id`)
104
+ # @return [Integer] New Relic ID
105
+ def retrieve_id(params, id_key = :id)
106
+ begin
107
+ id_val = params.fetch(id_key.to_sym)
108
+
109
+ if id_val.nil?
110
+ raise "you must supply a New Relic application ID"
111
+ else
112
+ id_val.to_i
113
+ end
114
+
115
+ rescue NoMethodError => e
116
+ raise "unable to convert '#{id_val.inspect}' to integer: #{e.message}"
117
+
118
+ rescue KeyError => e
119
+ raise "the params hash has no key called '#{id_key}': #{e.message}"
120
+
121
+ rescue StandardError => e
122
+ raise e
123
+ end
124
+ end
125
+
126
+ # @!method process_request_params
127
+ # Iterate over an API method's parameters, building up a hash of URI
128
+ # query parameters that will be added to the REST API query.
129
+ # @param [Symbol] api_method The REST API method that is being called
130
+ # @param [Hash] query_params The modifications to be made to this
131
+ # particular REST API request
132
+ # @return [Hash] parameters to be passed to execute() method
133
+ def process_request_params(api_method, query_params)
134
+ begin
135
+ this_methods_params = self.class.method_params(api_method.to_sym)
136
+
137
+ request_params = {}
138
+
139
+ this_methods_params.keys.each do |k|
140
+ request_params = build_request_params(request_params: request_params, method_param: k.to_sym, param_value: query_params[k.to_sym], method_params: this_methods_params)
141
+ end
142
+
143
+ request_params
144
+
145
+ rescue StandardError => e
146
+ raise e
147
+ end
148
+ end
149
+
150
+ # @!method build_request_params
151
+ # API requests optionally take parameters that modify their behavior;
152
+ # calling this method builds up a hash of the parameters that will be
153
+ # passed to the request. Essentially, it translates Ruby parameters
154
+ # into the literal strings that will be appended to the HTTP request.
155
+ # @param [Hash] params Parameters for this method
156
+ # @option params [Hash] :request_params Accumulated API request parameters
157
+ # @option params [Symbol] :method_param
158
+ # @option params [Symbol] :param_value
159
+ # @option params [Symbol] :method_params
160
+ # @return [Hash] (see :request_params)
161
+ def build_request_params(params)
162
+
163
+ begin
164
+ # these are the parameters that will eventually be passed to the REST
165
+ # API request; expect these to accumulate over multiple invocations
166
+ # of this method. You must return this hash, either modified or
167
+ # unmodified.
168
+ request_params = params.fetch(:request_params)
169
+
170
+ # this is a Symbol representing the type of modification that will be
171
+ # made to the API request; it's a lookup key
172
+ method_param = params.fetch(:method_param)
173
+
174
+ # this is a String representing the parameter that will be passed to
175
+ # the API request modification (_e.g._ if the modification is "filter
176
+ # by language type", this value specifies the language type)
177
+ param_value = params.fetch(:param_value)
178
+
179
+ # this is a Hash specifying the API parameter being built
180
+ method_params = params.fetch(:method_params)
181
+
182
+ this_requests_params = method_params.fetch(method_param)
183
+
184
+ if param_value.nil?
185
+ request_params
186
+ else
187
+ # default param key is the method param, stringified
188
+ param_key = this_requests_params.fetch(:key, method_param.to_s)
189
+
190
+ param_transform = this_requests_params.fetch(:transform, lambda {|x| x.to_s})
191
+ param_munge = this_requests_params.fetch(:munge, lambda {|x| x})
192
+
193
+ request_params.store(param_key.to_s, param_transform.call(param_munge.call(param_value)))
194
+
195
+ request_params
196
+ end
197
+
198
+ rescue KeyError => e
199
+ raise "unable to find filter options for API method '#{api_method}': #{e.message}"
200
+
201
+ rescue StandardError => e
202
+ raise e
203
+ end
204
+ end
205
+
206
+ # @! method parse_time
207
+ # Parses and validates a time parameter (accepts ISO8601 format and some
208
+ # "natural language" formats), converting to UTC
209
+ # @param [String] time String to be parsed
210
+ # @return [Time] parsed Time object
211
+ def self.parse_time(time)
212
+ begin
213
+ Chronic.parse(time).utc
214
+
215
+ rescue NoMethodError => e
216
+ raise "unable to parse '#{time}' as a time: #{e.message}"
217
+
218
+ rescue StandardError => e
219
+ raise e
220
+ end
221
+ end
222
+
223
+ # @! method format_time
224
+ # Converts a Time object to an ISO8601-formatted String, forcing UTC
225
+ # @param [Time] time Time object to be formatted
226
+ # @return [String] formatted String
227
+ def self.format_time(time)
228
+ begin
229
+ time.utc.strftime('%FT%T') + '+00:00'
230
+
231
+ rescue NoMethodError => e
232
+ raise "unable to parse '#{time}' as a time: #{e.message}"
233
+
234
+ rescue StandardError => e
235
+ raise e
236
+ end
237
+ end
238
+
239
+ end
240
+ end
241
+ end
@@ -0,0 +1,73 @@
1
+ # @markup markdown
2
+ # @title Reliquary::API::KeyTransactions
3
+ # @author Steve Huff
4
+ #
5
+
6
+ module Reliquary
7
+ module API
8
+ class KeyTransactions < Reliquary::API::Base
9
+
10
+ # URI fragment for Applications API endpoint
11
+ URI_FRAGMENT = 'key_transactions.json'
12
+
13
+ # URI method for Applications API endpoint
14
+ URI_METHOD = :get
15
+
16
+ # How to parameterize queries against API endpoint
17
+ # These are for parameters to be added to the query; some endpoints
18
+ # require additional parameters to build the URI fragment.
19
+ METHOD_PARAMS = {
20
+ :list => {
21
+ :name => {
22
+ :key => 'filter[name]',
23
+ },
24
+ :ids => {
25
+ :key => 'filter[ids]',
26
+ :transform => lambda {|x| x.join(',')},
27
+ },
28
+ :page => {},
29
+ },
30
+ :show => {},
31
+ }
32
+
33
+ # @!method list
34
+ # List key transactions, optionally filtering by name or ID
35
+ # @param [Hash] params parameters for listing
36
+ # @option [String] :name New Relic key transaction name to select
37
+ # @option [Array<String>] :ids New Relic key transaction policy IDs to select
38
+ #
39
+ def list(params = {})
40
+ begin
41
+ # this is the "default" Key Transactions method, no overrides
42
+ api_params = {}
43
+
44
+ execute(api_params, {:params => process_request_params(__method__, params)})
45
+
46
+ rescue StandardError => e
47
+ raise e
48
+ end
49
+ end
50
+
51
+ # @!method show
52
+ # Show summary for a single key transaction
53
+ # @param [Hash] params parameters for listing
54
+ # @option params [Integer] :id New Relic key transaction ID
55
+ def show(params = {})
56
+ begin
57
+ id = retrieve_id(params)
58
+
59
+ # HTTP method is the default GET
60
+ # override the URI fragment
61
+ api_params = { :uri_fragment => "key_transactions/#{id}.json" }
62
+
63
+ execute(api_params, {:params => process_request_params(__method__, params)})
64
+
65
+ rescue StandardError => e
66
+ raise e
67
+ end
68
+ end
69
+
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,116 @@
1
+ # @markup markdown
2
+ # @title Reliquary::Client
3
+ # @author Steve Huff
4
+ #
5
+
6
+ require 'multi_json'
7
+ require 'rest-client'
8
+ require 'uri'
9
+
10
+ module Reliquary
11
+ class Client
12
+
13
+ HTTP_METHODS = ['get','put','post','patch','delete','head']
14
+
15
+ # @!attribute [r] api_key
16
+ # @return [String] a [New Relic REST API](https://rpm.newrelic.com/api/explore) key
17
+ attr_reader :api_key
18
+
19
+ # @!attribute [r] api_base
20
+ # @return [URI] the base URI on which additional REST calls will be built
21
+ attr_reader :api_base
22
+
23
+ # @!method initialize(api_key = ENV['NEWRELIC_API_KEY'])
24
+ # Constructor method
25
+ # @param api_key [String] (see api_key)
26
+ # @return [Reliquary::Client] the initialized client
27
+ #
28
+ def initialize(api_key = ENV['NEWRELIC_API_KEY'])
29
+ begin
30
+ @api_key = validate_api_key(api_key)
31
+ @api_base = build_api_base('https://api.newrelic.com/v2/')
32
+
33
+ rescue NoMethodError => e
34
+ false
35
+
36
+ rescue StandardError => e
37
+ raise e
38
+ end
39
+ end
40
+
41
+ # @!method parse(json)
42
+ # Parse returned JSON into a Ruby object
43
+ #
44
+ # @param [String] json JSON-formatted string
45
+ # @return [Object] Ruby object representing JSON-formatted string
46
+ def parse(json)
47
+ begin
48
+ # strip off some layers of nonsense added by Oj
49
+ MultiJson.load(json, :symbolize_keys => true).values[0]
50
+
51
+ rescue StandardError => e
52
+ raise e
53
+ end
54
+ end
55
+
56
+ # @!method method_missing(method_name, *args, &block)
57
+ # Delegate HTTP method calls to RestClient::Resource
58
+ #
59
+ # @param method_name [Symbol] name of method (must be a member of
60
+ # {Reliquary::Client::HTTP_METHODS})
61
+ # @param args [Array] additional method params
62
+ # @param block [Proc] block to which method will yield
63
+ def method_missing(method_name, *args, &block)
64
+ begin
65
+ self.api_base.send(method_name.to_sym, *args, &block)
66
+
67
+ rescue StandardError => e
68
+ raise e
69
+ end
70
+ end
71
+
72
+ def respond_to_missing?(method_name, include_private = false)
73
+ HTTP_METHODS.include?(method_name.to_s) || super
74
+ end
75
+
76
+ private
77
+
78
+ def build_api_base(uri)
79
+ begin
80
+ validated_uri = URI(uri)
81
+
82
+ RestClient::Resource.new(validated_uri.to_s, :headers => auth_header)
83
+
84
+ rescue URI::InvalidURIError => e
85
+ raise "'#{uri}' does not look like a valid URI"
86
+
87
+ rescue StandardError => e
88
+ raise e
89
+ end
90
+ end
91
+
92
+
93
+ def validate_api_key(api_key)
94
+ begin
95
+ if /^[\h]{47}$/ =~ api_key
96
+ api_key
97
+ else
98
+ raise "'#{api_key}' does not look like a valid New Relic REST API key"
99
+ end
100
+
101
+ rescue StandardError => e
102
+ raise e
103
+ end
104
+ end
105
+
106
+ def auth_header(api_key = self.api_key)
107
+ begin
108
+ {:x_api_key => api_key}
109
+
110
+ rescue StandardError => e
111
+ raise e
112
+ end
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,3 @@
1
+ module Reliquary
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'reliquary/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "reliquary"
8
+ spec.version = Reliquary::VERSION
9
+ spec.authors = ["Steve Huff"]
10
+ spec.email = ["shuff@vecna.org"]
11
+
12
+ spec.summary = %q{Client for New Relic API v2.}
13
+ spec.description = %q{The official New Relic Ruby gem only supports API v1.}
14
+ spec.homepage = "https://github.com/hakamadare/rubygem-reliquary"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reliquary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Huff
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The official New Relic Ruby gem only supports API v1.
14
+ email:
15
+ - shuff@vecna.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".ruby-version"
23
+ - ".travis.yml"
24
+ - ".yardopts"
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/reliquary.rb
34
+ - lib/reliquary/api.rb
35
+ - lib/reliquary/api/applications.rb
36
+ - lib/reliquary/api/base.rb
37
+ - lib/reliquary/api/key_transactions.rb
38
+ - lib/reliquary/client.rb
39
+ - lib/reliquary/version.rb
40
+ - reliquary.gemspec
41
+ homepage: https://github.com/hakamadare/rubygem-reliquary
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.4.5.1
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Client for New Relic API v2.
65
+ test_files: []
66
+ has_rdoc: