statcounter 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a07f45e20aad61cd55786e84bcb777ecb6cffb6
4
- data.tar.gz: 0b47936f0a2454d253e5e896572027c4df974c81
3
+ metadata.gz: b22c925b0a6e4979a589c600ce5a14a7fdac3470
4
+ data.tar.gz: 6839234079b43d0ad9e5009c314c50c15507e554
5
5
  SHA512:
6
- metadata.gz: 67f0ac03e7fa837d29d93a1648f75c7a50216a7485a841b4d8a8a9b0b5740ed4ab072476beb068dfe614a047382375ae7bdfdef0ab631fee7116cf2acb86cb6e
7
- data.tar.gz: f077806e3fd8d8b7005e827f3dd87c112ee838bafc7b817e0434461be6dee54fd4d73c81af9c20d11653b2d808cbd205f5f038ef019b0b15770097657eac0e63
6
+ metadata.gz: dde7493e662532d654e4744d0808923df5d92912a17b1937f730252995226169d983026e69c64aeacf2e7c7c5e3b0c4df37e589a5b844daf790e7a0a47b503a8
7
+ data.tar.gz: 8ef5c97b4f29bb6bd7378c8a11b93e9ebc48dc8c305c4fa1b239fb1e830f988d321a341622789da61d481c048f91042edc583772e3b052363013a3f198940110
data/.rubocop.yml CHANGED
@@ -15,3 +15,6 @@ TrailingCommaInLiteral:
15
15
 
16
16
  TrailingCommaInArguments:
17
17
  Enabled: false
18
+
19
+ AllCops:
20
+ TargetRubyVersion: 2.2.2
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.2
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.1
3
+ - 2.2.2
4
4
  script: 'bundle exec rspec'
5
5
  notifications:
6
6
  email: false
data/README.md CHANGED
@@ -1,4 +1,184 @@
1
1
  # Statcounter
2
2
 
3
- TODO: Add readme
3
+ [![Build Status](https://secure.travis-ci.org/ksarunas/statcounter.png)](http://travis-ci.org/ksarunas/statcounter)
4
+ [![Gem Version](https://badge.fury.io/rb/statcounter.png)](http://badge.fury.io/rb/statcounter)
4
5
 
6
+ Ruby Statcounter API wrapper
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'statcounter'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install statcounter
23
+
24
+ ## Initialization
25
+
26
+ ```ruby
27
+ # config/initializers/statcounter.rb
28
+
29
+ Statcounter.configure do |config|
30
+ config.username = 'STATCOUNTER_USERNAME_HERE'
31
+ config.secret = 'STATCOUNTER_API_PASSWORD_HERE'
32
+ config.timeout = 60 # default is 60
33
+ config.timezone = 'America/New_York' #default is America/New_York
34
+ end
35
+ ```
36
+
37
+ In case your app has to handle multiple Statcounter accounts you can pass credentials to each endpoint wrapper method like this:
38
+
39
+ ```ruby
40
+ Statcounter::Projects.all(username: 'STATCOUNTER_USERNAME_HERE', secret: 'STATCOUNTER_API_PASSWORD_HERE')
41
+ ```
42
+
43
+ ## Examples
44
+
45
+ ### Projects
46
+
47
+ #### Get all your projects
48
+ ```ruby
49
+ Statcounter::Projects.all
50
+
51
+ # returns array of project hashes
52
+ [
53
+ {
54
+ project_id: '1000000',
55
+ project_name: 'mywebsite1.com',
56
+ url: 'https://www.mywebsite1.com',
57
+ project_group_id: '1',
58
+ project_group_name: 'Websites',
59
+ hidden_group: '0',
60
+ },
61
+ ]
62
+ ```
63
+
64
+ #### Find projects
65
+
66
+ When selecting one projects:
67
+
68
+ ```ruby
69
+ Statcounter::Projects.find(1000000)
70
+
71
+ # returns project hash
72
+ {
73
+ project_name: 'mywebsite1.com',
74
+ log_size: '500',
75
+ timezone: 'America/New_York',
76
+ url: 'https://www.mywebsite1.com',
77
+ log_oldest_entry: 'LOG_OLDEST_ENTRY',
78
+ log_latest_entry: 'LOG_LATEST_ENTRY',
79
+ created_at: 'CREATED_AT'
80
+ }
81
+ ```
82
+
83
+ When selecting multiple projects:
84
+
85
+ ```ruby
86
+ Statcounter::Projects.find([1000000, 1000001])
87
+
88
+ # returns array of project hashes
89
+ [
90
+ {
91
+ project_name: 'mywebsite1.com',
92
+ log_size: '500',
93
+ timezone: 'America/New_York',
94
+ url: 'https://www.mywebsite1.com',
95
+ log_oldest_entry: 'LOG_OLDEST_ENTRY',
96
+ log_latest_entry: 'LOG_LATEST_ENTRY',
97
+ created_at: 'CREATED_AT'
98
+ },
99
+ {
100
+ project_name: 'mywebsite2.com',
101
+ log_size: '500',
102
+ timezone: 'America/New_York',
103
+ url: 'https://www.mywebsite2.com',
104
+ log_oldest_entry: 'LOG_OLDEST_ENTRY',
105
+ log_latest_entry: 'LOG_LATEST_ENTRY',
106
+ created_at: 'CREATED_AT'
107
+ }
108
+ ]
109
+ ```
110
+
111
+ #### Create project
112
+
113
+ ```ruby
114
+ Statcounter::Projects.create(
115
+ project_name: 'mywebsite1.com',
116
+ url: 'https://www.mywebsite2.com',
117
+ public_stats: true, # default false
118
+ )
119
+
120
+ # returns hash with id and security code
121
+ {
122
+ project_id: '1000000',
123
+ security_code: 'hjk89077hh',
124
+ }
125
+ ```
126
+
127
+ ### Summary stats
128
+
129
+ #### Get daily
130
+
131
+ When one project:
132
+
133
+ ```ruby
134
+ Statcounter::SummaryStats.daily(
135
+ project_ids: 1000,
136
+ date_from: Date.yesterday,
137
+ date_to: Date.yesterday,
138
+ )
139
+
140
+ # Returns array of summary stats by date
141
+
142
+ [
143
+ {
144
+ date: '2016-07-04',
145
+ page_views: '876',
146
+ unique_visits: '609',
147
+ returning_visits: '57',
148
+ first_time_visits: '552',
149
+ }
150
+ ]
151
+ ```
152
+
153
+ When multiple projects:
154
+
155
+ ```ruby
156
+ Statcounter::SummaryStats.daily(
157
+ project_ids: [1000, 1001],
158
+ date_from: Date.yesterday,
159
+ date_to: Date.yesterday,
160
+ )
161
+
162
+ # Returns hash where key is project id and value is array of summery stats by date
163
+
164
+ {
165
+ 1000 => [
166
+ {
167
+ date: '2016-07-04',
168
+ page_views: '876',
169
+ unique_visits: '609',
170
+ returning_visits: '57',
171
+ first_time_visits: '552',
172
+ }
173
+ ],
174
+ 1001 => [
175
+ {
176
+ date: '2016-07-04',
177
+ page_views: '876',
178
+ unique_visits: '609',
179
+ returning_visits: '57',
180
+ first_time_visits: '552',
181
+ }
182
+ ],
183
+ }
184
+ ```
data/lib/statcounter.rb CHANGED
@@ -4,6 +4,7 @@ require 'statcounter/configuration'
4
4
  require 'statcounter/errors'
5
5
  require 'statcounter/params_encoder'
6
6
  require 'statcounter/projects'
7
+ require 'statcounter/summary_stats'
7
8
 
8
9
  module Statcounter
9
10
  API_URL = 'http://api.statcounter.com/'.freeze
@@ -1,9 +1,13 @@
1
1
  module Statcounter
2
2
  class Configuration
3
- attr_accessor :username, :secret, :timeout
3
+ attr_accessor :username, :secret, :timeout, :timezone
4
4
 
5
5
  def timeout
6
6
  @timeout ||= 60
7
7
  end
8
+
9
+ def timezone
10
+ @timezone ||= 'America/New_York'
11
+ end
8
12
  end
9
13
  end
@@ -5,9 +5,20 @@ module Statcounter
5
5
  response[:sc_data]
6
6
  end
7
7
 
8
- def self.find(project_id, credentials: nil)
9
- params = { pi: project_id }
8
+ def self.find(project_ids, credentials: nil)
9
+ params = { pi: project_ids }
10
10
  response = Statcounter.client.get('select_project', params: params, credentials: credentials)
11
+ response[:sc_data].size > 1 ? response[:sc_data] : response[:sc_data][0]
12
+ end
13
+
14
+ def self.create(project_name:, url:, public_stats: false, credentials: nil)
15
+ params = {
16
+ wt: project_name,
17
+ wu: url,
18
+ ps: public_stats ? 1 : 0,
19
+ }
20
+
21
+ response = Statcounter.client.get('add_project', params: params, credentials: credentials)
11
22
  response[:sc_data][0]
12
23
  end
13
24
  end
@@ -0,0 +1,25 @@
1
+ module Statcounter
2
+ class SummaryStats
3
+ def self.daily(project_ids:, date_from:, date_to:, credentials: nil)
4
+ params = {
5
+ s: :summary,
6
+ g: :daily,
7
+ pi: project_ids,
8
+ sd: date_from.day,
9
+ sm: date_from.month,
10
+ sy: date_from.year,
11
+ ed: date_to.day,
12
+ em: date_to.month,
13
+ ey: date_to.year,
14
+ }
15
+ response = Statcounter.client.get('stats', params: params, credentials: credentials)
16
+ if response[:project]
17
+ response[:project].each_with_object({}) do |project, result|
18
+ result[project[:id].to_i] = project[:sc_data]
19
+ end
20
+ else
21
+ response[:sc_data]
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Statcounter
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -0,0 +1,9 @@
1
+ {
2
+ "@attributes": {
3
+ "status": "ok"
4
+ },
5
+ "sc_data": [{
6
+ "project_id": "123123123",
7
+ "security_code": "hjk89077hh"
8
+ }]
9
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "@attributes": {
3
+ "status":"ok"
4
+ },
5
+ "sc_data":[
6
+ {
7
+ "project_name":"PROJECT_NAME",
8
+ "log_size": "LOG_SIZE",
9
+ "timezone": "TIME_ZONE",
10
+ "url":"URL",
11
+ "log_oldest_entry":"LOG_OLDEST_ENTRY",
12
+ "log_latest_entry":"LOG_LATEST_ENTRY",
13
+ "created_at":"CREATED_AT"
14
+ },
15
+ {
16
+ "project_name":"PROJECT_NAME2",
17
+ "log_size": "LOG_SIZE",
18
+ "timezone": "TIME_ZONE",
19
+ "url":"URL2",
20
+ "log_oldest_entry":"LOG_OLDEST_ENTRY",
21
+ "log_latest_entry":"LOG_LATEST_ENTRY",
22
+ "created_at":"CREATED_AT"
23
+ }
24
+ ]
25
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "@attributes":{
3
+ "status":"ok"
4
+ },
5
+ "sc_data":[
6
+ {
7
+ "date":"2016-06-28",
8
+ "page_views":"876",
9
+ "unique_visits":"609",
10
+ "returning_visits":"57",
11
+ "first_time_visits":"552"
12
+ }
13
+ ]
14
+ }
@@ -0,0 +1,31 @@
1
+ {
2
+ "@attributes":{
3
+ "status":"ok"
4
+ },
5
+ "project":[
6
+ {
7
+ "id":"1",
8
+ "sc_data":[
9
+ {
10
+ "date":"2016-06-28",
11
+ "page_views":"876",
12
+ "unique_visits":"609",
13
+ "returning_visits":"57",
14
+ "first_time_visits":"552"
15
+ }
16
+ ]
17
+ },
18
+ {
19
+ "id":"2",
20
+ "sc_data":[
21
+ {
22
+ "date":"2016-06-28",
23
+ "page_views":"28",
24
+ "unique_visits":"25",
25
+ "returning_visits":"7",
26
+ "first_time_visits":"18"
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ }
@@ -5,7 +5,7 @@ RSpec.describe Statcounter::Projects do
5
5
  subject { described_class.all(credentials: default_credentials) }
6
6
 
7
7
  before do
8
- stub_request(:get, 'http://api.statcounter.com/user_projects?f=json&sha1=06290316a4a4aa9911db04b42294609a8a4694e4&t=1466614800&u=john_brown&vn=3')
8
+ stub_request(:get, 'http://api.statcounter.com/user_projects?vn=3&t=1466614800&u=john_brown&f=json&sha1=06290316a4a4aa9911db04b42294609a8a4694e4')
9
9
  .to_return(body: File.read('spec/assets/user_projects.json'))
10
10
  end
11
11
 
@@ -23,13 +23,12 @@ RSpec.describe Statcounter::Projects do
23
23
  end
24
24
 
25
25
  describe '.find' do
26
- subject { described_class.find(project_id, credentials: default_credentials) }
27
- let(:project_id) { 1 }
26
+ subject { described_class.find(project_ids, credentials: default_credentials) }
27
+ let(:project_ids) { 1 }
28
+ let(:request_uri) { 'http://api.statcounter.com/select_project?pi=1&vn=3&t=1466614800&u=john_brown&f=json&sha1=229b79fa8787e1a8bf8d09c3a1cbec96a8219364' }
29
+ let(:response_file) { 'spec/assets/select_project.json' }
28
30
 
29
- before do
30
- stub_request(:get, 'http://api.statcounter.com/select_project?f=json&pi=1&sha1=229b79fa8787e1a8bf8d09c3a1cbec96a8219364&t=1466614800&u=john_brown&vn=3')
31
- .to_return(body: File.read('spec/assets/select_project.json'))
32
- end
31
+ before { stub_request(:get, request_uri).to_return(body: File.read(response_file)) }
33
32
 
34
33
  it 'returns project details' do
35
34
  expect(subject).to be_instance_of Hash
@@ -43,5 +42,40 @@ RSpec.describe Statcounter::Projects do
43
42
  :created_at,
44
43
  )
45
44
  end
45
+
46
+ context 'when multiple ids passed' do
47
+ let(:project_ids) { [1, 2] }
48
+ let(:request_uri) { 'http://api.statcounter.com/select_project?pi=1&pi=2&vn=3&t=1466614800&u=john_brown&f=json&sha1=397509ff17b98ad6abc394d97e7d761d768c8b22' }
49
+ let(:response_file) { 'spec/assets/select_project_multiple.json' }
50
+
51
+ it 'returns multiple project details' do
52
+ expect(subject).to be_instance_of Array
53
+ expect(subject.size).to eq 2
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '.create' do
59
+ subject do
60
+ described_class.create(
61
+ project_name: project_name,
62
+ url: url,
63
+ public_stats: public_stats,
64
+ credentials: default_credentials
65
+ )
66
+ end
67
+
68
+ let(:project_name) { 'Website title' }
69
+ let(:url) { 'http://websiteurl.com' }
70
+ let(:public_stats) { false }
71
+
72
+ before do
73
+ stub_request(:get, "http://api.statcounter.com/add_project?wt=#{project_name}&wu=#{url}&ps=0&vn=3&t=1466614800&u=john_brown&f=json&sha1=46f09a1b09f4c144508966353f406c8e109295f9")
74
+ .to_return(body: File.read('spec/assets/add_project.json'))
75
+ end
76
+
77
+ it 'creates new project' do
78
+ expect(subject).to include(:project_id, :security_code)
79
+ end
46
80
  end
47
81
  end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,7 @@ require 'rack'
7
7
  require 'webmock/rspec'
8
8
 
9
9
  WebMock.disable_net_connect!
10
+ WebMock::Config.instance.query_values_notation = :flat_array
10
11
 
11
12
  Dir['./spec/support/**/*.rb'].each { |f| require(f) }
12
13
 
@@ -0,0 +1,51 @@
1
+ RSpec.describe Statcounter::SummaryStats do
2
+ before { Timecop.freeze(Time.at(1466614800)) }
3
+
4
+ describe '.daily' do
5
+ subject do
6
+ described_class.daily(
7
+ project_ids: project_ids,
8
+ date_from: date_from,
9
+ date_to: date_to,
10
+ credentials: default_credentials
11
+ )
12
+ end
13
+
14
+ let(:project_ids) { 1 }
15
+ let(:date_from) { Date.new(2016, 6, 28) }
16
+ let(:date_to) { Date.new(2016, 6, 28) }
17
+ let(:request_uri) { 'http://api.statcounter.com/stats?s=summary&g=daily&pi=1&sd=28&sm=6&sy=2016&ed=28&em=6&ey=2016&vn=3&t=1466614800&u=john_brown&f=json&sha1=9c16ca0e1034bc8d878396b0c34a5232e971ce35' }
18
+ let(:response_file) { 'spec/assets/summary_stats_daily.json' }
19
+
20
+ before { stub_request(:get, request_uri).to_return(body: File.read(response_file)) }
21
+
22
+ it 'returns summery stats' do
23
+ expect(subject).to be_instance_of Array
24
+ expect(subject[0]).to include(
25
+ :date,
26
+ :page_views,
27
+ :unique_visits,
28
+ :returning_visits,
29
+ :first_time_visits,
30
+ )
31
+ end
32
+
33
+ context 'when multiple ids passed' do
34
+ let(:project_ids) { [1, 2] }
35
+ let(:request_uri) { 'http://api.statcounter.com/stats?s=summary&g=daily&pi=1&pi=2&sd=28&sm=6&sy=2016&ed=28&em=6&ey=2016&vn=3&t=1466614800&u=john_brown&f=json&sha1=ab0a3d2be2b3a7270ee1c2c0655e1cd7c3de818e' }
36
+ let(:response_file) { 'spec/assets/summary_stats_daily_multiple.json' }
37
+
38
+ it 'returns multiple project summery stats' do
39
+ expect(subject).to be_instance_of Hash
40
+ expect(subject.keys.size).to eq 2
41
+ expect(subject[2][0]).to include(
42
+ :date,
43
+ :page_views,
44
+ :unique_visits,
45
+ :returning_visits,
46
+ :first_time_visits,
47
+ )
48
+ end
49
+ end
50
+ end
51
+ end
data/statcounter.gemspec CHANGED
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'rspec', '~> 3.0'
25
25
  spec.add_development_dependency 'timecop', '~> 0.8.1'
26
26
  spec.add_development_dependency 'rack', '~> 1.6.4'
27
- spec.add_development_dependency 'webmock', '~> 1.24'
27
+ spec.add_development_dependency 'webmock', '~> 2.1'
28
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statcounter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Šarūnas Kūjalis"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-22 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '1.24'
117
+ version: '2.1'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '1.24'
124
+ version: '2.1'
125
125
  description:
126
126
  email:
127
127
  - sarjalis@gmail.com
@@ -132,6 +132,7 @@ files:
132
132
  - ".gitignore"
133
133
  - ".rspec"
134
134
  - ".rubocop.yml"
135
+ - ".ruby-version"
135
136
  - ".travis.yml"
136
137
  - Gemfile
137
138
  - README.md
@@ -142,14 +143,20 @@ files:
142
143
  - lib/statcounter/errors.rb
143
144
  - lib/statcounter/params_encoder.rb
144
145
  - lib/statcounter/projects.rb
146
+ - lib/statcounter/summary_stats.rb
145
147
  - lib/statcounter/version.rb
148
+ - spec/assets/add_project.json
146
149
  - spec/assets/failure.json
147
150
  - spec/assets/select_project.json
151
+ - spec/assets/select_project_multiple.json
152
+ - spec/assets/summary_stats_daily.json
153
+ - spec/assets/summary_stats_daily_multiple.json
148
154
  - spec/assets/user_projects.json
149
155
  - spec/client_spec.rb
150
156
  - spec/params_encoder_spec.rb
151
157
  - spec/projects_spec.rb
152
158
  - spec/spec_helper.rb
159
+ - spec/summary_stats_spec.rb
153
160
  - spec/support/request_stubbing.rb
154
161
  - statcounter.gemspec
155
162
  homepage: https://github.com/ksarunas/statcounter