mixpanel_client 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -3,7 +3,7 @@
3
3
  # Mixpanel API Ruby Client Library
4
4
  #
5
5
  # Copyright (c) 2009+ Keolo Keagy
6
- # MIT License http://www.opensource.org/licenses/mit-license.php
6
+ # See LICENSE for details.
7
7
  # Open sourced by the good folks at SharesPost.com
8
8
  #
9
9
  # Inspired by the official mixpanel php and python libraries.
@@ -12,6 +12,7 @@
12
12
  require 'cgi'
13
13
  require 'digest/md5'
14
14
  require 'open-uri'
15
+ require 'rubygems'
15
16
  require 'json'
16
17
 
17
18
  module Mixpanel
@@ -22,11 +23,11 @@ module Mixpanel
22
23
  VERSION = '1.0'
23
24
 
24
25
  def initialize(config)
25
- @api_key = config[:api_key]
26
+ @api_key = config[:api_key]
26
27
  @api_secret = config[:api_secret]
27
28
  end
28
29
 
29
- def request(endpoint, method, params)
30
+ def request(endpoint, meth, params)
30
31
  params[:api_key] = api_key
31
32
  params[:expire] = Time.now.to_i + 600 # Grant this request 10 minutes
32
33
  params[:format] ||= :json
@@ -34,7 +35,7 @@ module Mixpanel
34
35
 
35
36
  @format = params[:format]
36
37
 
37
- response = URI.parse("#{BASE_URI}/#{endpoint}/#{VERSION}/#{method}?#{urlencode(params)}").read
38
+ response = URI.parse(mixpanel_uri(endpoint, meth, params)).read
38
39
  to_hash(response)
39
40
  end
40
41
 
@@ -42,15 +43,20 @@ module Mixpanel
42
43
  Digest::MD5.hexdigest(args.map{|k,v| "#{k}=#{v}"}.sort.to_s + api_secret)
43
44
  end
44
45
 
46
+ def mixpanel_uri(endpoint, meth, params)
47
+ %Q(#{BASE_URI}/#{endpoint}/#{VERSION}/#{meth}?#{urlencode(params)})
48
+ end
49
+
45
50
  def urlencode(params)
46
- params.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.join('&')
51
+ params.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}"}.sort.join('&')
47
52
  end
48
53
 
49
- def to_hash(data)
50
- case @format
51
- when :json
52
- JSON.parse(data)
54
+ protected
55
+ def to_hash(data)
56
+ case @format
57
+ when :json
58
+ JSON.parse(data)
59
+ end
53
60
  end
54
- end
55
61
  end
56
62
  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.1.0"
8
+ s.version = "0.2.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{2009-11-10}
12
+ s.date = %q{2009-12-02}
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 = [
@@ -1,7 +1,42 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "MixpanelClient" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
3
+ describe 'Mixpanel::Client' do
4
+ before :all do
5
+ config = {:api_key => 'test', :api_secret => 'test'}
6
+ @api = Mixpanel::Client.new(config)
7
+ end
8
+
9
+ describe '#request' do
10
+ it 'should return a valid JSON result set.' do
11
+ pending 'Need to setup valid Mixpanel test account.'
12
+ data = @api.request(:events, :general, {
13
+ :event => '["test-event"]',
14
+ :unit => 'hour',
15
+ :interval => 24
16
+ })
17
+ data.should == {:some => 'thing'}
18
+ end
19
+ end
20
+
21
+ describe '#hash_args' do
22
+ it 'should return a hashed string alpha sorted by key names.' do
23
+ args = {:c => 'see', :a => 'aye', :d => 'dee', :b => 'bee'}
24
+ args_alpha_sorted = {:a => 'aye', :b => 'bee', :c => 'see', :d => 'dee'}
25
+ @api.hash_args(args).should == @api.hash_args(args_alpha_sorted)
26
+ end
27
+ end
28
+
29
+ describe '#mixpanel_uri' do
30
+ it 'should return a properly formatted mixpanel uri as a string' do
31
+ endpoint, meth, params = [:events, :general, {:c => 'see', :a => 'aye'}]
32
+ @api.mixpanel_uri(endpoint, meth, params).should == 'http://mixpanel.com/api/events/1.0/general?a=aye&c=see'
33
+ end
34
+ end
35
+
36
+ describe '#urlencode' do
37
+ it 'should return a string with url encoded values.' do
38
+ params = {:hey => '!@#$%^&*()\/"Ü', :soo => "hëllö?"}
39
+ @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'
40
+ end
6
41
  end
7
42
  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.1.0
4
+ version: 0.2.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: 2009-11-10 00:00:00 -08:00
12
+ date: 2009-12-02 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency