mixpanel_client 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +2 -2
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/mixpanel_client.rb +30 -31
- data/mixpanel_client.gemspec +2 -2
- data/spec/mixpanel_client_spec.rb +14 -16
- metadata +2 -2
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
Ruby access to the [Mixpanel](http://mixpanel.com/) web analytics tool.
|
4
4
|
|
5
5
|
## Installation
|
6
|
-
|
6
|
+
gem install mixpanel_client
|
7
7
|
|
8
8
|
## Example Usage
|
9
9
|
require 'rubygems'
|
@@ -24,4 +24,4 @@ Ruby access to the [Mixpanel](http://mixpanel.com/) web analytics tool.
|
|
24
24
|
|
25
25
|
## Copyright
|
26
26
|
|
27
|
-
Copyright (c) 2009 Keolo Keagy. See LICENSE for details.
|
27
|
+
Copyright (c) 2009+ Keolo Keagy. See LICENSE for details.
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/mixpanel_client.rb
CHANGED
@@ -4,7 +4,6 @@
|
|
4
4
|
#
|
5
5
|
# Copyright (c) 2009+ Keolo Keagy
|
6
6
|
# See LICENSE for details.
|
7
|
-
# Open sourced by the good folks at SharesPost.com
|
8
7
|
#
|
9
8
|
# Inspired by the official mixpanel php and python libraries.
|
10
9
|
# http://mixpanel.com/api/docs/guides/api/
|
@@ -13,57 +12,57 @@ require 'cgi'
|
|
13
12
|
require 'digest/md5'
|
14
13
|
require 'open-uri'
|
15
14
|
|
15
|
+
# Ruby library for the mixpanel.com web service
|
16
16
|
module Mixpanel
|
17
|
-
|
18
|
-
|
17
|
+
BASE_URI = 'http://mixpanel.com/api'
|
18
|
+
VERSION = '1.0'
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
# The mixpanel client can be used to easily consume data through the
|
21
|
+
# mixpanel API.
|
22
|
+
class Client
|
23
|
+
attr_accessor :api_key, :api_secret
|
22
24
|
|
23
25
|
def initialize(config)
|
24
26
|
@api_key = config[:api_key]
|
25
27
|
@api_secret = config[:api_secret]
|
26
|
-
@format ||= :json
|
27
28
|
end
|
28
29
|
|
29
30
|
def request(endpoint, meth, params)
|
30
|
-
|
31
|
-
|
32
|
-
params[:format] ||= @format
|
33
|
-
params[:sig] = hash_args(params)
|
34
|
-
|
35
|
-
@sig = params[:sig]
|
36
|
-
@format = params[:format]
|
37
|
-
|
38
|
-
response = get(mixpanel_uri(endpoint, meth, params))
|
31
|
+
uri = URI.mixpanel(endpoint, meth, normalize_params(params))
|
32
|
+
response = URI.get(uri)
|
39
33
|
to_hash(response)
|
40
34
|
end
|
41
35
|
|
36
|
+
def normalize_params(params)
|
37
|
+
params.merge!(
|
38
|
+
:api_key => api_key,
|
39
|
+
:expire => Time.now.to_i + 600, # Grant this request 10 minutes
|
40
|
+
:format => :json
|
41
|
+
).merge!(:sig => hash_args(params))
|
42
|
+
end
|
43
|
+
|
42
44
|
def hash_args(args)
|
43
|
-
Digest::MD5.hexdigest(args.map{|
|
45
|
+
Digest::MD5.hexdigest(args.map{|key,val| "#{key}=#{val}"}.sort.join + api_secret)
|
44
46
|
end
|
45
47
|
|
46
|
-
def
|
47
|
-
|
48
|
+
def to_hash(data)
|
49
|
+
require 'json' unless defined?(JSON)
|
50
|
+
JSON.parse(data)
|
48
51
|
end
|
52
|
+
end
|
49
53
|
|
50
|
-
|
51
|
-
|
54
|
+
# URI related helpers
|
55
|
+
class URI
|
56
|
+
def self.mixpanel(endpoint, meth, params)
|
57
|
+
%Q(#{BASE_URI}/#{endpoint}/#{VERSION}/#{meth}?#{self.encode(params)})
|
52
58
|
end
|
53
59
|
|
54
|
-
def
|
55
|
-
params.map{|
|
60
|
+
def self.encode(params)
|
61
|
+
params.map{|key,val| "#{key}=#{CGI.escape(val.to_s)}"}.sort.join('&')
|
56
62
|
end
|
57
63
|
|
58
|
-
def
|
59
|
-
|
60
|
-
when :placeholder
|
61
|
-
# This is just a placeholder in case Mixpanel supports alternate
|
62
|
-
# formats. Mixpanel at this time (v1.0) only supports JSON.
|
63
|
-
else # The default response is JSON
|
64
|
-
require 'json' unless defined?(JSON)
|
65
|
-
JSON.parse(data)
|
66
|
-
end
|
64
|
+
def self.get(uri)
|
65
|
+
::URI.parse(uri).read
|
67
66
|
end
|
68
67
|
end
|
69
68
|
end
|
data/mixpanel_client.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mixpanel_client}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Keolo Keagy"]
|
12
|
-
s.date = %q{2010-02-
|
12
|
+
s.date = %q{2010-02-23}
|
13
13
|
s.description = %q{Simple ruby client interface to the Mixpanel API.}
|
14
14
|
s.email = %q{keolo@dreampointmedia.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -2,20 +2,16 @@ require 'rubygems'
|
|
2
2
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe Mixpanel::Client do
|
6
6
|
before :all do
|
7
7
|
config = {:api_key => 'test', :api_secret => 'test'}
|
8
8
|
@api = Mixpanel::Client.new(config)
|
9
9
|
end
|
10
10
|
|
11
|
-
it 'should have a format attribute that defaults to :json' do
|
12
|
-
@api.format.should == :json
|
13
|
-
end
|
14
|
-
|
15
11
|
describe '#request' do
|
16
12
|
it 'should return json and convert to a ruby hash' do
|
17
13
|
# Stub Mixpanel web service
|
18
|
-
|
14
|
+
Mixpanel::URI.stub!(:get).and_return('{"some" : "thing"}')
|
19
15
|
|
20
16
|
data = @api.request(:events, :general, {
|
21
17
|
:event => '["test-event"]',
|
@@ -34,23 +30,25 @@ describe 'Mixpanel::Client' do
|
|
34
30
|
end
|
35
31
|
end
|
36
32
|
|
37
|
-
describe '#
|
33
|
+
describe '#to_hash' do
|
34
|
+
it 'should return a ruby hash given json as a string' do
|
35
|
+
@api.to_hash('{"a" : "aye", "b" : "bee"}').should == {'a' => 'aye', 'b' => 'bee'}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe Mixpanel::URI do
|
41
|
+
describe '.mixpanel' do
|
38
42
|
it 'should return a properly formatted mixpanel uri as a string' do
|
39
43
|
endpoint, meth, params = [:events, :general, {:c => 'see', :a => 'aye'}]
|
40
|
-
|
44
|
+
Mixpanel::URI.mixpanel(endpoint, meth, params).should == 'http://mixpanel.com/api/events/1.0/general?a=aye&c=see'
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
44
|
-
describe '
|
48
|
+
describe '.encode' do
|
45
49
|
it 'should return a string with url encoded values.' do
|
46
50
|
params = {:hey => '!@#$%^&*()\/"Ü', :soo => "hëllö?"}
|
47
|
-
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe '#to_hash' do
|
52
|
-
it 'should return a ruby hash given json as a string' do
|
53
|
-
@api.to_hash('{"a" : "aye", "b" : "bee"}').should == {'a' => 'aye', 'b' => 'bee'}
|
51
|
+
Mixpanel::URI.encode(params).should == 'hey=%21%40%23%24%25%5E%26%2A%28%29%5C%2F%22%C3%9C&soo=h%C3%ABll%C3%B6%3F'
|
54
52
|
end
|
55
53
|
end
|
56
54
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mixpanel_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keolo Keagy
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-23 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|