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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb3c234efb7347577546da62322f8e0be7f7e468
4
- data.tar.gz: dfee53a17404e085fcdc7edfdac20dfcffeae862
3
+ metadata.gz: dce7f0462d683c41764e5fcab83fd0e1ac016412
4
+ data.tar.gz: 572ba0c5e68bbd190a73223ea0490a5ec478c672
5
5
  SHA512:
6
- metadata.gz: 20728ae23d447582f13da324337cbeb50d2469dba56fa2449306c3d8bdd1322935b518a51a92ca477db67959de33920f81d8106d06a75021b501d7be6d19e530
7
- data.tar.gz: 848b93233a3010ace79c7680682638a92778326edfea34db3203d14546ae9f80ff02c0ecf86bdd787f743c40f03bebf3288b942e44b5f49e3259c80c3f1203e0
6
+ metadata.gz: 24e04c8e22b8ea43104338d1765f01a1ca54761068f49c5ef3a50355bde85db544ad7caf5beb26f4dad3803a47e94435d36433bfda8cf16908dbc0681ce9ea49
7
+ data.tar.gz: 6029294259d561e798dee7a92b3d0a11643378b9c4da0efa17d76bb390b1d40809771430f7a27f4268dd0df9149410f3b1f993a91f51c6a99a35c0af6a20ae5a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bernard (0.4.0)
4
+ bernard (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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.4.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 initializer `config/bernard.rb` in your application
25
+ Create a new client on the fly:
27
26
  ```ruby
28
- Bernard::Keen::Client.configure do |client|
29
- client.config = {
30
- uri: URI('https://api.keen.io'),
31
- application_name: '<YOUR APPLICATION NAME>',
32
- project_id: '<YOUR PROJECT ID>',
33
- write_key: '<YOUR WRITE KEY>',
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 1.
46
+ Increment an event that has occurred by 5.
51
47
  ```ruby
52
- client.count('visitors', '10')
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
- ### Easier way to make a client
67
- Instead of using an initializer you could create a new client on the fly:
62
+ ### Initializer
63
+ Create a new initializer `config/bernard.rb` in your application
68
64
  ```ruby
69
- client = Bernard::Keen::Client.new(
70
- uri: URI('https://api.keen.io')
71
- application_name: '<YOUR APPLICATION NAME>'
72
- project_id: '<YOUR PROJECT ID>'
73
- write_key: '<YOUR WRITE KEY>'
74
- ready_key: '<YOUR READ KEY>'
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
@@ -1,13 +1,29 @@
1
1
  module Bernard
2
2
  class Connection
3
- def initialize(uri)
4
- unless uri && uri.kind_of?(URI)
5
- raise(Bernard::ArgumentError, 'could not parse URI')
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 call
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
@@ -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
- private def config_from(args)
40
- if !args.empty?
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
@@ -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).strip.downcase
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
- request = Net::HTTP::Post.new(uri.path)
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
@@ -1,3 +1,3 @@
1
1
  module Bernard
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  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.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Hipkin