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 CHANGED
@@ -20,3 +20,4 @@ pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
22
  test/
23
+ tmp/
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
- $ gem install mixpanel_client
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
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'metric_fu'
3
4
 
4
5
  begin
5
6
  require 'jeweler'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.3.0
@@ -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
- class Client
18
- attr_accessor :api_key, :api_secret, :format
17
+ BASE_URI = 'http://mixpanel.com/api'
18
+ VERSION = '1.0'
19
19
 
20
- BASE_URI = 'http://mixpanel.com/api'
21
- VERSION = '1.0'
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
- params[:api_key] = api_key
31
- params[:expire] = Time.now.to_i + 600 # Grant this request 10 minutes
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{|k,v| "#{k}=#{v}"}.sort.join('') + api_secret)
45
+ Digest::MD5.hexdigest(args.map{|key,val| "#{key}=#{val}"}.sort.join + api_secret)
44
46
  end
45
47
 
46
- def get(uri)
47
- URI.parse(uri).read
48
+ def to_hash(data)
49
+ require 'json' unless defined?(JSON)
50
+ JSON.parse(data)
48
51
  end
52
+ end
49
53
 
50
- def mixpanel_uri(endpoint, meth, params)
51
- %Q(#{BASE_URI}/#{endpoint}/#{VERSION}/#{meth}?#{urlencode(params)})
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 urlencode(params)
55
- params.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.sort.join('&')
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 to_hash(data)
59
- case @format
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
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mixpanel_client}
8
- s.version = "0.2.5"
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-08}
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 'Mixpanel::Client' do
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
- @api.stub!(:get).and_return('{"some" : "thing"}')
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 '#mixpanel_uri' do
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
- @api.mixpanel_uri(endpoint, meth, params).should == 'http://mixpanel.com/api/events/1.0/general?a=aye&c=see'
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 '#urlencode' do
48
+ describe '.encode' do
45
49
  it 'should return a string with url encoded values.' do
46
50
  params = {:hey => '!@#$%^&*()\/"Ü', :soo => "hëllö?"}
47
- @api.urlencode(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'
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.2.5
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-08 00:00:00 -08:00
12
+ date: 2010-02-23 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency