Class: Statsy
- Inherits:
-
Object
- Object
- Statsy
- Defined in:
- lib/statsy.rb
Defined Under Namespace
Classes: NoAuthParams
Constant Summary collapse
- VERSION =
0.1
Instance Method Summary collapse
- #api_key ⇒ Object
- #api_key=(api_key) ⇒ Object
- #increment(stream, value = 1) ⇒ Object
-
#initialize(api_key = nil, secret_key = nil) ⇒ Statsy
constructor
A new instance of Statsy.
- #secret_key ⇒ Object
- #secret_key=(secret_key) ⇒ Object
- #send_data(streams) ⇒ Object
-
#sign(expires, stream_prefix = nil) ⇒ Object
time, the amount of time in seconds you want this signature to be valid for stream_prefix (optional), if set only allow streams.
Constructor Details
#initialize(api_key = nil, secret_key = nil) ⇒ Statsy
Returns a new instance of Statsy.
11 12 13 14 |
# File 'lib/statsy.rb', line 11 def initialize(api_key = nil, secret_key = nil) @api_key = api_key @secret_key = secret_key end |
Instance Method Details
#api_key ⇒ Object
20 21 22 |
# File 'lib/statsy.rb', line 20 def api_key @api_key end |
#api_key=(api_key) ⇒ Object
16 17 18 |
# File 'lib/statsy.rb', line 16 def api_key=(api_key) @api_key = api_key end |
#increment(stream, value = 1) ⇒ Object
40 41 42 |
# File 'lib/statsy.rb', line 40 def increment(stream, value = 1) send_data([{ stream: stream, weight: value }]) end |
#secret_key ⇒ Object
28 29 30 |
# File 'lib/statsy.rb', line 28 def secret_key @secret_key end |
#secret_key=(secret_key) ⇒ Object
24 25 26 |
# File 'lib/statsy.rb', line 24 def secret_key=(secret_key) @secret_key = secret_key end |
#send_data(streams) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/statsy.rb', line 44 def send_data(streams) expires = Time.now.to_i + 600 params = { api_key: api_key, expires: expires, signature: sign(expires), events: streams } net = Net::HTTP.new("statsyapp.com") request = Net::HTTP::Post.new("/api/v1/multiple_event") request.body = JSON.generate(params) request["Content-Type"] = "application/json" response = net.start do |http| http.request(request) end puts response.body end |
#sign(expires, stream_prefix = nil) ⇒ Object
time, the amount of time in seconds you want this signature to be valid for stream_prefix (optional), if set only allow streams
34 35 36 37 38 |
# File 'lib/statsy.rb', line 34 def sign(expires, stream_prefix = nil) raise NoAuthParams if api_key.nil? || secret_key.nil? Digest::SHA1.base64digest([secret_key, 'POST', stream_prefix, expires].join("")) end |