mixpanel_export 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/lib/mixpanel_export.rb +9 -0
- data/lib/mixpanel_export/event_properties.rb +21 -0
- data/lib/mixpanel_export/events.rb +22 -0
- data/lib/mixpanel_export/request.rb +77 -0
- data/lib/mixpanel_export/segmentation.rb +25 -0
- data/lib/mixpanel_export/version.rb +3 -0
- data/mixpanel_export.gemspec +27 -0
- data/spec/lib/mixpanel_export/event_properties_spec.rb +85 -0
- data/spec/lib/mixpanel_export/events_spec.rb +87 -0
- data/spec/lib/mixpanel_export/segmentation_spec.rb +128 -0
- data/spec/lib/request_spec.rb +56 -0
- data/spec/spec_helper.rb +3 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8beceadfd9784b4997ff02d400ab753769740da0
|
4
|
+
data.tar.gz: 59e9c92d6141f39fa6e98157983084895847062f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3fde0b42d0dcc8310b552c738ed3608eb115b23634fed71c7abb7fc7757f70182668eb8ea9eca722c95d26f39d1e76f648561b8d12195d184f4c2faf7bdf1418
|
7
|
+
data.tar.gz: f0c699d5a2935e366da84835957a4d500abbd5995c94ada72897a2c3ea45c1c866a7cf70b37e73617ec91dc80f8c37f61efe2458dc4ba8a4794b9b6f907b280c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Pedro Carvalho
|
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,31 @@
|
|
1
|
+
# Mixpanel::Export
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'mixpanel-export'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mixpanel-export
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/mixpanel-export/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module MixpanelExport
|
2
|
+
class EventProperties
|
3
|
+
attr_reader :request
|
4
|
+
|
5
|
+
def initialize(api_secret, api_key)
|
6
|
+
@request = MixpanelExport::Request.new(api_secret, api_key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def properties(options={})
|
10
|
+
request.get('/events/properties', options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def top(options={})
|
14
|
+
request.get('/events/properties/top', options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def values(options={})
|
18
|
+
request.get('/events/properties/values', options)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MixpanelExport
|
2
|
+
class Events
|
3
|
+
attr_reader :request
|
4
|
+
|
5
|
+
def initialize(api_secret, api_key)
|
6
|
+
@request = MixpanelExport::Request.new(api_secret, api_key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def all(options={})
|
10
|
+
options[:unit] = options[:unit] || "month"
|
11
|
+
request.get('/events', options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def top(options={})
|
15
|
+
request.get('/top', options)
|
16
|
+
end
|
17
|
+
|
18
|
+
def names(options={})
|
19
|
+
request.get('/names', options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "httparty"
|
2
|
+
require "digest"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
module MixpanelExport
|
6
|
+
API_ENDPOINT = "http://mixpanel.com/api/2.0"
|
7
|
+
|
8
|
+
class RequestError < HTTParty::Error; end
|
9
|
+
|
10
|
+
class Request
|
11
|
+
include HTTParty
|
12
|
+
base_uri API_ENDPOINT
|
13
|
+
|
14
|
+
def initialize(api_secret, api_key)
|
15
|
+
@api_secret = api_secret
|
16
|
+
@api_key = api_key
|
17
|
+
end
|
18
|
+
|
19
|
+
def get(path, options={})
|
20
|
+
response = self.class.get(path, query: build_query(Hash[normalize(options)]))
|
21
|
+
|
22
|
+
if response.success?
|
23
|
+
response.parsed_response || ""
|
24
|
+
else
|
25
|
+
raise RequestError, response.parsed_response["error"]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
attr_reader :api_secret, :api_key
|
32
|
+
|
33
|
+
def calculate_signature(args)
|
34
|
+
digest = Digest::MD5.new
|
35
|
+
digest << args.map { |k,v| "#{k}=#{v}" }.sort.join
|
36
|
+
digest << api_secret
|
37
|
+
digest.hexdigest
|
38
|
+
end
|
39
|
+
|
40
|
+
def build_query(options)
|
41
|
+
query = options.dup || {}
|
42
|
+
|
43
|
+
query = ({
|
44
|
+
format: default_format,
|
45
|
+
expire: default_request_expiration,
|
46
|
+
api_key: api_key
|
47
|
+
}).merge(query)
|
48
|
+
|
49
|
+
query.merge!(sig: calculate_signature(query))
|
50
|
+
query
|
51
|
+
end
|
52
|
+
|
53
|
+
def normalize(hash={})
|
54
|
+
hash.map do |k,v|
|
55
|
+
k, v =
|
56
|
+
k, case v
|
57
|
+
when is_a?(String)
|
58
|
+
v.encode("utf-8")
|
59
|
+
when is_a?(Array)
|
60
|
+
JSON.dump(v).encode("utf-8")
|
61
|
+
when is_a?(Hash)
|
62
|
+
normalize(v)
|
63
|
+
else
|
64
|
+
v.to_s
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def default_format
|
70
|
+
"json"
|
71
|
+
end
|
72
|
+
|
73
|
+
def default_request_expiration
|
74
|
+
Time.now.utc.to_i + 600
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module MixpanelExport
|
2
|
+
class Segmentation
|
3
|
+
attr_reader :request
|
4
|
+
|
5
|
+
def initialize(api_secret, api_key)
|
6
|
+
@request = MixpanelExport::Request.new(api_secret, api_key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def segmentation(options={})
|
10
|
+
request.get('/segmentation', options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def numeric(options={})
|
14
|
+
request.get('/segmentation/numeric', options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def sum(options={})
|
18
|
+
request.get('/segmentation/sum', options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def average(options={})
|
22
|
+
request.get('/segmentation/average', options)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
|
5
|
+
require 'mixpanel_export/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "mixpanel_export"
|
9
|
+
spec.version = MixpanelExport::VERSION
|
10
|
+
spec.authors = ["Pedro Carvalho"]
|
11
|
+
spec.email = ["incude@gmail.com"]
|
12
|
+
spec.summary = %q{Mixpanel's data export API client.}
|
13
|
+
spec.description = %q{Convenience wrapper for mixpanel's data export API.}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.2.0"
|
25
|
+
spec.add_development_dependency "webmock", "~> 1.20.4"
|
26
|
+
spec.add_development_dependency "httparty", "~> 0.13.3"
|
27
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MixpanelExport::EventProperties do
|
4
|
+
subject(:events) { MixpanelExport::EventProperties.new('api_secret', 'api_key') }
|
5
|
+
|
6
|
+
describe "#properties" do
|
7
|
+
let(:response) do
|
8
|
+
{ 'data':
|
9
|
+
{ 'series': ['2010-05-29',
|
10
|
+
'2010-05-30',
|
11
|
+
'2010-05-31',
|
12
|
+
],
|
13
|
+
'values': {
|
14
|
+
'splash features': {
|
15
|
+
'2010-05-29': 6,
|
16
|
+
'2010-05-30': 4,
|
17
|
+
'2010-05-31': 5,
|
18
|
+
}
|
19
|
+
}
|
20
|
+
},
|
21
|
+
'legend_size': 2
|
22
|
+
}.to_json
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns empty if body is empty" do
|
26
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events/properties").
|
27
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
28
|
+
to_return(:status => 200, :body => "", :headers => {})
|
29
|
+
|
30
|
+
expect(events.properties).to eq("")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "return a json string if request succeeded" do
|
34
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events/properties").
|
35
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
36
|
+
to_return(:status => 200, :body => response, :headers => {})
|
37
|
+
|
38
|
+
expect(events.properties).to eq(response)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#top" do
|
43
|
+
let(:response) do
|
44
|
+
{'ad version': {'count': 295}, 'user type': {'count': 91}}.to_json
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns empty if body is empty" do
|
48
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events/properties/top").
|
49
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
50
|
+
to_return(:status => 200, :body => "", :headers => {})
|
51
|
+
|
52
|
+
expect(events.top).to eq("")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "return a json string if request succeeded" do
|
56
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events/properties/top").
|
57
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
58
|
+
to_return(:status => 200, :body => response, :headers => {})
|
59
|
+
|
60
|
+
expect(events.top).to eq(response)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#values" do
|
65
|
+
let(:response) do
|
66
|
+
['male', 'female', 'unknown'].to_json
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns empty if body is empty" do
|
70
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events/properties/values").
|
71
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
72
|
+
to_return(:status => 200, :body => "", :headers => {})
|
73
|
+
|
74
|
+
expect(events.values).to eq("")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "return a json string if request succeeded" do
|
78
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events/properties/values").
|
79
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
80
|
+
to_return(:status => 200, :body => response, :headers => {})
|
81
|
+
|
82
|
+
expect(events.values).to eq(response)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MixpanelExport::Events do
|
4
|
+
subject(:events) { MixpanelExport::Events.new('api_secret', 'api_key') }
|
5
|
+
|
6
|
+
describe "#all" do
|
7
|
+
let(:response) do
|
8
|
+
{ 'data': {
|
9
|
+
'series': ['2010-05-29', '2010-05-30', '2010-05-31'],
|
10
|
+
'values': {'account-page': { '2010-05-30': 1, },
|
11
|
+
'splash features': { '2010-05-29': 6,
|
12
|
+
'2010-05-30': 4,
|
13
|
+
'2010-05-31': 5, }
|
14
|
+
}
|
15
|
+
},
|
16
|
+
'legend_size': 2 }.to_json
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns empty if body is empty" do
|
20
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events").
|
21
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
22
|
+
to_return(:status => 200, :body => "", :headers => {})
|
23
|
+
|
24
|
+
expect(events.all).to eq("")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "return a json string if request succeeded" do
|
28
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events").
|
29
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
30
|
+
to_return(:status => 200, :body => response, :headers => {})
|
31
|
+
|
32
|
+
expect(events.all).to eq(response)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#top" do
|
37
|
+
let(:response) do
|
38
|
+
{ 'events': [
|
39
|
+
{ 'amount': 2,
|
40
|
+
'event': 'funnel',
|
41
|
+
'percent_change': -0.35635745999582824},
|
42
|
+
{ 'amount': 75,
|
43
|
+
'event': 'pages',
|
44
|
+
'percent_change': -0.20209602478821687},
|
45
|
+
{ 'amount': 2, 'event': 'projects', 'percent_change': 1.0 }],
|
46
|
+
'type': 'unique' }.to_json
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns empty if body is empty" do
|
50
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/top").
|
51
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
52
|
+
to_return(:status => 200, :body => "", :headers => {})
|
53
|
+
|
54
|
+
expect(events.top).to eq("")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "return a json string if request succeeded" do
|
58
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/top").
|
59
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
60
|
+
to_return(:status => 200, :body => response, :headers => {})
|
61
|
+
|
62
|
+
expect(events.top).to eq(response)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#names" do
|
67
|
+
let(:response) do
|
68
|
+
[ 'battle','click signup button','send message','View homepage'].to_json
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns empty if body is empty" do
|
72
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/names").
|
73
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
74
|
+
to_return(:status => 200, :body => "", :headers => {})
|
75
|
+
|
76
|
+
expect(events.names).to eq("")
|
77
|
+
end
|
78
|
+
|
79
|
+
it "return a json string if request succeeded" do
|
80
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/names").
|
81
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
82
|
+
to_return(:status => 200, :body => response, :headers => {})
|
83
|
+
|
84
|
+
expect(events.names).to eq(response)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MixpanelExport::Segmentation do
|
4
|
+
subject(:events) { MixpanelExport::Segmentation.new('api_secret', 'api_key') }
|
5
|
+
|
6
|
+
describe "#segmentation" do
|
7
|
+
let(:response) do
|
8
|
+
{'data': {'series': ['2011-08-08',
|
9
|
+
'2011-08-09',
|
10
|
+
'2011-08-06',
|
11
|
+
'2011-08-07'],
|
12
|
+
'values': {'signed up': {'2011-08-06': 147,
|
13
|
+
'2011-08-07': 146,
|
14
|
+
'2011-08-08': 776,
|
15
|
+
'2011-08-09': 1376}}},
|
16
|
+
'legend_size': 1}.to_json
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns empty if body is empty" do
|
20
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation").
|
21
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
22
|
+
to_return(:status => 200, :body => "", :headers => {})
|
23
|
+
|
24
|
+
expect(events.segmentation).to eq("")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "return a json string if request succeeded" do
|
28
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation").
|
29
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
30
|
+
to_return(:status => 200, :body => response, :headers => {})
|
31
|
+
|
32
|
+
expect(events.segmentation).to eq(response)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#numeric" do
|
37
|
+
let(:response) do
|
38
|
+
{'data': {'series': ['2011-08-08',
|
39
|
+
'2011-08-09',
|
40
|
+
'2011-08-06',
|
41
|
+
'2011-08-07'],
|
42
|
+
'values': {'2,000 - 2,100': {'2011-08-06': 1,
|
43
|
+
'2011-08-07': 5,
|
44
|
+
'2011-08-08': 4,
|
45
|
+
'2011-08-09': 15},
|
46
|
+
'2,100 - 2,200': {'2011-08-07': 2,
|
47
|
+
'2011-08-08': 7,
|
48
|
+
'2011-08-09': 15},
|
49
|
+
'2,200 - 2,300': {'2011-08-06': 1,
|
50
|
+
'2011-08-08': 6,
|
51
|
+
'2011-08-09': 5},
|
52
|
+
'2,300 - 2,400': {'2011-08-06': 4,
|
53
|
+
'2011-08-08': 1,
|
54
|
+
'2011-08-09': 12},
|
55
|
+
'2,400 - 2,500': {'2011-08-08': 2,
|
56
|
+
'2011-08-09': 5}}},
|
57
|
+
'legend_size': 5}.to_json
|
58
|
+
end
|
59
|
+
|
60
|
+
it "returns empty if body is empty" do
|
61
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation/numeric").
|
62
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
63
|
+
to_return(:status => 200, :body => "", :headers => {})
|
64
|
+
|
65
|
+
expect(events.numeric).to eq("")
|
66
|
+
end
|
67
|
+
|
68
|
+
it "return a json string if request succeeded" do
|
69
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation/numeric").
|
70
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
71
|
+
to_return(:status => 200, :body => response, :headers => {})
|
72
|
+
|
73
|
+
expect(events.numeric).to eq(response)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#sum" do
|
78
|
+
let(:response) do
|
79
|
+
{'results': {'2011-08-06': 376.0,
|
80
|
+
'2011-08-07': 634.0,
|
81
|
+
'2011-08-08': 474.0,
|
82
|
+
'2011-08-09': 483.0},
|
83
|
+
'status': 'ok'}.to_json
|
84
|
+
end
|
85
|
+
|
86
|
+
it "returns empty if body is empty" do
|
87
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation/sum").
|
88
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
89
|
+
to_return(:status => 200, :body => "", :headers => {})
|
90
|
+
|
91
|
+
expect(events.sum).to eq("")
|
92
|
+
end
|
93
|
+
|
94
|
+
it "return a json string if request succeeded" do
|
95
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation/sum").
|
96
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
97
|
+
to_return(:status => 200, :body => response, :headers => {})
|
98
|
+
|
99
|
+
expect(events.sum).to eq(response)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "#average" do
|
104
|
+
let(:response) do
|
105
|
+
{'results': {'2011-08-06': 8.64705882352939,
|
106
|
+
'2011-08-07': 4.640625,
|
107
|
+
'2011-08-08': 3.6230899830221,
|
108
|
+
'2011-08-09': 7.3353658536585},
|
109
|
+
'status': 'ok'}.to_json
|
110
|
+
end
|
111
|
+
|
112
|
+
it "returns empty if body is empty" do
|
113
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation/average").
|
114
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
115
|
+
to_return(:status => 200, :body => "", :headers => {})
|
116
|
+
|
117
|
+
expect(events.average).to eq("")
|
118
|
+
end
|
119
|
+
|
120
|
+
it "return a json string if request succeeded" do
|
121
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/segmentation/average").
|
122
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
123
|
+
to_return(:status => 200, :body => response, :headers => {})
|
124
|
+
|
125
|
+
expect(events.average).to eq(response)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MixpanelExport::Request do
|
4
|
+
let(:request) { MixpanelExport::Request.new('api_secret', 'api_key') }
|
5
|
+
|
6
|
+
describe "#get" do
|
7
|
+
it "raises error if the request is not successful" do
|
8
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events").
|
9
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
10
|
+
to_return(status: 400)
|
11
|
+
|
12
|
+
expect { request.get('/events') }.to raise_exception
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns the request body if request succeeds" do
|
16
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events").
|
17
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
18
|
+
to_return(status: 200, body: "{}")
|
19
|
+
|
20
|
+
expect(request.get('/events')).to eq("{}")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "requires a signature to be calculated" do
|
24
|
+
stub_request(:get, "http://mixpanel.com/api/2.0/events").
|
25
|
+
with(query: hash_including(:api_key, :sig, :expire, :format)).
|
26
|
+
to_return(status: 200, body: "{}")
|
27
|
+
|
28
|
+
expect(request).to receive(:calculate_signature).once.and_return('e720dfe014c0107e3f080b0880997bca')
|
29
|
+
request.get('/events')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#calculate_signature" do
|
34
|
+
it "returns a valid signature" do
|
35
|
+
options = { api_key: "f0aa346688cee071cd85d857285a3464", interval: 7, type: "average", event: ["splash features<", "account-page"], unit: "day", expire: 1275624968, format: "json" }
|
36
|
+
expect(request.send(:calculate_signature, options)).to eq('1de1b89208ab1d3a4e234a489de40ff0')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#build_query" do
|
41
|
+
it "should set an expire date if none provided" do
|
42
|
+
expire_date = Time.now.utc
|
43
|
+
Time.stub(:now).and_return(expire_date)
|
44
|
+
|
45
|
+
expect(request.send(:build_query, {})).to include(expire: (expire_date.to_i + 600))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#normalize" do
|
50
|
+
it "transforms a hash according to a set of rules" do
|
51
|
+
options = { api_key: "f0aa346688cee071cd85d857285a3464", interval: 7, type: "average", event: ["splash features<", "account-page"], unit: "day", expire: 1275624968, format: "json" }
|
52
|
+
|
53
|
+
expect(request.send(:normalize, options)).to include([:api_key, "f0aa346688cee071cd85d857285a3464"], [:interval,"7"], [:type, "average"], [:event, "[\"splash features<\", \"account-page\"]"], [:unit, "day"], [:expire, "1275624968"], [:format, "json"])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mixpanel_export
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pedro Carvalho
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.20.4
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.20.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: httparty
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.13.3
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.13.3
|
83
|
+
description: Convenience wrapper for mixpanel's data export API.
|
84
|
+
email:
|
85
|
+
- incude@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/mixpanel_export.rb
|
96
|
+
- lib/mixpanel_export/event_properties.rb
|
97
|
+
- lib/mixpanel_export/events.rb
|
98
|
+
- lib/mixpanel_export/request.rb
|
99
|
+
- lib/mixpanel_export/segmentation.rb
|
100
|
+
- lib/mixpanel_export/version.rb
|
101
|
+
- mixpanel_export.gemspec
|
102
|
+
- spec/lib/mixpanel_export/event_properties_spec.rb
|
103
|
+
- spec/lib/mixpanel_export/events_spec.rb
|
104
|
+
- spec/lib/mixpanel_export/segmentation_spec.rb
|
105
|
+
- spec/lib/request_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
homepage: ''
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.4.5
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Mixpanel's data export API client.
|
131
|
+
test_files:
|
132
|
+
- spec/lib/mixpanel_export/event_properties_spec.rb
|
133
|
+
- spec/lib/mixpanel_export/events_spec.rb
|
134
|
+
- spec/lib/mixpanel_export/segmentation_spec.rb
|
135
|
+
- spec/lib/request_spec.rb
|
136
|
+
- spec/spec_helper.rb
|