hg-api 0.0.1

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: 707dd430082262283db96436e1b07a8c0cca779a
4
+ data.tar.gz: aeaf032ff4ca32998942c0b928e1a30e0715070b
5
+ SHA512:
6
+ metadata.gz: 0b60ca7188810a394c264785f9f20a2fab5bfd81f13f9eb401bb904439c649494c2ef1f70221f18e66ff8d17fed9a3d331e703c319b82217d6b6ab6b2df9038e
7
+ data.tar.gz: b29693de3c2080a4faab643d20cb51eacbac2a1fe7e25604a395e879e1edbbe5d9c1e9b89497075b6d4a33f130398d8bd9c5bad9a8e94bb9de3203b16215921b
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hg-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Artyom Keydunov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Hg::Api
2
+
3
+ Ruby API client for Hosted Graphite.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hg-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hg-api
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ client = HGAPI.new
23
+ client.metric("signup", 1)
24
+ client.metric("load", 100, via: :tcp)
25
+ ```
26
+
27
+ Default transport is udp, it can be changed by passing options hash while
28
+ constructing client:
29
+
30
+ ```ruby
31
+ client = HGAPI.new(via: :http)
32
+ client.metric("this.metric.send.over.http", 5)
33
+ ```
34
+
35
+ Supported transports: udp, tcp, http.
36
+
37
+ API KEY must be present as environmental variable HOSTED_GRAPHITE_API_KEY.
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/hg-api/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/hg-api.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hg_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hg-api"
8
+ spec.version = HGAPI::VERSION
9
+ spec.authors = ["Artyom Keydunov", "Alexey Vakhov"]
10
+ spec.email = ["artyom.keydunov@uchi.ru", "vakhov@uchi.ru"]
11
+ spec.summary = %q{Ruby API client for Hosted Graphite.}
12
+ spec.description = %q{Ruby API client for Hosted Graphite.}
13
+ spec.homepage = "https://github.com/uchiru/hg-api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
data/lib/hg-api.rb ADDED
@@ -0,0 +1 @@
1
+ require 'hg_api'
@@ -0,0 +1,59 @@
1
+ require 'socket'
2
+ require 'net/http'
3
+
4
+ module HGAPI
5
+ class Client
6
+ SUPPORTED_TRANSPORTS = [:udp, :tcp, :http]
7
+ HTTP_URI ="https://hostedgraphite.com/api/v1/sink"
8
+ HOST = 'carbon.hostedgraphite.com'
9
+ PORT = 2003
10
+
11
+ def initialize(options = {})
12
+ @default_transport = check_transport!(options[:via]) || :udp
13
+ @api_key = ENV["HOSTED_GRAPHITE_API_KEY"]
14
+ @disabled = @api_key.nil?
15
+ end
16
+
17
+ def metric(key, value, options = {})
18
+ return if @disabled
19
+ send_metric(key, value, check_transport!(options[:via]) || @default_transport)
20
+ end
21
+
22
+ private
23
+
24
+ def check_transport!(transport)
25
+ if transport && !SUPPORTED_TRANSPORTS.include?(transport.to_sym)
26
+ raise "#{transport} is unsupported transport"
27
+ end
28
+ transport
29
+ end
30
+
31
+ def send_metric(key, value, transport)
32
+ self.send("send_metric_#{transport}", key, value)
33
+ end
34
+
35
+ def send_metric_udp(key, value)
36
+ sock = UDPSocket.new
37
+ sock.send "#{@api_key}.#{key} #{value}\n", 0, HOST, PORT
38
+ sock.close
39
+ end
40
+
41
+ def send_metric_tcp(key, value)
42
+ conn = TCPSocket.new HOST, PORT
43
+ conn.puts "#{@api_key}.#{key} #{value}\n"
44
+ conn.close
45
+ end
46
+
47
+ def send_metric_http(key, value)
48
+ uri = URI(HTTP_URI)
49
+
50
+ req = Net::HTTP::Post.new(uri.request_uri)
51
+ req.basic_auth @api_key, nil
52
+ req.body = "#{key} #{value}"
53
+
54
+ res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
55
+ http.request(req)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module HGAPI
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hg_api.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "hg_api/version"
2
+ require "hg_api/client"
3
+
4
+ # Usage example:
5
+ # client = HGAPI.new
6
+ # client.metric("signup", 1)
7
+ # client.metric("load", 100, via: :tcp)
8
+ #
9
+ # Default transport is udp, it can be changed by passing options hash while
10
+ # constructing client:
11
+ #
12
+ # client = HGAPI.new(via: :http)
13
+ # client.metric("this.metric.send.over.http", 5)
14
+ #
15
+ # Supported transports: udp, tcp, http.
16
+ #
17
+ # API KEY must be present as environmental variable HOSTED_GRAPHITE_API_KEY.
18
+
19
+ module HGAPI
20
+ def self.version
21
+ HGAPI::VERSION
22
+ end
23
+
24
+ def self.new options = {}
25
+ Client.new options
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hg-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Artyom Keydunov
8
+ - Alexey Vakhov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.6'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: Ruby API client for Hosted Graphite.
43
+ email:
44
+ - artyom.keydunov@uchi.ru
45
+ - vakhov@uchi.ru
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - hg-api.gemspec
56
+ - lib/hg-api.rb
57
+ - lib/hg_api.rb
58
+ - lib/hg_api/client.rb
59
+ - lib/hg_api/version.rb
60
+ homepage: https://github.com/uchiru/hg-api
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Ruby API client for Hosted Graphite.
84
+ test_files: []
85
+ has_rdoc: