bernard 0.4.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +20 -23
- data/lib/bernard/connection.rb +22 -6
- data/lib/bernard/keen/client.rb +16 -16
- data/lib/bernard/keen/methods.rb +3 -13
- data/lib/bernard/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dce7f0462d683c41764e5fcab83fd0e1ac016412
|
4
|
+
data.tar.gz: 572ba0c5e68bbd190a73223ea0490a5ec478c672
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24e04c8e22b8ea43104338d1765f01a1ca54761068f49c5ef3a50355bde85db544ad7caf5beb26f4dad3803a47e94435d36433bfda8cf16908dbc0681ce9ea49
|
7
|
+
data.tar.gz: 6029294259d561e798dee7a92b3d0a11643378b9c4da0efa17d76bb390b1d40809771430f7a27f4268dd0df9149410f3b1f993a91f51c6a99a35c0af6a20ae5a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,7 @@ Currently supporting:
|
|
12
12
|
|
13
13
|
Add this line to your application's Gemfile:
|
14
14
|
```ruby
|
15
|
-
gem 'bernard', '~> 0.
|
15
|
+
gem 'bernard', '~> 1.0.0'
|
16
16
|
```
|
17
17
|
|
18
18
|
And then execute:
|
@@ -21,19 +21,15 @@ $ bundle
|
|
21
21
|
```
|
22
22
|
|
23
23
|
## Basic setup
|
24
|
-
To use Bernard you can either create an initializer or construct it as you need it.
|
25
24
|
|
26
|
-
Create a new
|
25
|
+
Create a new client on the fly:
|
27
26
|
```ruby
|
28
|
-
Bernard::Keen::Client.
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
read_key: '<YOUR READ KEY>'
|
35
|
-
}
|
36
|
-
end
|
27
|
+
client = Bernard::Keen::Client.new(
|
28
|
+
application_name: '<YOUR APPLICATION NAME>'
|
29
|
+
project_id: '<YOUR PROJECT ID>'
|
30
|
+
write_key: '<YOUR WRITE KEY>'
|
31
|
+
ready_key: '<YOUR READ KEY>'
|
32
|
+
)
|
37
33
|
```
|
38
34
|
|
39
35
|
## Usage
|
@@ -47,9 +43,9 @@ client.tick('foo')
|
|
47
43
|
|
48
44
|
### Count
|
49
45
|
|
50
|
-
Increment an event that has occurred by
|
46
|
+
Increment an event that has occurred by 5.
|
51
47
|
```ruby
|
52
|
-
client.count('visitors', '
|
48
|
+
client.count('visitors', '5')
|
53
49
|
```
|
54
50
|
|
55
51
|
### Gauge
|
@@ -63,16 +59,17 @@ client.gauge('office_noise_level', '43')
|
|
63
59
|
|
64
60
|
## Advanced configuration
|
65
61
|
|
66
|
-
###
|
67
|
-
|
62
|
+
### Initializer
|
63
|
+
Create a new initializer `config/bernard.rb` in your application
|
68
64
|
```ruby
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
65
|
+
Bernard::Keen::Client.configure do |client|
|
66
|
+
client.config = {
|
67
|
+
application_name: '<YOUR APPLICATION NAME>',
|
68
|
+
project_id: '<YOUR PROJECT ID>',
|
69
|
+
write_key: '<YOUR WRITE KEY>',
|
70
|
+
read_key: '<YOUR READ KEY>'
|
71
|
+
}
|
72
|
+
end
|
76
73
|
```
|
77
74
|
|
78
75
|
### Run asynchronously
|
data/lib/bernard/connection.rb
CHANGED
@@ -1,13 +1,29 @@
|
|
1
1
|
module Bernard
|
2
2
|
class Connection
|
3
|
-
def initialize(
|
4
|
-
unless uri && uri.kind_of?(URI)
|
5
|
-
raise(Bernard::ArgumentError, 'could not
|
3
|
+
def initialize(client:)
|
4
|
+
unless client.uri && client.uri.kind_of?(URI)
|
5
|
+
raise(Bernard::ArgumentError, 'could not create a connection with URI')
|
6
|
+
end
|
7
|
+
|
8
|
+
@uri = client.uri
|
9
|
+
@write_key = client.write_key
|
10
|
+
@read_key = client.read_key
|
11
|
+
end
|
12
|
+
|
13
|
+
def post(payload)
|
14
|
+
request = Net::HTTP::Post.new(uri.path)
|
15
|
+
request['Authorization'] = write_key
|
16
|
+
request['Content-Type'] = 'application/json'
|
17
|
+
request.body = payload
|
18
|
+
|
19
|
+
begin
|
20
|
+
adapter.request(request)
|
21
|
+
rescue Timeout::Error
|
22
|
+
return false
|
6
23
|
end
|
7
|
-
@uri = uri
|
8
24
|
end
|
9
25
|
|
10
|
-
def
|
26
|
+
def adapter
|
11
27
|
http = Net::HTTP.new(uri.host, uri.port)
|
12
28
|
http.open_timeout = 5
|
13
29
|
http.read_timeout = 5
|
@@ -16,6 +32,6 @@ module Bernard
|
|
16
32
|
end
|
17
33
|
|
18
34
|
private
|
19
|
-
attr_reader :uri
|
35
|
+
attr_reader :uri, :write_key, :read_key
|
20
36
|
end
|
21
37
|
end
|
data/lib/bernard/keen/client.rb
CHANGED
@@ -13,7 +13,6 @@ module Bernard
|
|
13
13
|
|
14
14
|
def config=(args)
|
15
15
|
@config = {
|
16
|
-
uri: args[:uri],
|
17
16
|
application_name: args[:application_name],
|
18
17
|
project_id: args[:project_id],
|
19
18
|
write_key: args[:write_key],
|
@@ -29,27 +28,14 @@ module Bernard
|
|
29
28
|
def initialize(args = {})
|
30
29
|
config = config_from(args)
|
31
30
|
|
32
|
-
@uri = config.fetch(:uri, nil)
|
33
31
|
@application_name = config.fetch(:application_name, nil)
|
34
32
|
@project_id = config.fetch(:project_id, nil)
|
35
33
|
@write_key = config.fetch(:write_key, nil)
|
36
34
|
@read_key = config.fetch(:read_key, nil)
|
37
35
|
end
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
args
|
42
|
-
elsif Bernard::Keen::Client.config
|
43
|
-
Bernard::Keen::Client.config
|
44
|
-
else
|
45
|
-
{}
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def uri=(value)
|
50
|
-
raise(Bernard::ArgumentError, 'Missing URI') unless value
|
51
|
-
raise(Bernard::ArgumentError, 'Invalid URI') unless value.kind_of?(URI)
|
52
|
-
@uri = value
|
37
|
+
def uri
|
38
|
+
@uri ||= URI('https://api.keen.io')
|
53
39
|
end
|
54
40
|
|
55
41
|
def application_name=(value)
|
@@ -70,6 +56,20 @@ module Bernard
|
|
70
56
|
def read_key=(value)
|
71
57
|
@read_key = value
|
72
58
|
end
|
59
|
+
|
60
|
+
def connection
|
61
|
+
Bernard::Connection.new(client: self)
|
62
|
+
end
|
63
|
+
|
64
|
+
private def config_from(args)
|
65
|
+
if !args.empty?
|
66
|
+
args
|
67
|
+
elsif Bernard::Keen::Client.config
|
68
|
+
Bernard::Keen::Client.config
|
69
|
+
else
|
70
|
+
{}
|
71
|
+
end
|
72
|
+
end
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
data/lib/bernard/keen/methods.rb
CHANGED
@@ -13,22 +13,12 @@ module Bernard
|
|
13
13
|
write(:gauge, type: event, value: Float(value))
|
14
14
|
end
|
15
15
|
|
16
|
-
def write(event, metadata)
|
17
|
-
event = String(event).
|
16
|
+
private def write(event, metadata)
|
17
|
+
event = String(event).downcase
|
18
18
|
payload = metadata.merge!(default_params).to_json
|
19
19
|
uri.path = "/3.0/projects/#{project_id}/events/#{event}"
|
20
20
|
|
21
|
-
|
22
|
-
request['Authorization'] = write_key
|
23
|
-
request['Content-Type'] = 'application/json'
|
24
|
-
request.body = payload
|
25
|
-
|
26
|
-
begin
|
27
|
-
connection = Bernard::Connection.new(uri).call
|
28
|
-
connection.request(request)
|
29
|
-
rescue Timeout::Error
|
30
|
-
return false
|
31
|
-
end
|
21
|
+
connection.post(payload)
|
32
22
|
end
|
33
23
|
|
34
24
|
private def default_params
|
data/lib/bernard/version.rb
CHANGED