bernard 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd466e27be29274756368250e35c489fd65c4a37
4
- data.tar.gz: 14875501d58f8ffafd0f611488695162d4f75b64
3
+ metadata.gz: 1960eb4876f61e949ae9a22b0ee62b0122033ed1
4
+ data.tar.gz: e1062af9819a7ccd7ddaadbde2b8c49b606cd6e0
5
5
  SHA512:
6
- metadata.gz: 11a5fe4879d483b4e5f536a05076f1b9cbd83c87d2a48f20f996ce197c3f9cb9c22a4b6bce89447cce271652f1d404631c347dcf2eac6e6e692a9db8c0433176
7
- data.tar.gz: 14776960c759e3e69ba0e3de42222a5e573b275f90270f24146825ceae653e8925192480bb9d7f210f4fd87a1b9ecc3d76ef488d3d2877b75a60489abed8e5ff
6
+ metadata.gz: 01787a42c7823b47fcd35b8d43cf3eb032f6c91d69cb2ceaa65b51433b118c2228b7bd425153ac147d1292e2d0c9ce634c842b67a4a5cd6156c141e04f0e7069
7
+ data.tar.gz: 94fe2d382009a6bf7cecfd2bbfc313b92678da488bbe81cedd0e8e5b3d9ef3c667798e812c6cce1542f5f191f518f0ae60799220fac4105e0107fdd4c273e28b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bernard (0.0.0)
4
+ bernard (0.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -43,10 +43,10 @@ PLATFORMS
43
43
 
44
44
  DEPENDENCIES
45
45
  bernard!
46
- pry
47
- rake
48
- rspec
49
- webmock
46
+ pry (= 0.10.3)
47
+ rake (= 11.1.2)
48
+ rspec (= 3.4.0)
49
+ webmock (= 1.24.2)
50
50
 
51
51
  BUNDLED WITH
52
- 1.12.2
52
+ 1.12.3
data/README.md CHANGED
@@ -4,6 +4,62 @@ Sends event data to visualisation services.
4
4
 
5
5
  ![bernard](http://dogkeg.com/wp-content/uploads/2015/10/saint_bernard__dog_keg_barrel-30-640x427-e1446127647535.jpg)
6
6
 
7
+
8
+ ## Install
9
+
10
+ Add this line to your application's Gemfile:
11
+ ```ruby
12
+ gem 'bernard', '~> 0.2.0'
13
+ ```
14
+
15
+ And then execute:
16
+ ```sh
17
+ $ bundle
18
+ ```
19
+
20
+ ## Setup
21
+ To use Bernard you can either create an initializer or construct it as you need it.
22
+
23
+ Create a new initializer `config/bernard.rb` in your application
24
+ ```ruby
25
+ Bernard::Keen::Client.configure do |client|
26
+ client.config = {
27
+ uri: URI('https://api.keen.io'),
28
+ project_id: '<YOUR PROJECT ID>',
29
+ write_key: '<YOUR WRITE KEY>',
30
+ read_key: '<YOUR READ KEY>'
31
+ }
32
+ end
33
+ ```
34
+
35
+ OR
36
+
37
+ Create a new client on the fly:
38
+ ```ruby
39
+ client = Bernard::Keen::Client.new(
40
+ uri: URI('https://api.keen.io')
41
+ project_id: '<YOUR PROJECT ID>'
42
+ write_key: '<YOUR WRITE KEY>'
43
+ ready_key: '<YOUR READ KEY>'
44
+ )
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ### Tick
50
+
51
+ Increment an event that has occurred by 1.
52
+ ```ruby
53
+ client.tick('foo')
54
+ ```
55
+
56
+ ### Gauge
57
+
58
+ Update an event to a new value.
59
+ ```ruby
60
+ client.gauge('office_noise_level', '43')
61
+ ```
62
+
7
63
  ## License
8
64
 
9
65
  (c) 2016 The Dextrous Web Ltd. Released under the MIT license.
data/bernard.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'bernard/version'
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = 'bernard'
7
7
  gem.version = Bernard::VERSION
8
- gem.date = '2016-05-11'
8
+ gem.date = '2016-05-17'
9
9
  gem.summary = 'Sends event data to visualisation services'
10
10
  gem.description = 'Sends event data to visualisation services.'
11
11
  gem.authors = ['Tom Hipkin', 'Robert Lee-Cann']
@@ -0,0 +1,21 @@
1
+ module Bernard
2
+ class Connection
3
+ def initialize(uri)
4
+ unless uri && uri.kind_of?(URI)
5
+ raise(Bernard::ArgumentError, 'could not parse URI')
6
+ end
7
+ @uri = uri
8
+ end
9
+
10
+ def call
11
+ http = Net::HTTP.new(uri.host, uri.port)
12
+ http.open_timeout = 5
13
+ http.read_timeout = 5
14
+ http.use_ssl = true
15
+ http
16
+ end
17
+
18
+ private
19
+ attr_reader :uri
20
+ end
21
+ end
@@ -1,3 +1,4 @@
1
- class Bernard
1
+ module Bernard
2
2
  class Exception < StandardError; end
3
+ class ArgumentError < StandardError; end
3
4
  end
@@ -0,0 +1,93 @@
1
+ require 'bernard/connection'
2
+
3
+ module Bernard
4
+ module Keen
5
+ class Client
6
+
7
+ class << self
8
+ attr_reader :config
9
+
10
+ def configure
11
+ yield self
12
+ end
13
+
14
+ def config=(args)
15
+ @config = {
16
+ uri: args[:uri],
17
+ project_id: args[:project_id],
18
+ write_key: args[:write_key],
19
+ read_key: args[:read_key]
20
+ }
21
+ end
22
+ end
23
+
24
+ attr_reader :uri, :project_id, :write_key, :read_key
25
+
26
+ def initialize(args = {})
27
+ config = config_from(args)
28
+
29
+ @uri = config.fetch(:uri, nil)
30
+ @project_id = config.fetch(:project_id, nil)
31
+ @write_key = config.fetch(:write_key, nil)
32
+ @read_key = config.fetch(:read_key, nil)
33
+ end
34
+
35
+ private def config_from(args)
36
+ if !args.empty?
37
+ args
38
+ elsif Bernard::Keen::Client.config
39
+ Bernard::Keen::Client.config
40
+ else
41
+ {}
42
+ end
43
+ end
44
+
45
+ def uri=(value)
46
+ raise(Bernard::ArgumentError, 'Missing URI') unless value
47
+ raise(Bernard::ArgumentError, 'Invalid URI') unless value.kind_of?(URI)
48
+ @uri = value
49
+ end
50
+
51
+ def project_id=(value)
52
+ raise(Bernard::ArgumentError, 'Missing project_id') unless value
53
+ @project_id = value
54
+ end
55
+
56
+ def write_key=(value)
57
+ raise(Bernard::ArgumentError, 'Missing write_key') unless value
58
+ @write_key = value
59
+ end
60
+
61
+ def read_key=(value)
62
+ @read_key = value
63
+ end
64
+
65
+ def tick(event)
66
+ write(:tick, type: event, count: 1)
67
+ end
68
+
69
+ def gauge(event, value)
70
+ write(:gauge, type: event, value: Float(value))
71
+ end
72
+
73
+ def write(event, metadata)
74
+
75
+ event = String(event).strip.downcase
76
+ uri.path = "/3.0/projects/#{project_id}/events/#{event}"
77
+
78
+ request = Net::HTTP::Post.new(uri.path)
79
+ request['Authorization'] = write_key
80
+ request['Content-Type'] = 'application/json'
81
+ request.body = metadata.to_json
82
+
83
+ begin
84
+ connection = Bernard::Connection.new(uri).call
85
+ response = connection.request(request)
86
+ # puts "Bernards #{event} response was #{response.code}"
87
+ rescue Timeout::Error
88
+ return false
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -1,5 +1,4 @@
1
- class Bernard
1
+ module Bernard
2
2
  class Schema
3
-
4
3
  end
5
4
  end
@@ -1,3 +1,3 @@
1
- class Bernard
2
- VERSION = '0.1.0'.freeze
1
+ module Bernard
2
+ VERSION = '0.2.0'.freeze
3
3
  end
data/lib/bernard.rb CHANGED
@@ -1,59 +1,12 @@
1
1
  require 'bernard/exception'
2
2
  require 'bernard/schema'
3
3
  require 'bernard/version'
4
+ require 'bernard/connection'
5
+ require 'bernard/keen/client'
4
6
 
5
7
  require 'json'
6
8
  require 'net/https'
7
9
  require 'uri'
8
10
 
9
- class Bernard
10
- BASE_URI = 'https://api.keen.io'
11
-
12
- attr_reader :project_id, :write_key
13
-
14
- def project_id=(id)
15
- project_id = String(id).strip.downcase
16
- raise Bernard::Exception unless !!(project_id =~ /\A[0-9a-f]{24}\Z/)
17
-
18
- @project_id = project_id
19
- end
20
-
21
- def write_key=(key)
22
- write_key = String(key).strip.downcase
23
- raise Bernard::Exception unless !!(write_key =~ /\A[0-9a-f]{192}\Z/)
24
-
25
- @write_key = write_key
26
- end
27
-
28
- def publish(event, metadata)
29
- event = String(event).strip.downcase
30
-
31
- uri = URI.parse(BASE_URI)
32
-
33
- http = Net::HTTP.new(uri.host, uri.port)
34
- http.open_timeout = 5
35
- http.read_timeout = 5
36
- http.use_ssl = true
37
-
38
- request = Net::HTTP::Post.new("/3.0/projects/#{project_id}/events/#{event}")
39
- request['Authorization'] = write_key
40
- request['Content-Type'] = 'application/json'
41
- request.body = metadata.to_json
42
-
43
- begin
44
- response = http.request(request)
45
- rescue Timeout::Error
46
- return false
47
- end
48
-
49
- response.code == '201'
50
- end
51
-
52
- def tick(event)
53
- publish(:tick, type: event, count: 1)
54
- end
55
-
56
- def gauge(event, value)
57
- publish(:gauge, type: event, value: Float(value))
58
- end
11
+ module Bernard
59
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bernard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Hipkin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-11 00:00:00.000000000 Z
12
+ date: 2016-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -87,10 +87,12 @@ files:
87
87
  - bin/.keep
88
88
  - lib/bernard.rb
89
89
  - lib/bernard/.keep
90
+ - lib/bernard/connection.rb
90
91
  - lib/bernard/exception.rb
92
+ - lib/bernard/keen/.keep
93
+ - lib/bernard/keen/client.rb
91
94
  - lib/bernard/schema.rb
92
95
  - lib/bernard/version.rb
93
- - test/.keep
94
96
  homepage: http://rubygems.org/gems/bernard
95
97
  licenses:
96
98
  - MIT
File without changes