mixpanel_client 2.0.0.beta2 → 2.0.0

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.
data/.gitignore CHANGED
@@ -19,6 +19,8 @@ config/mixpanel.yml
19
19
  coverage
20
20
  rdoc
21
21
  pkg
22
+ doc
23
+ .yardoc
22
24
 
23
25
  ## PROJECT::SPECIFIC
24
26
  test/
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Ruby access to the [Mixpanel](http://mixpanel.com/) web analytics tool.
4
4
 
5
+ [Mixpanel Data API Reference](http://mixpanel.com/api/docs/guides/api/v2)
5
6
 
6
7
  ## Installation
7
8
 
@@ -12,7 +13,8 @@ Ruby access to the [Mixpanel](http://mixpanel.com/) web analytics tool.
12
13
  require 'rubygems'
13
14
  require 'mixpanel_client'
14
15
 
15
- client = Mixpanel::Client.new('api_key' => 'changeme', 'api_secret' => 'changeme')
16
+ config = {'api_key' => 'changeme', 'api_secret' => 'changeme'}
17
+ client = Mixpanel::Client.new(config)
16
18
 
17
19
  data = client.request do
18
20
  resource 'events/properties'
@@ -23,7 +25,7 @@ Ruby access to the [Mixpanel](http://mixpanel.com/) web analytics tool.
23
25
  unit 'hour'
24
26
  interval 24
25
27
  limit 5
26
- bucket 'kicked'
28
+ bucket 'contents'
27
29
  end
28
30
 
29
31
  puts data.inspect
@@ -2,28 +2,23 @@
2
2
 
3
3
  # Mixpanel API Ruby Client Library
4
4
  #
5
- # Copyright (c) 2009+ Keolo Keagy
6
- # See LICENSE for details.
5
+ # Allows access to the mixpanel.com API using the ruby programming language
7
6
  #
8
- # Inspired by the official mixpanel php and python libraries.
9
- # http://mixpanel.com/api/docs/guides/api/
10
-
11
- require 'cgi'
12
- require 'digest/md5'
13
- require 'open-uri'
14
- require 'json' unless defined?(JSON)
15
-
16
- # Ruby library for the mixpanel.com web service
7
+ # Copyright (c) 2009+ Keolo Keagy
8
+ # See LICENSE for details
17
9
  module Mixpanel
10
+ # Return metrics from Mixpanel Data API
18
11
  class Client
19
- BASE_URI = 'http://mixpanel.com/api'
20
- API_VERSION = '2.0'
12
+ BASE_URI = 'http://mixpanel.com/api/2.0'
21
13
 
22
- # The mixpanel client can be used to easily consume data through the mixpanel API
23
- OPTIONS = [:resource, :event, :funnel, :name, :type, :unit, :interval, :limit, :format, :bucket, :values]
24
- attr_reader :uri
14
+ attr_reader :uri
25
15
  attr_accessor :api_key, :api_secret
26
16
 
17
+ # Availalbe options for a Mixpanel API request
18
+ OPTIONS = [:resource, :event, :funnel, :name, :type, :unit, :interval, :limit, :format, :bucket,
19
+ :values]
20
+
21
+ # Dynamically define accessor methods for each option
27
22
  OPTIONS.each do |option|
28
23
  class_eval "
29
24
  def #{option}(arg=nil)
@@ -32,50 +27,72 @@ module Mixpanel
32
27
  "
33
28
  end
34
29
 
30
+ # Configure the client
31
+ #
32
+ # @example
33
+ # config = {'api_key' => '123', 'api_secret' => '456'}
34
+ # client = Mixpanel::Client.new(config)
35
+ #
36
+ # @param [Hash] config consisting of an 'api_key' and an 'api_secret'
35
37
  def initialize(config)
36
38
  @api_key = config['api_key']
37
39
  @api_secret = config['api_secret']
38
40
  end
39
41
 
40
- def params
41
- OPTIONS.inject({}) do |params, param|
42
- option = send(param)
43
- params.merge!(param => option) if param != :resource && !option.nil?
44
- params
45
- end
46
- end
47
-
42
+ # Return mixpanel data as a JSON object or CSV string
43
+ #
44
+ # @example
45
+ # data = client.request do
46
+ # resource 'events/properties'
47
+ # event '["test-event"]'
48
+ # name 'hello'
49
+ # values '["uno", "dos"]'
50
+ # type 'general'
51
+ # unit 'hour'
52
+ # interval 24
53
+ # limit 5
54
+ # bucket 'contents'
55
+ # end
56
+ #
57
+ # @param [Block] options variables used to make a specific request for mixpanel data
58
+ # @return [JSON, String] mixpanel response as a JSON object or CSV string
48
59
  def request(&options)
49
60
  reset_options
50
61
  instance_eval(&options)
51
62
  @uri = URI.mixpanel(resource, normalize_params(params))
52
63
  response = URI.get(@uri)
53
- to_hash(response)
64
+ Utils.to_hash(response, @format)
54
65
  end
55
66
 
56
- def normalize_params(params)
57
- params.merge!(
58
- :api_key => @api_key,
59
- :expire => Time.now.to_i + 600 # Grant this request 10 minutes
60
- ).merge!(:sig => generate_signature(params))
61
- end
67
+ private
62
68
 
63
- def generate_signature(args)
64
- Digest::MD5.hexdigest(args.map{|key,val| "#{key}=#{val}"}.sort.join + api_secret)
69
+ # Reset options so we can reuse the Mixpanel::Client object without the options persisting
70
+ # between requests
71
+ def reset_options
72
+ (OPTIONS - [:resource]).each do |option|
73
+ eval "remove_instance_variable(:@#{option}) if defined?(@#{option})"
74
+ end
65
75
  end
66
76
 
67
- def to_hash(data)
68
- if @format == 'csv'
69
- data
70
- else
71
- JSON.parse(data)
77
+ # Return a hash of options for a given request
78
+ #
79
+ # @return [Hash] collection of options passed in from the request method
80
+ def params
81
+ OPTIONS.inject({}) do |params, param|
82
+ option = send(param)
83
+ params.merge!(param => option) if param != :resource && !option.nil?
84
+ params
72
85
  end
73
86
  end
74
87
 
75
- def reset_options
76
- (OPTIONS - [:resource]).each do |option|
77
- eval "remove_instance_variable(:@#{option}) if defined?(@#{option})"
78
- end
88
+ # Return a hash of options along with defaults and a generated signature
89
+ #
90
+ # @return [Hash] collection of options including defaults and generated signature
91
+ def normalize_params(params)
92
+ params.merge!(
93
+ :api_key => @api_key,
94
+ :expire => Time.now.to_i + 600 # Grant this request 10 minutes
95
+ ).merge!(:sig => Utils.generate_signature(params, @api_secret))
79
96
  end
80
97
  end
81
98
  end
@@ -1,4 +1,12 @@
1
+ #!/usr/bin/env ruby -Ku
2
+
3
+ # Mixpanel API Ruby Client Library
4
+ #
1
5
  # Define exceptions for this library
6
+ #
7
+ # Copyright (c) 2009+ Keolo Keagy
8
+ # See LICENSE for details
2
9
  module Mixpanel
10
+ # URI related exceptions
3
11
  class URI::HTTPError < StandardError; end
4
12
  end
data/lib/mixpanel/uri.rb CHANGED
@@ -2,17 +2,15 @@
2
2
 
3
3
  # Mixpanel API Ruby Client Library
4
4
  #
5
- # Copyright (c) 2009+ Keolo Keagy
6
- # See LICENSE for details.
7
- #
8
- # Inspired by the official mixpanel php and python libraries.
9
- # http://mixpanel.com/api/docs/guides/api/
10
-
11
5
  # URI related helpers
6
+ #
7
+ # Copyright (c) 2009+ Keolo Keagy
8
+ # See LICENSE for details
12
9
  module Mixpanel
10
+ # Utilities to assist generating and requesting URIs
13
11
  class URI
14
12
  def self.mixpanel(resource, params)
15
- File.join([Mixpanel::Client::BASE_URI, Mixpanel::Client::API_VERSION, resource.to_s]) + "?#{self.encode(params)}"
13
+ "#{File.join([Mixpanel::Client::BASE_URI, resource.to_s])}?#{self.encode(params)}"
16
14
  end
17
15
 
18
16
  def self.encode(params)
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby -Ku
2
+
3
+ # Mixpanel API Ruby Client Library
4
+ #
5
+ # Allows access to the mixpanel.com api using the ruby programming language
6
+ #
7
+ # Copyright (c) 2009+ Keolo Keagy
8
+ # See LICENSE for details
9
+ module Mixpanel
10
+ # Utility methods for Mixpanel::Client
11
+ class Client
12
+ module Utils
13
+ # Return a string composed of hashed values specified by the mixpanel data API
14
+ #
15
+ # @return [String] md5 hash signature required by mixpanel data API
16
+ def self.generate_signature(args, api_secret)
17
+ Digest::MD5.hexdigest(args.map{|key,val| "#{key}=#{val}"}.sort.join + api_secret)
18
+ end
19
+
20
+ # Return a JSON object or a string depending on a given format
21
+ #
22
+ # @param [String] data either CSV or JSON formatted
23
+ # @return [JSON, String] data
24
+ def self.to_hash(data, format)
25
+ if format == 'csv'
26
+ data
27
+ else
28
+ JSON.parse(data)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,6 +1,15 @@
1
+ #!/usr/bin/env ruby -Ku
2
+
3
+ # Mixpanel API Ruby Client Library
4
+ #
1
5
  # Set version number for mixpanel_client
6
+ #
7
+ # Copyright (c) 2009+ Keolo Keagy
8
+ # See LICENSE for details
2
9
  module Mixpanel
10
+ # Return metrics from Mixpanel Data API
3
11
  class Client
4
- VERSION = '2.0.0.beta2'
12
+ # Mixpanel::Client library version
13
+ VERSION = '2.0.0'
5
14
  end
6
15
  end
@@ -1,3 +1,20 @@
1
+ #!/usr/bin/env ruby -Ku
2
+
3
+ # Mixpanel API Ruby Client Library
4
+ #
5
+ # Library includes.
6
+ #
7
+ # Copyright (c) 2009+ Keolo Keagy.
8
+ # See LICENSE for details.
9
+
10
+ # Libraries.
11
+ require 'cgi'
12
+ require 'digest/md5'
13
+ require 'open-uri'
14
+ require 'json' unless defined?(JSON)
15
+
16
+ # Mixpanel::Client libraries.
1
17
  require "#{File.dirname(__FILE__)}/mixpanel/client"
18
+ require "#{File.dirname(__FILE__)}/mixpanel/utils"
2
19
  require "#{File.dirname(__FILE__)}/mixpanel/uri"
3
20
  require "#{File.dirname(__FILE__)}/mixpanel/exceptions"
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ['lib']
21
21
 
22
- s.add_dependency('json', '~> 1.5.1') if RUBY_VERSION < '1.9'
22
+ s.add_runtime_dependency('json', '~> 1.5.1') if RUBY_VERSION < '1.9'
23
23
  s.add_development_dependency('rspec', '>=2.5.0')
24
24
  s.add_development_dependency('webmock', '>=1.6.2')
25
25
  s.add_development_dependency('metric_fu', '>=2.1.1')
@@ -71,13 +71,13 @@ describe Mixpanel::Client do
71
71
  it 'should return a hashed string alpha sorted by key names.' do
72
72
  args = {:c => 'see', :a => 'ey', :d => 'dee', :b => 'bee'}
73
73
  args_alpha_sorted = {:a => 'ey', :b => 'bee', :c => 'see', :d => 'dee'}
74
- @client.generate_signature(args).should == @client.generate_signature(args_alpha_sorted)
74
+ Mixpanel::Client::Utils.generate_signature(args, @client.api_secret).should == Mixpanel::Client::Utils.generate_signature(args_alpha_sorted, @client.api_secret)
75
75
  end
76
76
  end
77
77
 
78
78
  describe '#to_hash' do
79
79
  it 'should return a ruby hash given json as a string' do
80
- @client.to_hash('{"a" : "ey", "b" : "bee"}').should == {'a' => 'ey', 'b' => 'bee'}
80
+ Mixpanel::Client::Utils.to_hash('{"a" : "ey", "b" : "bee"}', @client.format).should == {'a' => 'ey', 'b' => 'bee'}
81
81
  end
82
82
  end
83
83
 
metadata CHANGED
@@ -1,15 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixpanel_client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 62196455
5
- prerelease: 6
4
+ hash: 15
5
+ prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
9
  - 0
10
- - beta
11
- - 2
12
- version: 2.0.0.beta2
10
+ version: 2.0.0
13
11
  platform: ruby
14
12
  authors:
15
13
  - Keolo Keagy
@@ -17,7 +15,7 @@ autorequire:
17
15
  bindir: bin
18
16
  cert_chain: []
19
17
 
20
- date: 2011-06-01 00:00:00 -07:00
18
+ date: 2011-07-15 00:00:00 -07:00
21
19
  default_executable:
22
20
  dependencies:
23
21
  - !ruby/object:Gem::Dependency
@@ -105,6 +103,7 @@ files:
105
103
  - lib/mixpanel/client.rb
106
104
  - lib/mixpanel/exceptions.rb
107
105
  - lib/mixpanel/uri.rb
106
+ - lib/mixpanel/utils.rb
108
107
  - lib/mixpanel/version.rb
109
108
  - lib/mixpanel_client.rb
110
109
  - mixpanel_client.gemspec
@@ -134,14 +133,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
133
  required_rubygems_version: !ruby/object:Gem::Requirement
135
134
  none: false
136
135
  requirements:
137
- - - ">"
136
+ - - ">="
138
137
  - !ruby/object:Gem::Version
139
- hash: 25
138
+ hash: 3
140
139
  segments:
141
- - 1
142
- - 3
143
- - 1
144
- version: 1.3.1
140
+ - 0
141
+ version: "0"
145
142
  requirements: []
146
143
 
147
144
  rubyforge_project: mixpanel_client