gosquared 3.0.4 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/lib/gosquared.rb +36 -37
- data/lib/gosquared/account.rb +2 -2
- data/lib/gosquared/client.rb +55 -58
- data/lib/gosquared/now.rb +45 -48
- data/lib/gosquared/people.rb +58 -49
- data/lib/gosquared/tracking.rb +37 -39
- data/lib/gosquared/trends.rb +49 -50
- data/spec/account_spec.rb +54 -56
- data/spec/client_spec.rb +54 -55
- data/spec/now_spec.rb +25 -26
- data/spec/people_spec.rb +105 -70
- data/spec/spec_helper.rb +51 -53
- data/spec/tracking_spec.rb +47 -48
- data/spec/trends_spec.rb +30 -31
- metadata +9 -8
data/lib/gosquared/tracking.rb
CHANGED
@@ -1,44 +1,42 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'client'
|
2
2
|
|
3
3
|
module Gosquared
|
4
4
|
class Tracking
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
|
5
|
+
BASEURL = 'https://api.gosquared.com/tracking/v1/'.freeze
|
6
|
+
DIMENSIONS = %w[event identify pageview ping properties timeout transaction].freeze
|
7
|
+
|
8
|
+
def initialize(api_key, site_token, client = Gosquared::Client.new)
|
9
|
+
@site_token = site_token
|
10
|
+
@api_key = api_key
|
11
|
+
@client = client
|
12
|
+
end
|
13
|
+
|
14
|
+
DIMENSIONS.each do |dimension|
|
15
|
+
define_method dimension do |options|
|
16
|
+
@dimension = dimension
|
17
|
+
@data = options
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def post
|
23
|
+
check_for_nil_user
|
24
|
+
response = @client.post(url, @data)
|
25
|
+
@data = nil if response.code === '200'
|
26
|
+
response
|
27
|
+
end
|
28
|
+
|
29
|
+
def url
|
30
|
+
url = BASEURL + @dimension + "?api_key=#{@api_key}" + "&site_token=#{@site_token}"
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def check_for_nil_user
|
36
|
+
if @data.key?(:person_id) && @data[:person_id].nil?
|
37
|
+
@data.tap { |_data| @data.delete(:person_id) }
|
38
|
+
warn 'person_id is nil, event will not be track against a user'
|
39
|
+
end
|
40
|
+
end
|
43
41
|
end
|
44
42
|
end
|
data/lib/gosquared/trends.rb
CHANGED
@@ -1,52 +1,51 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'client'
|
2
2
|
|
3
3
|
module Gosquared
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
4
|
+
class Trends
|
5
|
+
BASEURL = 'https://api.gosquared.com/trends/v2/'.freeze
|
6
|
+
DIMENSIONS = %w[aggregate browser category country event language organisation os page path1 product screenDimensions sources transactions].freeze
|
7
|
+
@@filters = { date_format: @date_format, from: @from, to: @to,
|
8
|
+
format: @format, limit: @limit, sort: @sort, group: @group,
|
9
|
+
source_type: @source_type }
|
10
|
+
|
11
|
+
def initialize(api_key, site_token, client = Gosquared::Client.new)
|
12
|
+
@site_token = site_token
|
13
|
+
@api_key = api_key
|
14
|
+
@client = client
|
15
|
+
end
|
16
|
+
|
17
|
+
DIMENSIONS.each do |dimension|
|
18
|
+
define_method dimension do
|
19
|
+
@dimension = dimension
|
20
|
+
self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
@@filters.each do |key, _value|
|
25
|
+
define_method key do |argument|
|
26
|
+
@@filters[key] = argument
|
27
|
+
self
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def fetch
|
32
|
+
data = @client.get(url)
|
33
|
+
@@filters.each { |key, _value| @@filters[key] = nil } if data
|
34
|
+
data
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def url
|
40
|
+
array = ['']
|
41
|
+
url = BASEURL + @dimension + "?api_key=#{@api_key}" + "&site_token=#{@site_token}"
|
42
|
+
@@filters.each { |key, value| array << "#{camelize(key.to_s)}=#{value}" if value }
|
43
|
+
parameters = array.join('&')
|
44
|
+
url.concat(parameters)
|
45
|
+
end
|
46
|
+
|
47
|
+
def camelize(key)
|
48
|
+
key.split('_').each_with_index.map { |fragment, index| index == 0 ? fragment : fragment.capitalize }.join('')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/account_spec.rb
CHANGED
@@ -1,67 +1,65 @@
|
|
1
1
|
describe Gosquared::Account do
|
2
|
-
|
2
|
+
subject(:gs) { described_class.new('demo', 'GSN-2194840-F') }
|
3
|
+
VERSION = '3.1.0'.freeze
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
Gosquared::Account::DIMENSIONS.each do |dimension|
|
6
|
+
before do
|
7
|
+
data = '{"a": [{"test": "response"}]}'
|
8
|
+
stub_request(:get, "https://api.gosquared.com/account/v1/#{dimension}?api_key=demo&site_token=GSN-2194840-F")
|
9
|
+
.with(headers: { 'Accept' => '*/*' })
|
10
|
+
.to_return(status: 200, body: data, headers: {})
|
11
|
+
end
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
Gosquared::Account::DIMENSIONS.each do |dimension|
|
15
|
+
it "fetches a request from the GoSquared Account API with #{dimension} dimension" do
|
16
|
+
gs.send(dimension.to_s)
|
17
|
+
expect(gs.fetch).to eq('a' => [{ 'test' => 'response' }])
|
18
|
+
end
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
before do
|
22
|
+
data = '{"a": [{"test": "response"}]}'
|
23
|
+
stub_request(:get, 'https://api.gosquared.com/account/v1/sites/GSN-086224-W?api_key=demo&site_token=GSN-2194840-F')
|
24
|
+
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host' => 'api.gosquared.com' })
|
25
|
+
.to_return(status: 200, body: data, headers: {})
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
it 'retrieves a site by its site token' do
|
29
|
+
gs.sites.token('GSN-086224-W')
|
30
|
+
expect(gs.fetch).to eq('a' => [{ 'test' => 'response' }])
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
33
|
+
before do
|
34
|
+
stub_request(:post, 'https://api.gosquared.com/account/v1/blocked/ips?api_key=demo&ip=20.15.33.99&site_token=GSN-2194840-F')
|
35
|
+
.with(body: '[""]',
|
36
|
+
headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => 'application/json' })
|
37
|
+
.to_return(status: 200, body: '', headers: {})
|
38
|
+
stub_request(:delete, 'https://api.gosquared.com/account/v1/blocked/ips?api_key=demo&ip=20.15.33.99&site_token=GSN-2194840-F')
|
39
|
+
.with(body: '[""]',
|
40
|
+
headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type' => 'application/json' })
|
41
|
+
.to_return(status: 200, body: '', headers: {})
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
it "sends a delete request to the GoSquared Account API with an IP address to block bots" do
|
49
|
-
gs.blocked.ips.ip('20.15.33.99')
|
50
|
-
expect(gs.delete.code).to eq('200')
|
51
|
-
end
|
52
|
-
|
53
|
-
before do
|
54
|
-
data = '{"a": [{"test": "response"}]}'
|
55
|
-
stub_request(:get, "https://api.gosquared.com/account/v1/blocked/visitors/test.email@gmail.com?api_key=demo&site_token=GSN-2194840-F").
|
56
|
-
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'api.gosquared.com', 'User-Agent'=>'Ruby'}).
|
57
|
-
to_return(:status => 200, :body => data, :headers => {})
|
58
|
-
end
|
59
|
-
|
60
|
-
it "retrieves a list of blocked visitors" do
|
61
|
-
gs.blocked.visitors("test.email@gmail.com")
|
62
|
-
expect(gs.fetch).to eq("a" => [{"test"=>"response"}])
|
63
|
-
end
|
44
|
+
it 'posts a request to the GoSquared Account API with an IP address to block bots' do
|
45
|
+
gs.blocked.ips.ip('20.15.33.99')
|
46
|
+
expect(gs.post.code).to eq('200')
|
47
|
+
end
|
64
48
|
|
49
|
+
it 'sends a delete request to the GoSquared Account API with an IP address to block bots' do
|
50
|
+
gs.blocked.ips.ip('20.15.33.99')
|
51
|
+
expect(gs.delete.code).to eq('200')
|
52
|
+
end
|
65
53
|
|
54
|
+
before do
|
55
|
+
data = '{"a": [{"test": "response"}]}'
|
56
|
+
stub_request(:get, 'https://api.gosquared.com/account/v1/blocked/visitors/test.email@gmail.com?api_key=demo&site_token=GSN-2194840-F')
|
57
|
+
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host' => 'api.gosquared.com', 'User-Agent' => 'Ruby' })
|
58
|
+
.to_return(status: 200, body: data, headers: {})
|
59
|
+
end
|
66
60
|
|
61
|
+
it 'retrieves a list of blocked visitors' do
|
62
|
+
gs.blocked.visitors('test.email@gmail.com')
|
63
|
+
expect(gs.fetch).to eq('a' => [{ 'test' => 'response' }])
|
64
|
+
end
|
67
65
|
end
|
data/spec/client_spec.rb
CHANGED
@@ -3,69 +3,68 @@ require 'uri'
|
|
3
3
|
require 'json'
|
4
4
|
|
5
5
|
describe Gosquared::Client do
|
6
|
-
|
6
|
+
subject(:client) { described_class.new }
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
EXCEPTIONS = [Timeout::Error, EOFError,
|
9
|
+
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError].freeze
|
10
10
|
|
11
|
-
EXCEPTIONS.each do |exception|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
it "raises a Errno::EINVAL error on a get request" do
|
20
|
-
uri = URI('www.example.com')
|
21
|
-
allow(Net::HTTP).to receive(:get).with(uri).and_raise(Errno::EINVAL)
|
22
|
-
expect{client.get('www.example.com')}.to output("[error] HTTP error: Invalid argument\n[error] StandardError: Could not parse JSON\n").to_stdout
|
23
|
-
end
|
11
|
+
EXCEPTIONS.each do |exception|
|
12
|
+
it "raises a #{exception} error on a get request" do
|
13
|
+
uri = URI('www.example.com')
|
14
|
+
allow(Net::HTTP).to receive(:get).with(uri).and_raise(exception)
|
15
|
+
expect { client.get('www.example.com') }.to output("[error] HTTP error: #{exception}\n[error] StandardError: Could not parse JSON\n").to_stdout
|
16
|
+
end
|
17
|
+
end
|
24
18
|
|
25
|
-
it
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
19
|
+
it 'raises a Errno::EINVAL error on a get request' do
|
20
|
+
uri = URI('www.example.com')
|
21
|
+
allow(Net::HTTP).to receive(:get).with(uri).and_raise(Errno::EINVAL)
|
22
|
+
expect { client.get('www.example.com') }.to output("[error] HTTP error: Invalid argument\n[error] StandardError: Could not parse JSON\n").to_stdout
|
23
|
+
end
|
30
24
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
25
|
+
it 'raises a Errno::ECONNRESET error on a get request' do
|
26
|
+
uri = URI('www.example.com')
|
27
|
+
allow(Net::HTTP).to receive(:get).with(uri).and_raise(Errno::ECONNRESET)
|
28
|
+
expect { client.get('www.example.com') }.to output("[error] HTTP error: Connection reset by peer\n[error] StandardError: Could not parse JSON\n").to_stdout
|
29
|
+
end
|
38
30
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
31
|
+
EXCEPTIONS.each do |exception|
|
32
|
+
it "raises a #{exception} error on a post request" do
|
33
|
+
uri = URI.parse('www.example.com')
|
34
|
+
allow(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_raise(exception)
|
35
|
+
expect { client.post('www.example.com', 'body') }.to output("[error] HTTP error: #{exception}\n[error] StandardError: Could not print response message\n").to_stdout
|
36
|
+
end
|
37
|
+
end
|
44
38
|
|
45
|
-
it
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
39
|
+
it 'raises a Errno::EINVAL error on a post request' do
|
40
|
+
uri = URI.parse('www.example.com')
|
41
|
+
allow(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_raise(Errno::EINVAL)
|
42
|
+
expect { client.post('www.example.com', 'body') }.to output("[error] HTTP error: Invalid argument\n[error] StandardError: Could not print response message\n").to_stdout
|
43
|
+
end
|
50
44
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
45
|
+
it 'raises a Errno::ECONNRESET error on a post request' do
|
46
|
+
uri = URI.parse('www.example.com')
|
47
|
+
allow(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_raise(Errno::ECONNRESET)
|
48
|
+
expect { client.post('www.example.com', 'body') }.to output("[error] HTTP error: Connection reset by peer\n[error] StandardError: Could not print response message\n").to_stdout
|
49
|
+
end
|
58
50
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
51
|
+
EXCEPTIONS.each do |exception|
|
52
|
+
it "raises a #{exception} error on a delete request" do
|
53
|
+
uri = URI.parse('www.example.com')
|
54
|
+
allow(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_raise(exception)
|
55
|
+
expect { client.delete('www.example.com', 'body') }.to output("[error] HTTP error: #{exception}\n[error] StandardError: Could not print response message\n").to_stdout
|
56
|
+
end
|
57
|
+
end
|
64
58
|
|
65
|
-
it
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
59
|
+
it 'raises a Errno::EINVAL error on a delete request' do
|
60
|
+
uri = URI.parse('www.example.com')
|
61
|
+
allow(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_raise(Errno::EINVAL)
|
62
|
+
expect { client.delete('www.example.com', 'body') }.to output("[error] HTTP error: Invalid argument\n[error] StandardError: Could not print response message\n").to_stdout
|
63
|
+
end
|
70
64
|
|
65
|
+
it 'raises a Errno::ECONNRESET error on a delete request' do
|
66
|
+
uri = URI.parse('www.example.com')
|
67
|
+
allow(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_raise(Errno::ECONNRESET)
|
68
|
+
expect { client.delete('www.example.com', 'body') }.to output("[error] HTTP error: Connection reset by peer\n[error] StandardError: Could not print response message\n").to_stdout
|
69
|
+
end
|
71
70
|
end
|
data/spec/now_spec.rb
CHANGED
@@ -1,32 +1,31 @@
|
|
1
1
|
describe Gosquared::Now do
|
2
|
-
|
2
|
+
subject(:gs) { described_class.new('demo', 'GSN-2194840-F') }
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
Gosquared::Now::DIMENSIONS.each do |dimension|
|
5
|
+
before do
|
6
|
+
data = '{"a": [{"test": "response"}]}'
|
7
|
+
stub_request(:get, "https://api.gosquared.com/now/v3/#{dimension}?api_key=demo&site_token=GSN-2194840-F")
|
8
|
+
.with(headers: { 'Accept' => '*/*', 'User-Agent' => 'Ruby' })
|
9
|
+
.to_return(status: 200, body: data, headers: {})
|
10
|
+
end
|
11
|
+
end
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
Gosquared::Now::DIMENSIONS.each do |dimension|
|
14
|
+
it "fetches a request from the GoSquared Now API with #{dimension} dimension" do
|
15
|
+
gs.send dimension.to_s
|
16
|
+
expect(gs.fetch).to eq('a' => [{ 'test' => 'response' }])
|
17
|
+
end
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
it "fetches a request from the GoSquared Now API with paramaters" do
|
28
|
-
gs.browsers.limit('5')
|
29
|
-
expect(gs.fetch).to eq("a" => [{"test"=>"response"}, {"with"=>"params"}])
|
30
|
-
end
|
20
|
+
before do
|
21
|
+
data = '{"a": [{"test": "response"}, {"with": "params"}]}'
|
22
|
+
stub_request(:get, 'https://api.gosquared.com/now/v3/browsers?api_key=demo&site_token=GSN-2194840-F&limit=5')
|
23
|
+
.with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host' => 'api.gosquared.com', 'User-Agent' => 'Ruby' })
|
24
|
+
.to_return(status: 200, body: data, headers: {})
|
25
|
+
end
|
31
26
|
|
27
|
+
it 'fetches a request from the GoSquared Now API with paramaters' do
|
28
|
+
gs.browsers.limit('5')
|
29
|
+
expect(gs.fetch).to eq('a' => [{ 'test' => 'response' }, { 'with' => 'params' }])
|
30
|
+
end
|
32
31
|
end
|