gosquared 0.1.5 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ require './lib/gosquared/client'
2
+ class Now
3
+
4
+ BASEURL = "https://api.gosquared.com/now/v3/"
5
+ DIMENSIONS = %w(browsers campaigns concurrents engagement geo languages notifications
6
+ organisations overview pages platforms sources time timeSeries visitors)
7
+ @@filters = {dateFormat: @date_format, from: @from, to: @to,
8
+ format: @format, limit: @limit, sort: @sort,
9
+ presenter: @presenter, visitors_mode: @string, href: @href,
10
+ drill_limit: @drill_limit, sections: @sections,
11
+ minimal: @minimal, interval: @interval}
12
+
13
+ def initialize(api_key, site_token, client=Client.new)
14
+ @site_token = site_token
15
+ @api_key = api_key
16
+ @client = client
17
+ end
18
+
19
+
20
+ DIMENSIONS.each do |dimension|
21
+ define_method dimension do
22
+ @dimension = dimension
23
+ self
24
+ end
25
+ end
26
+
27
+ @@filters.each do |key, value|
28
+ define_method key do |argument|
29
+ @@filters[key] = argument
30
+ self
31
+ end
32
+ end
33
+
34
+ def fetch
35
+ data = @client.get(url)
36
+ @@filters.each{|key, value| @@filters[key]=nil} if data
37
+ data
38
+ end
39
+
40
+ private
41
+
42
+ def url
43
+ array = [""]
44
+ @url = BASEURL + @dimension + "?api_key=#{@api_key}" + "&site_token=#{@site_token}"
45
+ @@filters.each {|key, value| array << "#{key}=#{value}" if value }
46
+ parameters=array.join('&')
47
+ @url = @url.concat(parameters)
48
+ end
49
+
50
+ end
@@ -0,0 +1,54 @@
1
+ require './lib/gosquared/client'
2
+ class People
3
+
4
+ BASEURL = "https://api.gosquared.com/people/v1/"
5
+ VERSION = %w(v1 v2 v3)
6
+ DIMENSIONS = %w(devices eventTypes people propertyTypes feed smartgroups)
7
+ @@filters = {query: @query, filters: @filters, sort: @sort,
8
+ format: @presenter, limit: @limit, type: @type, from: @from, to: @to}
9
+
10
+ def initialize(api_key, site_token, client =Client.new)
11
+ @site_token = site_token
12
+ @api_key = api_key
13
+ @person_id = ""
14
+ @person_filter = ""
15
+ @client = client
16
+ end
17
+
18
+ DIMENSIONS.each do |dimension|
19
+ define_method dimension do |options = ""|
20
+ @dimension = dimension
21
+ @data = options
22
+ self
23
+ end
24
+ end
25
+
26
+ @@filters.each do |key, value|
27
+ define_method key do |argument|
28
+ @@filters[key] = argument
29
+ self
30
+ end
31
+ end
32
+
33
+ def person_id(object, filter)
34
+ @person_id = "/" + object
35
+ @person_filter = "/" + filter
36
+ self
37
+ end
38
+
39
+ def fetch
40
+ data = @client.get(url)
41
+ @@filters.each{|key, value| @@filters[key]=nil} if data
42
+ data
43
+ end
44
+
45
+ def url
46
+ array = [""]
47
+ url = BASEURL + @dimension + @person_id + @person_filter +
48
+ "?api_key=#{@api_key}" + "&site_token=#{@site_token}"
49
+ @@filters.each { |key, value| array << "#{key}=#{value}" if value }
50
+ parameters=array.join('&')
51
+ url.concat(parameters)
52
+ end
53
+
54
+ end
@@ -0,0 +1,29 @@
1
+ require './lib/gosquared/client'
2
+ class Tracking
3
+
4
+ BASEURL = "https://api.gosquared.com/tracking/v1/"
5
+ DIMENSIONS = %w(event identify pageview ping properties timeout transaction)
6
+
7
+ def initialize(api_key, site_token, client=Client.new)
8
+ @site_token = site_token
9
+ @api_key = api_key
10
+ @client = client
11
+ end
12
+
13
+ DIMENSIONS.each do |dimension|
14
+ define_method dimension do |options|
15
+ @dimension = dimension
16
+ @data = options
17
+ self
18
+ end
19
+ end
20
+
21
+ def post
22
+ @client.post(url, @data)
23
+ end
24
+
25
+ def url
26
+ url = BASEURL + @dimension + "?api_key=#{@api_key}" + "&site_token=#{@site_token}"
27
+ end
28
+
29
+ end
@@ -0,0 +1,44 @@
1
+ require './lib/gosquared/client'
2
+ class Trends
3
+
4
+ BASEURL = "https://api.gosquared.com/trends/v2/"
5
+ DIMENSIONS = %w(aggregate browser category country event language organisation os page path1 product screenDimensions sources transactions)
6
+ @@filters = {date_format: @date_format, from: @from, to: @to,
7
+ format: @format, limit: @limit, sort: @sort, group: @group}
8
+
9
+ def initialize(api_key, site_token, client=Client.new)
10
+ @site_token = site_token
11
+ @api_key = api_key
12
+ @client = client
13
+ end
14
+
15
+ DIMENSIONS.each do |dimension|
16
+ define_method dimension do
17
+ @dimension = dimension
18
+ self
19
+ end
20
+ end
21
+
22
+ @@filters.each do |key, value|
23
+ define_method key do |argument|
24
+ @@filters[key] = argument
25
+ self
26
+ end
27
+ end
28
+
29
+ def fetch
30
+ data = @client.get(url)
31
+ @@filters.each{|key, value| @@filters[key]=nil} if data
32
+ data
33
+ end
34
+
35
+ private
36
+
37
+ def url
38
+ array = [""]
39
+ url = BASEURL + @dimension + "?api_key=#{@api_key}" + "&site_token=#{@site_token}"
40
+ @@filters.each {|key, value| array << "#{key}=#{value}" if value }
41
+ parameters=array.join('&')
42
+ url.concat(parameters)
43
+ end
44
+ end
@@ -0,0 +1,67 @@
1
+ describe Account do
2
+ subject(:gs) { described_class.new("demo", "GSN-2194840-F") }
3
+
4
+ Account::DIMENSIONS.each do |dimension|
5
+ before do
6
+ data = '{"a": [{"test": "response"}]}'
7
+ stub_request(:get, "https://api.gosquared.com/account/v1/#{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
+
13
+ Account::DIMENSIONS.each do |dimension|
14
+ it "fetches a request from the GoSquared Account API with #{dimension} dimension" do
15
+ gs.send("#{dimension}")
16
+ expect(gs.fetch).to eq("a" => [{"test"=>"response"}])
17
+ end
18
+ end
19
+
20
+ before do
21
+ data = '{"a": [{"test": "response"}]}'
22
+ stub_request(:get, "https://api.gosquared.com/account/v1/sites/GSN-086224-W?api_key=demo&site_token=GSN-2194840-F").
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
26
+
27
+ it "retrieves a site by its site token" do
28
+ gs.sites.token("GSN-086224-W")
29
+ expect(gs.fetch).to eq("a" => [{"test"=>"response"}])
30
+ end
31
+
32
+ before do
33
+ stub_request(:post, "https://api.gosquared.com/account/v1/blocked/ips?api_key=demo&ip=20.15.33.99&site_token=GSN-2194840-F").
34
+ with(:body => "[ \"\" ]",
35
+ :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
36
+ to_return(:status => 200, :body => "", :headers => {})
37
+ stub_request(:delete, "https://api.gosquared.com/account/v1/blocked/ips?api_key=demo&ip=20.15.33.99&site_token=GSN-2194840-F").
38
+ with(:body => "[ \"\" ]",
39
+ :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
40
+ to_return(:status => 200, :body => "", :headers => {})
41
+ end
42
+
43
+ it "posts a request to the GoSquared Account API with an IP address to block bots" do
44
+ gs.blocked.ips.ip('20.15.33.99')
45
+ expect(gs.post.code).to eq('200')
46
+ end
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&ip=20.15.33.99&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
64
+
65
+
66
+
67
+ end
@@ -0,0 +1,71 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ describe Client do
6
+ subject(:client) { described_class.new }
7
+
8
+ EXCEPTIONS = [Timeout::Error, EOFError,
9
+ Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError]
10
+
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
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
24
+
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
30
+
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
38
+
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
44
+
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
50
+
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
58
+
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
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
70
+
71
+ end
data/spec/now_spec.rb ADDED
@@ -0,0 +1,32 @@
1
+ describe Now do
2
+ subject(:gs) { described_class.new("demo", "GSN-2194840-F") }
3
+
4
+ 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
+
13
+ Now::DIMENSIONS.each do |dimension|
14
+ it "fetches a request from the GoSquared Now API with #{dimension} dimension" do
15
+ gs.send "#{dimension}"
16
+ expect(gs.fetch).to eq("a" => [{"test"=>"response"}])
17
+ end
18
+ end
19
+
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
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
31
+
32
+ end
@@ -0,0 +1,51 @@
1
+ describe People do
2
+ subject(:gs) { described_class.new("demo", "GSN-106863-S") }
3
+
4
+ People::DIMENSIONS.each do |dimension|
5
+ before do
6
+ data = '{"a": [{"test": "response"}]}'
7
+ stub_request(:get, "https://api.gosquared.com/people/v1/#{dimension}?api_key=demo&site_token=GSN-106863-S"
8
+ ).
9
+ with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
10
+ to_return(status: 200, body: data, headers: {})
11
+ end
12
+ end
13
+
14
+ People::DIMENSIONS.each do |dimension|
15
+ it "fetches a request from the GoSquared People API with #{dimension} dimension" do
16
+ gs.send "#{dimension}"
17
+ expect(gs.fetch).to eq("a" => [{"test"=>"response"}])
18
+ end
19
+ end
20
+
21
+ before do
22
+ data = '{"a": [{"test": "response"}, {"with": "params"}]}'
23
+ stub_request(:get, "https://api.gosquared.com/people/v1/people/example.email@example.com/devices?api_key=demo&site_token=GSN-106863-S").
24
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'api.gosquared.com', 'User-Agent'=>'Ruby'}).
25
+ to_return(:status => 200, :body => data, :headers => {})
26
+ end
27
+
28
+ it "fetches a request from the GoSquared People API with a person_id and paramaters" do
29
+ gs.people.person_id('example.email@example.com','devices')
30
+ expect(gs.fetch).to eq("a" => [{"test"=>"response"}, {"with"=>"params"}])
31
+ end
32
+
33
+
34
+ People::DIMENSIONS.each do |dimension|
35
+ before do
36
+ data = '{"a": [{"test": "response"}, {"with": "params"}]}'
37
+ stub_request(:get, "https://api.gosquared.com/people/v1/#{dimension}?api_key=demo&site_token=GSN-106863-S&limit=5").
38
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Host'=>'api.gosquared.com', 'User-Agent'=>'Ruby'}).
39
+ to_return(:status => 200, :body => data, :headers => {})
40
+ end
41
+ end
42
+
43
+ GoSquared::People::DIMENSIONS.each do |dimension|
44
+ it "fetches a request from the GoSquared People API with dimension and paramaters" do
45
+ gs.send("#{dimension}").limit(5)
46
+ expect(gs.fetch).to eq("a" => [{"test"=>"response"}, {"with"=>"params"}])
47
+ end
48
+ end
49
+
50
+
51
+ end
@@ -0,0 +1,105 @@
1
+ require 'gosquared.rb'
2
+ require 'gosquared/account.rb'
3
+ require 'gosquared/now.rb'
4
+ require 'gosquared/trends.rb'
5
+ require 'gosquared/tracking.rb'
6
+ require 'gosquared/people.rb'
7
+ require 'gosquared/client.rb'
8
+ require 'webmock/rspec'
9
+
10
+ # This file was generated by the `rspec --init` command. Conventionally, all
11
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
12
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
13
+ # this file to always be loaded, without a need to explicitly require it in any
14
+ # files.
15
+ #
16
+ # Given that it is always loaded, you are encouraged to keep this file as
17
+ # light-weight as possible. Requiring heavyweight dependencies from this file
18
+ # will add to the boot time of your test suite on EVERY test run, even for an
19
+ # individual file that may not need all of that loaded. Instead, consider making
20
+ # a separate helper file that requires the additional dependencies and performs
21
+ # the additional setup, and require it from the spec files that actually need
22
+ # it.
23
+ #
24
+ # The `.rspec` file also contains a few flags that are not defaults but that
25
+ # users commonly want.
26
+ #
27
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
28
+ RSpec.configure do |config|
29
+ # rspec-expectations config goes here. You can use an alternate
30
+ # assertion/expectation library such as wrong or the stdlib/minitest
31
+ # assertions if you prefer.
32
+ config.expect_with :rspec do |expectations|
33
+ # This option will default to `true` in RSpec 4. It makes the `description`
34
+ # and `failure_message` of custom matchers include text for helper methods
35
+ # defined using `chain`, e.g.:
36
+ # be_bigger_than(2).and_smaller_than(4).description
37
+ # # => "be bigger than 2 and smaller than 4"
38
+ # ...rather than:
39
+ # # => "be bigger than 2"
40
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
41
+ end
42
+
43
+ # rspec-mocks config goes here. You can use an alternate test double
44
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
45
+ config.mock_with :rspec do |mocks|
46
+ # Prevents you from mocking or stubbing a method that does not exist on
47
+ # a real object. This is generally recommended, and will default to
48
+ # `true` in RSpec 4.
49
+ mocks.verify_partial_doubles = true
50
+ end
51
+
52
+ # The settings below are suggested to provide a good initial experience
53
+ # with RSpec, but feel free to customize to your heart's content.
54
+ =begin
55
+ # These two settings work together to allow you to limit a spec run
56
+ # to individual examples or groups you care about by tagging them with
57
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
58
+ # get run.
59
+ config.filter_run :focus
60
+ config.run_all_when_everything_filtered = true
61
+
62
+ # Allows RSpec to persist some state between runs in order to support
63
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
64
+ # you configure your source control system to ignore this file.
65
+ config.example_status_persistence_file_path = "spec/examples.txt"
66
+
67
+ # Limits the available syntax to the non-monkey patched syntax that is
68
+ # recommended. For more details, see:
69
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
70
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
72
+ config.disable_monkey_patching!
73
+
74
+ # This setting enables warnings. It's recommended, but in some cases may
75
+ # be too noisy due to issues in dependencies.
76
+ config.warnings = true
77
+
78
+ # Many RSpec users commonly either run the entire suite or an individual
79
+ # file, and it's useful to allow more verbose output when running an
80
+ # individual spec file.
81
+ if config.files_to_run.one?
82
+ # Use the documentation formatter for detailed output,
83
+ # unless a formatter has already been configured
84
+ # (e.g. via a command-line flag).
85
+ config.default_formatter = 'doc'
86
+ end
87
+
88
+ # Print the 10 slowest examples and example groups at the
89
+ # end of the spec run, to help surface which specs are running
90
+ # particularly slow.
91
+ config.profile_examples = 10
92
+
93
+ # Run specs in random order to surface order dependencies. If you find an
94
+ # order dependency and want to debug it, you can fix the order by providing
95
+ # the seed, which is printed after each run.
96
+ # --seed 1234
97
+ config.order = :random
98
+
99
+ # Seed global randomization in this process using the `--seed` CLI option.
100
+ # Setting this allows you to use `--seed` to deterministically reproduce
101
+ # test failures related to randomization by passing the same `--seed` value
102
+ # as the one that triggered the failure.
103
+ Kernel.srand config.seed
104
+ =end
105
+ end