statsy-app 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in statsy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alan Harper
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.
@@ -0,0 +1,30 @@
1
+ # Statsy
2
+
3
+ Gem to send data to the Statsy time series data collection service (statsyapp.com)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'statsy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install statsy
18
+
19
+ ## Usage
20
+
21
+ 1. Set your api_key and secret_key
22
+ 2. Use the increment method to send data
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc "Run those specs"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,60 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ class Statsy
6
+ VERSION = 0.1
7
+
8
+ class NoAuthParams < StandardError
9
+ end
10
+
11
+ def initialize(api_key = nil, secret_key = nil)
12
+ @api_key = api_key
13
+ @secret_key = secret_key
14
+ end
15
+
16
+ def api_key=(api_key)
17
+ @api_key = api_key
18
+ end
19
+
20
+ def api_key
21
+ @api_key
22
+ end
23
+
24
+ def secret_key=(secret_key)
25
+ @secret_key = secret_key
26
+ end
27
+
28
+ def secret_key
29
+ @secret_key
30
+ end
31
+
32
+ # time, the amount of time in seconds you want this signature to be valid for
33
+ # stream_prefix (optional), if set only allow streams
34
+ def sign(expires, stream_prefix = nil)
35
+ raise NoAuthParams if api_key.nil? || secret_key.nil?
36
+
37
+ Digest::SHA1.base64digest([secret_key, 'POST', stream_prefix, expires].join(""))
38
+ end
39
+
40
+ def increment(stream, value = 1)
41
+ send_data([{ stream: stream, weight: value }])
42
+ end
43
+
44
+ def send_data(streams)
45
+ expires = Time.now.to_i + 600
46
+ params = { api_key: api_key, expires: expires, signature: sign(expires), events: streams }
47
+
48
+ net = Net::HTTP.new("statsyapp.com")
49
+ request = Net::HTTP::Post.new("/api/v1/multiple_event")
50
+
51
+ request.body = JSON.generate(params)
52
+ request["Content-Type"] = "application/json"
53
+
54
+ response = net.start do |http|
55
+ http.request(request)
56
+ end
57
+
58
+ puts response.body
59
+ end
60
+ end
@@ -0,0 +1,16 @@
1
+ require 'statsy'
2
+ require 'fakeweb'
3
+
4
+ FakeWeb.allow_net_connect = false
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+
11
+ # Run specs in random order to surface order dependencies. If you find an
12
+ # order dependency and want to debug it, you can fix the order by providing
13
+ # the seed, which is printed after each run.
14
+ # --seed 1234
15
+ config.order = 'random'
16
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Statsy do
4
+ let(:statsy) { Statsy.new }
5
+
6
+ context "setup" do
7
+ it "should have api_key getter/setter" do
8
+ statsy = Statsy.new
9
+ statsy.api_key = "abc123"
10
+ statsy.api_key.should == "abc123"
11
+ end
12
+
13
+ it "should ahve secret_key getter/setter" do
14
+ statsy.secret_key = "def456"
15
+ statsy.secret_key.should == "def456"
16
+ end
17
+
18
+ it "sets api key & secret key when passed to initializer" do
19
+ statsy = Statsy.new("api key", "secret key")
20
+ statsy.api_key.should == "api key"
21
+ statsy.secret_key.should == "secret key"
22
+ end
23
+ end
24
+
25
+ context "signature calculation" do
26
+ it "calculates correct signature with no stream prefix" do
27
+ statsy.api_key = "abc123"
28
+ statsy.secret_key = "def456"
29
+ statsy.sign(300).should == "6cpng1sPY9htddzYA/2uADMRovA="
30
+ end
31
+
32
+ it "calculates correct signature with stream prefix set" do
33
+ statsy.api_key = "abc123"
34
+ statsy.secret_key = "def456"
35
+ statsy.sign(600, "widgets.melbourne").should == "R1Ro0z/gQw5Mu/R+aKQp7tL7sKE="
36
+ end
37
+
38
+ it "raises Statsy::NoAuthParams when not set" do
39
+ lambda { statsy.increment("test.counter") }.should raise_error(Statsy::NoAuthParams)
40
+ end
41
+ end
42
+
43
+ context "use statsy" do
44
+ before do
45
+ statsy.api_key = "abc123"
46
+ statsy.secret_key = "def456"
47
+ end
48
+
49
+ it "increments with no parameter should increment by one" do
50
+ statsy.should_receive(:send_data).with([{ :stream => "gems.statsy.downloads", :weight => 1 }])
51
+ statsy.increment("gems.statsy.downloads")
52
+ end
53
+
54
+ it "increments a stream by the given parameter" do
55
+ statsy.should_receive(:send_data).with([{ :stream => "gems.statsy.downloads", :weight => 5 }])
56
+ statsy.increment("gems.statsy.downloads", 5)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'statsy'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "statsy-app"
8
+ gem.version = Statsy::VERSION
9
+ gem.authors = ["Alan Harper"]
10
+ gem.email = ["alan@sct.com.au"]
11
+ gem.description = %q{Client gem for Statsy}
12
+ gem.summary = %q{Gem to send data to the Statsy time series data collection service (statsyapp.com)}
13
+ gem.homepage = "http://www.statsyapp.com/"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec"
21
+ gem.add_development_dependency "fakeweb"
22
+ gem.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statsy-app
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alan Harper
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fakeweb
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Client gem for Statsy
63
+ email:
64
+ - alan@sct.com.au
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/statsy.rb
76
+ - spec/spec_helper.rb
77
+ - spec/statsy_spec.rb
78
+ - statsy.gemspec
79
+ homepage: http://www.statsyapp.com/
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.23
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Gem to send data to the Statsy time series data collection service (statsyapp.com)
103
+ test_files:
104
+ - spec/spec_helper.rb
105
+ - spec/statsy_spec.rb