librato-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 451f581cef2f4102c93734ca13fe10bd467e6b7c
4
+ data.tar.gz: 3a544e9e7de8f597fb0cadd3d99633a119da194d
5
+ SHA512:
6
+ metadata.gz: 7cbbf4c94b83f4f77254d8218da1ae9b84460a2cce263029ffb11e359b58b8c2dc480d702d46d798d6ae8873e547aefde1bb4657cceb553d4368fb4318b3e543
7
+ data.tar.gz: 342cf76e5aa4cbfd71332bfb04c8bfa0170d5f31ca3eaddf0d840d631e2cab9004a4454bf222ff3270f2dbaee5462f21f523789d4345194be8668bc22c754e70
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ test.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in librato-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Genki Sugawara
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.
data/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # Librato::Client
2
+
3
+ Librato API Client for for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'librato-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install librato-client
20
+
21
+ ## Usage
22
+
23
+ * [Librato API Documentation — Librato API Documentation](http://dev.librato.com/v1)
24
+ * [RESOURCES](https://github.com/winebarrel/librato-client/blob/master/lib/librato/client/client.rb#L11)
25
+ * [SUB_RESOURCES](https://github.com/winebarrel/librato-client/blob/master/lib/librato/client/resource.rb#L2)
26
+
27
+ ### Create client
28
+
29
+ ```ruby
30
+ client = Librato::Client.new(
31
+ user: '...',
32
+ token: '...'
33
+ # [, Other Options]
34
+ )
35
+ ```
36
+
37
+ * Other Options
38
+ * `:debug`
39
+ * `:expand_pageable_resources`
40
+ * `:raise_error_if_not_exist`
41
+ * `:wrap_faraday_client_error`
42
+ * `:default_alerts_version`
43
+
44
+ ### List metrics
45
+
46
+ ```ruby
47
+ client.metrics.get
48
+ #=> [{"name"=>"login-delay",
49
+ # "display_name"=>nil,
50
+ # "type"=>"gauge",
51
+ # "attributes"=>{"created_by_ua"=>"Ruby Librato Client 0.1.0"},
52
+ # "description"=>nil,
53
+ # "period"=>nil,
54
+ # "source_lag"=>nil}]
55
+ ```
56
+
57
+ ### Show a metric
58
+
59
+ ```ruby
60
+ client.metrics("login-delay").get
61
+ #=> {"name"=>"login-delay",
62
+ # "display_name"=>nil,
63
+ # "type"=>"gauge",
64
+ # "attributes"=>{"created_by_ua"=>"Ruby Librato Client 0.1.0"},
65
+ # "description"=>nil,
66
+ # "period"=>nil,
67
+ # "source_lag"=>nil}
68
+ ```
69
+
70
+ ```ruby
71
+ client.metrics("login-delay").get(
72
+ compose: 'series("*")',
73
+ start_time: 1439108273,
74
+ resolution: 300
75
+ )
76
+
77
+ #=> {"measurements"=>
78
+ # {"foo.bar.com"=>
79
+ # [{"measure_time"=>1439108400,
80
+ # "value"=>3.5,
81
+ # "count"=>5,
82
+ # "min"=>3.5,
83
+ # "max"=>3.5,
84
+ # "sum"=>17.5,
85
+ # "sum_squares"=>61.25},
86
+ # ...
87
+ ```
88
+
89
+ see [Composite Metrics Language Specification – Customer Feedback & Support for Librato](http://support.metrics.librato.com/knowledgebase/articles/337431-composite-metrics-language-specification)
90
+
91
+ ### Create a metric
92
+
93
+ ```ruby
94
+ client.metrics.post({
95
+ gauges: {
96
+ "login-delay" => {
97
+ value: 3.5,
98
+ source: "foo.bar.com"
99
+ }
100
+ }
101
+ }
102
+ ```
103
+
104
+ ### Update a metric
105
+
106
+ ```ruby
107
+ client.metrics("login-delay").put(display_name: "Login delay")
108
+ ```
109
+
110
+ ### Delete a metrics
111
+
112
+ ```ruby
113
+ client.metrics("login-delay").delete
114
+ ```
115
+
116
+ ### List spaces
117
+
118
+ ```ruby
119
+ client.spaces.get
120
+ ```
121
+
122
+ ### Show a space
123
+
124
+ ```ruby
125
+ client.spaces(12345).get
126
+ ```
127
+
128
+ ### Create a space
129
+
130
+ ```ruby
131
+ client.spaces.post(name: "My Space")
132
+ ```
133
+
134
+ ### List charts
135
+
136
+ ```ruby
137
+ client.spaces(12345).charts.get
138
+ ```
139
+
140
+ ### Show a chart
141
+
142
+ ```ruby
143
+ client.spaces(12345).charts(6789).get
144
+ ```
145
+
146
+ ### Create a chart
147
+
148
+ ```ruby
149
+ client.spaces(77109).charts.post(
150
+ name: "My Chart",
151
+ type: "line",
152
+ streams: [...]
153
+ )
154
+ ```
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 "librato/client"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,85 @@
1
+ class Librato::Client
2
+ ENDPOINT = 'https://metrics-api.librato.com'
3
+ API_VERSION = 'v1'
4
+ USER_AGENT = "Ruby Librato Client #{Librato::Client::VERSION}"
5
+
6
+ DEFAULT_ADAPTERS = [
7
+ Faraday::Adapter::NetHttp,
8
+ Faraday::Adapter::Test
9
+ ]
10
+
11
+ RESOURCES = {
12
+ :metrics => [:get, :post, :put, :delete],
13
+ :annotations => [:get, :post, :put, :delete],
14
+ :alerts => [:get, :post, :put, :delete],
15
+ :api_tokens => [:get, :post, :put, :delete],
16
+ :charts => [:get, :post, :delete],
17
+ :dashboards => [:get, :post, :put, :delete],
18
+ :instruments => [:get, :post, :put, :delete],
19
+ :jobs => [:get ],
20
+ :services => [:get, :post, :put, :delete],
21
+ :sources => [:get, :put, :delete],
22
+ :spaces => [:get, :post, :put, :delete, :charts],
23
+ :users => [:get, :post, :put, :delete],
24
+ }
25
+
26
+ RESOURCE_OPTIONS = {
27
+ :expand_pageable_resources => true,
28
+ :raise_error_if_not_exist => false,
29
+ :wrap_faraday_client_error => true,
30
+ :default_alerts_version => 2,
31
+ }
32
+
33
+ def initialize(options)
34
+ unless user = options.delete(:user)
35
+ raise ArgumentError, ':user is required'
36
+ end
37
+
38
+ unless token = options.delete(:token)
39
+ raise ArgumentError, ':token is required'
40
+ end
41
+
42
+ @debug = options.delete(:debug)
43
+ @resource_options = {}
44
+
45
+ RESOURCE_OPTIONS.each do |key, default_value|
46
+ if options.has_key?(key)
47
+ @resource_options[key] = options.delete(key)
48
+ else
49
+ @resource_options[key] = default_value
50
+ end
51
+ end
52
+
53
+ options[:url] ||= ENDPOINT
54
+
55
+ @conn = Faraday.new(options) do |faraday|
56
+ faraday.request :url_encoded
57
+ faraday.response :json, :content_type => /\bjson\b/
58
+ faraday.response :raise_error
59
+ faraday.response :logger if @debug
60
+
61
+ faraday.basic_auth user, token
62
+
63
+ yield(faraday) if block_given?
64
+
65
+ unless DEFAULT_ADAPTERS.any? {|i| faraday.builder.handlers.include?(i) }
66
+ faraday.adapter Faraday.default_adapter
67
+ end
68
+ end
69
+
70
+ @conn.headers[:user_agent] = USER_AGENT
71
+ end
72
+
73
+ def method_missing(name, *args)
74
+ unless RESOURCES.has_key?(name)
75
+ raise NoMethodError, "undefined method: #{name} for #{self.inspect}"
76
+ end
77
+
78
+ unless (0..1).include?(args.length)
79
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 0..1)"
80
+ end
81
+
82
+ Librato::Client::Resource.new(
83
+ @conn, nil, name, args[0], RESOURCES[name], @resource_options)
84
+ end
85
+ end
@@ -0,0 +1,8 @@
1
+ class Librato::Client::Error < StandardError
2
+ attr_reader :cause
3
+
4
+ def initialize(cause)
5
+ super(cause.response)
6
+ @cause = cause
7
+ end
8
+ end
@@ -0,0 +1,60 @@
1
+ class Librato::Client::PageableResources
2
+ include Enumerable
3
+
4
+ def initialize(resource, name, method_name, params, body)
5
+ @resource = resource
6
+ @name = name
7
+ @method_name = method_name
8
+ @params = params
9
+ @body = body
10
+ end
11
+
12
+ def resources
13
+ @body[@name.to_s]
14
+ end
15
+
16
+ def length
17
+ @body['query']['length'] || 0
18
+ end
19
+
20
+ def offset
21
+ @body['query']['offset'] || 0
22
+ end
23
+
24
+ def total
25
+ @body['query']['total'] || 0
26
+ end
27
+
28
+ def found
29
+ @body['query']['found'] || 0
30
+ end
31
+
32
+ def has_next?
33
+ offset + length < found
34
+ end
35
+
36
+ def next_page
37
+ params = @params.merge(:offset => offset + length)
38
+ @resource.request(@method_name, params, :expand_pageable_resources => false)
39
+ end
40
+
41
+ def each
42
+ page = self
43
+
44
+ loop do
45
+ yield(page)
46
+ break unless page.has_next?
47
+ page = page.next_page
48
+ end
49
+ end
50
+
51
+ def each_resource(&block)
52
+ if block
53
+ self.each do |page|
54
+ page.resources.each(&block)
55
+ end
56
+ else
57
+ self.enum_for(:each_resource)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,96 @@
1
+ class Librato::Client::Resource
2
+ SUB_RESOURCES = {
3
+ :charts => [:get, :post, :put, :delete],
4
+ }
5
+
6
+ def initialize(conn, root, name, resource_id, methods, options = {})
7
+ @conn = conn
8
+ @root = root
9
+ @name = name
10
+ @resource_id = resource_id
11
+ @methods = methods
12
+ @options = options
13
+ end
14
+
15
+ def request(method_name, params = {}, options = {})
16
+ options = @options.merge(options)
17
+
18
+ begin
19
+ url = [Librato::Client::API_VERSION]
20
+ url << @root if @root
21
+ url << @name
22
+ url << @resource_id if @resource_id
23
+
24
+ if @name == :alerts and options[:default_alerts_version]
25
+ params[:version] = options[:default_alerts_version]
26
+ end
27
+
28
+ res = @conn.send(method_name) do |req|
29
+ req.url url.join('/')
30
+
31
+ if [:post, :put].include?(method_name)
32
+ req.headers['Content-Type'] = 'application/json'
33
+ req.body = JSON.dump(params)
34
+ else
35
+ req.params = params
36
+ end
37
+ end
38
+
39
+ if res.body.kind_of?(Hash) and res.body.has_key?('query')
40
+ pageable_resources = Librato::Client::PageableResources.new(
41
+ self, @name, method_name, params, res.body)
42
+
43
+ if options[:expand_pageable_resources]
44
+ pageable_resources.each_resource.to_a
45
+ else
46
+ pageable_resources
47
+ end
48
+ else
49
+ res.body
50
+ end
51
+ rescue Faraday::ClientError => e
52
+ if not options[:raise_error_if_not_exist] and e.kind_of?(Faraday::ResourceNotFound)
53
+ nil
54
+ else
55
+ if options[:wrap_faraday_client_error]
56
+ e = Librato::Client::Error.new(e)
57
+ end
58
+
59
+ raise e
60
+ end
61
+ end
62
+ end
63
+
64
+ def method_missing(name, *args, &block)
65
+ unless @methods.include?(name)
66
+ raise NoMethodError, "undefined method: #{name} for #{self.inspect}"
67
+ end
68
+
69
+ if SUB_RESOURCES.has_key?(name)
70
+ unless (0..1).include?(args.length)
71
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 0..1)"
72
+ end
73
+
74
+ root = [@name]
75
+ root << @resource_id if @resource_id
76
+
77
+ Librato::Client::Resource.new(
78
+ @conn, root.join('/'), name, args[0], SUB_RESOURCES[name], @options)
79
+ else
80
+ unless (0..2).include?(args.length)
81
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 0..2)"
82
+ end
83
+
84
+ params = args[0]
85
+ options = args[1]
86
+
87
+ [params, options].each do |arg|
88
+ if not arg.nil? and not arg.kind_of?(Hash)
89
+ raise TypeError, "wrong argument: #{arg.inspect} (expected Hash)"
90
+ end
91
+ end
92
+
93
+ request(name, params || {}, options || {}, &block)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,5 @@
1
+ module Librato
2
+ class Client
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'json'
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ require 'librato/client/version'
7
+ require 'librato/client/client'
8
+ require 'librato/client/error'
9
+ require 'librato/client/pageable_resources'
10
+ require 'librato/client/resource'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'librato/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'librato-client'
8
+ spec.version = Librato::Client::VERSION
9
+ spec.authors = ['Genki Sugawara']
10
+ spec.email = ['sgwr_dts@yahoo.co.jp']
11
+
12
+ spec.summary = %q{Librato API Client for for Ruby.}
13
+ spec.description = %q{Librato API Client for for Ruby.}
14
+ spec.homepage = 'https://github.com/winebarrel/librato-client'
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
+
22
+ spec.add_dependency 'faraday'
23
+ spec.add_dependency 'faraday_middleware'
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec', '>= 3.0.0'
28
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: librato-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ description: Librato API Client for for Ruby.
84
+ email:
85
+ - sgwr_dts@yahoo.co.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/librato/client.rb
100
+ - lib/librato/client/client.rb
101
+ - lib/librato/client/error.rb
102
+ - lib/librato/client/pageable_resources.rb
103
+ - lib/librato/client/resource.rb
104
+ - lib/librato/client/version.rb
105
+ - librato-client.gemspec
106
+ homepage: https://github.com/winebarrel/librato-client
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.14
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Librato API Client for for Ruby.
130
+ test_files: []