cube-tapjoy 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +87 -0
- data/Rakefile +12 -0
- data/cube-tapjoy.gemspec +19 -0
- data/lib/cube.rb +95 -0
- data/lib/cube/version.rb +3 -0
- data/realtest.rb +6 -0
- data/spec/cube_spec.rb +133 -0
- data/spec/helper.rb +37 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Cody Krieger
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# cube-ruby
|
2
|
+
|
3
|
+
A Cube client for Ruby (http://square.github.com/cube). Heavily based on
|
4
|
+
[this statsd ruby client][sdc].
|
5
|
+
|
6
|
+
MIT licensed. See [the LICENSE file][license] for more details.
|
7
|
+
for more details.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Since Cube is under constant development, it's best if you specify a particular
|
12
|
+
version of cube-ruby in your `Gemfile`. That way, you can ensure your Ruby
|
13
|
+
application and your Cube instance will be compatible with each other.
|
14
|
+
|
15
|
+
Here's a version compatibility table:
|
16
|
+
|
17
|
+
Cube | cube-ruby
|
18
|
+
--------------------
|
19
|
+
0.2.0 | 0.0.1
|
20
|
+
0.2.1 | 0.0.2 (master)
|
21
|
+
|
22
|
+
Add this line to your application's `Gemfile`:
|
23
|
+
|
24
|
+
gem 'cube-ruby', require: "cube"
|
25
|
+
|
26
|
+
# or specify a particular version:
|
27
|
+
gem 'cube-ruby', '0.0.2', require: "cube"
|
28
|
+
|
29
|
+
And then execute:
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
$ gem install cube-ruby
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
### Set up a global Cube client
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# use default hostname and port of localhost:1180
|
43
|
+
$cube = Cube::Client.new
|
44
|
+
|
45
|
+
# use custom hostname and port
|
46
|
+
$cube = Cube::Client.new 'cube.example.org', 2280
|
47
|
+
```
|
48
|
+
|
49
|
+
### Send Cube some metrics!
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# send a new "foo" event to cube
|
53
|
+
$cube.send "foo"
|
54
|
+
|
55
|
+
# send a new event to cube that looks like this:
|
56
|
+
# { type: "request", data: { value: "somevalue" } }
|
57
|
+
$cube.send "request", value: "somevalue"
|
58
|
+
|
59
|
+
# optionally specify a specific date/time (two days ago)
|
60
|
+
$cube.send "request", DateTime.now - 2, value: "othervalue"
|
61
|
+
|
62
|
+
# specify an event id (https://github.com/square/cube/wiki/Events)
|
63
|
+
event_id = 42
|
64
|
+
$cube.send "request", DateTime.now, event_id, duration_ms: 234
|
65
|
+
```
|
66
|
+
|
67
|
+
Yes, the method is called `send`. You can still call `Object#send` on
|
68
|
+
`Cube::Client` objects by using the `__send__` method, [per the Ruby docs][rd].
|
69
|
+
|
70
|
+
## Testing
|
71
|
+
|
72
|
+
Run the specs with `rake`.
|
73
|
+
|
74
|
+
To include real UDP socket testing in the specs, run `LIVE=true rake`.
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
1. Fork it
|
79
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
81
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
+
5. Create new Pull Request
|
83
|
+
|
84
|
+
[sdc]: https://github.com/github/statsd-ruby
|
85
|
+
[license]: https://github.com/codykrieger/cube-ruby/blob/master/LICENSE
|
86
|
+
[rd]: http://ruby-doc.org/core-1.9.3/Object.html#method-i-send
|
87
|
+
|
data/Rakefile
ADDED
data/cube-tapjoy.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/cube/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Cody Krieger"]
|
6
|
+
gem.email = ["cody@codykrieger.com"]
|
7
|
+
gem.description = %q{A Cube client for Ruby (http://square.github.com/cube).}
|
8
|
+
gem.summary = %q{A Cube client for Ruby (http://square.github.com/cube).}
|
9
|
+
gem.homepage = "https://github.com/Tapjoy/cube-ruby"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "cube-tapjoy"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Cube::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "minitest"
|
19
|
+
end
|
data/lib/cube.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'cube/version'
|
2
|
+
|
3
|
+
module Cube
|
4
|
+
require 'date'
|
5
|
+
# We'll be sending data to Cube over a UDP socket.
|
6
|
+
require 'socket'
|
7
|
+
# Cube requires data to be in JSON format.
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
class Client
|
11
|
+
# A namespace to prepend to all Cube calls.
|
12
|
+
attr_accessor :namespace
|
13
|
+
|
14
|
+
# We'll use this to eliminate any unwanted/disallowed characters from our
|
15
|
+
# event type later on.
|
16
|
+
RESERVED_CHARS_REGEX = /[^\w\d]/
|
17
|
+
|
18
|
+
class << self
|
19
|
+
# Set to any logger instance that responds to #debug and #error (like the
|
20
|
+
# Rails or stdlib logger) to enable metric logging.
|
21
|
+
attr_accessor :logger
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set 'er up with a host and port, defaults to `localhost:1180`.
|
25
|
+
#
|
26
|
+
# @param [String] The hostname to send metrics to.
|
27
|
+
# @param [Integer] The UDP port to send metrics to.
|
28
|
+
def initialize(host="localhost", port=1180)
|
29
|
+
@host, @port = host, port
|
30
|
+
end
|
31
|
+
|
32
|
+
# The primary endpoint for sending metrics to Cube.
|
33
|
+
#
|
34
|
+
# @param [String] The desired name of the new Cube event.
|
35
|
+
# @param [Array] A splat that takes an optional DateTime, an event id
|
36
|
+
# (typically an integer, but can be any object), and a Hash of data.
|
37
|
+
def send(type, *args)
|
38
|
+
time = nil
|
39
|
+
id = nil
|
40
|
+
data = nil
|
41
|
+
|
42
|
+
until args.empty?
|
43
|
+
arg = args.shift
|
44
|
+
|
45
|
+
if arg.is_a? DateTime
|
46
|
+
time ||= arg
|
47
|
+
elsif arg.is_a? Hash
|
48
|
+
data ||= arg
|
49
|
+
else
|
50
|
+
id ||= arg
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Send off our parsed arguments to be further massaged and socketized.
|
55
|
+
actual_send type, time, id, data
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Actually send the given data to a socket, and potentially log it along
|
61
|
+
# the way.
|
62
|
+
#
|
63
|
+
# @param [String] The desired name of the new Cube event.
|
64
|
+
# @param [DateTime] Optional. A specific time when the Cube event occurred.
|
65
|
+
# @param [Object] Optional. Typically an integer, but can be any object.
|
66
|
+
# @param [Hash] Optional. Anything in this hash will be stored in the
|
67
|
+
# `data` subdocument of the Cube event.
|
68
|
+
def actual_send(type, time, id, data)
|
69
|
+
# Namespace support!
|
70
|
+
prefix = "#{@namespace}_" unless @namespace.nil?
|
71
|
+
# Get rid of any unwanted characters, and replace each of them with an _.
|
72
|
+
type = type.to_s.gsub RESERVED_CHARS_REGEX, '_'
|
73
|
+
|
74
|
+
# Start constructing the message to be sent to Cube over UDP.
|
75
|
+
message = {
|
76
|
+
:type => "#{prefix}#{type}"
|
77
|
+
}
|
78
|
+
message[:time] = time.iso8601 unless time.nil?
|
79
|
+
message[:id] = id unless id.nil?
|
80
|
+
message[:data] = data unless data.nil?
|
81
|
+
|
82
|
+
# JSONify it, log it, and send it off.
|
83
|
+
message_str = message.to_json
|
84
|
+
self.class.logger.debug { "Cube: #{message_str}" } if self.class.logger
|
85
|
+
|
86
|
+
socket.send message_str, 0, @host, @port
|
87
|
+
rescue => err
|
88
|
+
self.class.logger.error { "Cube: #{err.class} #{err}" } if self.class.logger
|
89
|
+
end
|
90
|
+
|
91
|
+
# Helper for getting the socket. `@socket` can be set to a mock object to
|
92
|
+
# test without needing an actual UDP socket.
|
93
|
+
def socket ; @socket ||= UDPSocket.new ; end
|
94
|
+
end
|
95
|
+
end
|
data/lib/cube/version.rb
ADDED
data/realtest.rb
ADDED
data/spec/cube_spec.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'date'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
# a few things yanked verbatim from here: https://github.com/github/statsd-ruby/blob/master/spec/statsd_spec.rb
|
6
|
+
# license: https://github.com/github/statsd-ruby/blob/master/LICENSE.txt
|
7
|
+
|
8
|
+
describe Cube::Client do
|
9
|
+
before do
|
10
|
+
@cube = Cube::Client.new "cube.example.org", 1185
|
11
|
+
class << @cube
|
12
|
+
attr_reader :host, :port
|
13
|
+
def socket ; @socket ||= FakeUDPSocket.new ; end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
after { @cube.socket.clear }
|
18
|
+
|
19
|
+
describe "#initialize" do
|
20
|
+
it "should set the host and port" do
|
21
|
+
@cube.host.must_equal "cube.example.org"
|
22
|
+
@cube.port.must_equal 1185
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should default the host and port to localhost:1180" do
|
26
|
+
cube = Cube::Client.new
|
27
|
+
cube.instance_variable_get('@host').must_equal 'localhost'
|
28
|
+
cube.instance_variable_get('@port').must_equal 1180
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#send" do
|
33
|
+
it "should format the message according to the Cube spec" do
|
34
|
+
type = "request"
|
35
|
+
@cube.send type
|
36
|
+
recv = JSON.parse @cube.socket.recv.first
|
37
|
+
recv["type"].must_equal "request"
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "with a time" do
|
41
|
+
it "should format the message according to the Cube spec" do
|
42
|
+
type = "request"
|
43
|
+
time = DateTime.now
|
44
|
+
@cube.send type, time
|
45
|
+
recv = JSON.parse @cube.socket.recv.first
|
46
|
+
recv["type"].must_equal "request"
|
47
|
+
recv["time"].must_equal time.iso8601
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "with an id" do
|
52
|
+
it "should format the message according to the Cube spec" do
|
53
|
+
type = "request"
|
54
|
+
time = DateTime.now
|
55
|
+
id = 42
|
56
|
+
data = { :duration_ms => 234 }
|
57
|
+
@cube.send type, time, id, data
|
58
|
+
recv = JSON.parse @cube.socket.recv.first
|
59
|
+
recv["type"].must_equal "request"
|
60
|
+
recv["time"].must_equal time.iso8601
|
61
|
+
recv["id"].must_equal 42
|
62
|
+
recv["data"].must_equal({ "duration_ms" => 234 })
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "with data" do
|
67
|
+
it "should format the message according to the Cube spec" do
|
68
|
+
type = "request"
|
69
|
+
time = DateTime.now
|
70
|
+
id = 42
|
71
|
+
data = { :duration_ms => 234 }
|
72
|
+
@cube.send type, time, id, data
|
73
|
+
recv = JSON.parse @cube.socket.recv.first
|
74
|
+
recv["type"].must_equal "request"
|
75
|
+
recv["time"].must_equal time.iso8601
|
76
|
+
recv["id"].must_equal 42
|
77
|
+
recv["data"].must_equal({ "duration_ms" => 234 })
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "with namespace" do
|
83
|
+
before { @cube.namespace = 'someservice' }
|
84
|
+
|
85
|
+
it "should add namespace to send" do
|
86
|
+
@cube.send "request"
|
87
|
+
recv = JSON.parse @cube.socket.recv.first
|
88
|
+
recv["type"].must_equal 'someservice_request'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "with logging" do
|
93
|
+
require 'stringio'
|
94
|
+
before { Cube::Client.logger = Logger.new(@log = StringIO.new) }
|
95
|
+
|
96
|
+
it "should write to the log in debug" do
|
97
|
+
Cube::Client.logger.level = Logger::DEBUG
|
98
|
+
|
99
|
+
@cube.send "request"
|
100
|
+
|
101
|
+
recv = @cube.socket.recv.first
|
102
|
+
@log.string.must_match "Cube: #{recv}"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should not write to the log unless debug" do
|
106
|
+
Cube::Client.logger.level = Logger::INFO
|
107
|
+
|
108
|
+
@cube.send "request"
|
109
|
+
|
110
|
+
recv = @cube.socket.recv.first
|
111
|
+
@log.string.must_be_empty
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe Cube::Client do
|
117
|
+
describe "with a real UDP socket" do
|
118
|
+
it "should actually send stuff over the socket" do
|
119
|
+
socket = UDPSocket.new
|
120
|
+
host, port = 'localhost', 12345
|
121
|
+
socket.bind host, port
|
122
|
+
|
123
|
+
time = DateTime.now
|
124
|
+
cube = Cube::Client.new host, port
|
125
|
+
cube.send "request", time
|
126
|
+
|
127
|
+
recv = JSON.parse socket.recvfrom(64).first
|
128
|
+
recv["type"].must_equal "request"
|
129
|
+
recv["time"].must_equal time.iso8601
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end if ENV['LIVE']
|
133
|
+
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
|
3
|
+
# yanked almost verbatim from here: https://github.com/github/statsd-ruby/blob/master/spec/helper.rb
|
4
|
+
# license: https://github.com/github/statsd-ruby/blob/master/LICENSE.txt
|
5
|
+
|
6
|
+
$:.unshift File.dirname(__FILE__)
|
7
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
8
|
+
|
9
|
+
require 'cube'
|
10
|
+
require 'logger'
|
11
|
+
|
12
|
+
class FakeUDPSocket
|
13
|
+
def initialize
|
14
|
+
@buffer = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def send(message, *args)
|
18
|
+
@buffer.push [message]
|
19
|
+
end
|
20
|
+
|
21
|
+
def recv
|
22
|
+
res = @buffer.shift
|
23
|
+
end
|
24
|
+
|
25
|
+
def clear
|
26
|
+
@buffer = []
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
inspect
|
31
|
+
end
|
32
|
+
|
33
|
+
def inspect
|
34
|
+
"<FakeUDPSocket: #{@buffer.inspect}>"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cube-tapjoy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Cody Krieger
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-08 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: minitest
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: A Cube client for Ruby (http://square.github.com/cube).
|
36
|
+
email:
|
37
|
+
- cody@codykrieger.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- cube-tapjoy.gemspec
|
51
|
+
- lib/cube.rb
|
52
|
+
- lib/cube/version.rb
|
53
|
+
- realtest.rb
|
54
|
+
- spec/cube_spec.rb
|
55
|
+
- spec/helper.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: https://github.com/Tapjoy/cube-ruby
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: A Cube client for Ruby (http://square.github.com/cube).
|
90
|
+
test_files:
|
91
|
+
- spec/cube_spec.rb
|
92
|
+
- spec/helper.rb
|