autometal-piwik 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +9 -0
- data/License.txt +20 -0
- data/Manifest.txt +20 -0
- data/PostInstall.txt +4 -0
- data/README.rdoc +81 -0
- data/Rakefile +16 -0
- data/Todo.txt +6 -0
- data/autometal-piwik.gemspec +50 -0
- data/lib/piwik.rb +10 -0
- data/lib/piwik/base.rb +55 -0
- data/lib/piwik/site.rb +252 -0
- data/lib/piwik/user.rb +5 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +82 -0
- data/setup.rb +1585 -0
- data/test/files/config/piwik.yml +2 -0
- data/test/piwik_test.rb +22 -0
- data/test/test_helper.rb +9 -0
- metadata +175 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Riopro Informática Ltda
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
PostInstall.txt
|
5
|
+
README.rdoc
|
6
|
+
Rakefile
|
7
|
+
Todo.txt
|
8
|
+
autometal-piwik.gemspec
|
9
|
+
lib/piwik.rb
|
10
|
+
lib/piwik/base.rb
|
11
|
+
lib/piwik/site.rb
|
12
|
+
lib/piwik/user.rb
|
13
|
+
script/console
|
14
|
+
script/destroy
|
15
|
+
script/generate
|
16
|
+
script/txt2html
|
17
|
+
setup.rb
|
18
|
+
test/files/config/piwik.yml
|
19
|
+
test/piwik_test.rb
|
20
|
+
test/test_helper.rb
|
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
= autometal-piwik
|
2
|
+
|
3
|
+
* http://github.com/riopro/Achillefs/tree/master
|
4
|
+
* http://humbuckercode.co.uk/licks/gems/piwik
|
5
|
+
|
6
|
+
== DESCRIPTION:
|
7
|
+
|
8
|
+
A simple Ruby client for the Piwik API. This gem is based on Rodrigo Tassinari de Oliveira's piwik gem (https://github.com/riopro/piwik). Since it hasn't been updated since 2008, I took the liberty to fork it, and finish it up.
|
9
|
+
|
10
|
+
== FEATURES/PROBLEMS:
|
11
|
+
|
12
|
+
* Object-oriented interface to the Piwik API;
|
13
|
+
* For now, only a small subset of the API is implemented (only basic actions)
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
Piwik is an open source web analytics software, written in PHP. It provides an
|
18
|
+
extensive REST-like API, and this gem aims to be a simple Ruby wrapper to access
|
19
|
+
this API in a Ruby-friendly way. For example:
|
20
|
+
|
21
|
+
require 'rubygems'
|
22
|
+
require 'piwik'
|
23
|
+
site = Piwik::Site.load(1, 'http://your.piwi.install', 'some_auth_key')
|
24
|
+
=> #<Piwik::Site:0xb74bf994 @name="Example.com", @config={:auth_token=>"some_auth_key", :piwik_url=>"http://your.piwi.install"}, @id=1, @main_url="http://www.example.com", @created_at=Tue Jul 15 18:55:40 -0300 2008>
|
25
|
+
site.pageviews(:month, Date.today)
|
26
|
+
=> 88
|
27
|
+
|
28
|
+
Currently, only Piwik::Site simple new / create / save / update methods have been implemented.
|
29
|
+
|
30
|
+
For more information on Piwik and it's API, see the Piwik website (http://piwik.org)
|
31
|
+
and the Piwik API reference (http://dev.piwik.org/trac/wiki/API/Reference).
|
32
|
+
|
33
|
+
== REQUIREMENTS:
|
34
|
+
|
35
|
+
RubyGems and the following gems (installed automatically if necessary):
|
36
|
+
|
37
|
+
active_support
|
38
|
+
xml-simple
|
39
|
+
rest-client
|
40
|
+
|
41
|
+
== INSTALL:
|
42
|
+
|
43
|
+
sudo gem install autometal-piwik --source=http://gemcutter.org
|
44
|
+
|
45
|
+
== CHANGELOG:
|
46
|
+
* 0.2.3
|
47
|
+
Started adding some tests
|
48
|
+
* 0.2.0
|
49
|
+
Reworked plugin to also look for a rails config file called piwik.yml, and not just the .piwik user prefs file.
|
50
|
+
* 0.0.2 2008-07-22 (riopro)
|
51
|
+
* Added specs for existing API methods
|
52
|
+
* Created RubyForge project at http://rubyforge.org/projects/piwik/
|
53
|
+
* 0.0.1 2008-07-21 (riopro)
|
54
|
+
* major enhancement:
|
55
|
+
* Initial release
|
56
|
+
|
57
|
+
|
58
|
+
== LICENSE:
|
59
|
+
|
60
|
+
(The MIT License)
|
61
|
+
|
62
|
+
Copyright (c) 2011 Achillefs Charmpilas, Humbucker Ltd
|
63
|
+
|
64
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
65
|
+
a copy of this software and associated documentation files (the
|
66
|
+
'Software'), to deal in the Software without restriction, including
|
67
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
68
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
69
|
+
permit persons to whom the Software is furnished to do so, subject to
|
70
|
+
the following conditions:
|
71
|
+
|
72
|
+
The above copyright notice and this permission notice shall be
|
73
|
+
included in all copies or substantial portions of the Software.
|
74
|
+
|
75
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
76
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
77
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
78
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
79
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
80
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
81
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/piwik'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
$hoe = Hoe.spec 'autometal-piwik' do
|
9
|
+
self.developer 'Achillefs Charmpilas', 'ac@humbuckercode.co.uk'
|
10
|
+
self.post_install_message = File.read('PostInstall.txt')
|
11
|
+
self.rubyforge_name = self.name
|
12
|
+
self.extra_deps = [['activesupport','>= 2.3.8'],['xml-simple',">=1.0.11"],["rest-client",">= 0.5.1"]]
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'newgem/tasks'
|
16
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
data/Todo.txt
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{autometal-piwik}
|
5
|
+
s.version = "0.2.3"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Achillefs Charmpilas"]
|
9
|
+
s.date = %q{2011-01-17}
|
10
|
+
s.description = %q{A simple Ruby client for the Piwik API. This gem is based on Rodrigo Tassinari de Oliveira's piwik gem (https://github.com/riopro/piwik). Since it hasn't been updated since 2008, I took the liberty to fork it, and finish it up.}
|
11
|
+
s.email = ["ac@humbuckercode.co.uk"]
|
12
|
+
s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "Todo.txt"]
|
13
|
+
s.files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "Todo.txt", "autometal-piwik.gemspec", "lib/piwik.rb", "lib/piwik/base.rb", "lib/piwik/site.rb", "lib/piwik/user.rb", "script/console", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "spec/piwik_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "test/test_helper.rb"]
|
14
|
+
s.homepage = %q{http://github.com/riopro/Achillefs/tree/master}
|
15
|
+
s.post_install_message = %q{
|
16
|
+
For more information on piwik, see http://piwik.rubyforge.org or
|
17
|
+
http://github.com/Achillefs/piwik/
|
18
|
+
|
19
|
+
}
|
20
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
s.rubyforge_project = %q{autometal-piwik}
|
23
|
+
s.rubygems_version = %q{1.4.2}
|
24
|
+
s.summary = %q{A simple Ruby client for the Piwik API}
|
25
|
+
s.test_files = ["test/test_helper.rb"]
|
26
|
+
|
27
|
+
if s.respond_to? :specification_version then
|
28
|
+
s.specification_version = 3
|
29
|
+
|
30
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
31
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 2.3.8"])
|
32
|
+
s.add_runtime_dependency(%q<xml-simple>, [">= 1.0.11"])
|
33
|
+
s.add_runtime_dependency(%q<rest-client>, [">= 0.5.1"])
|
34
|
+
s.add_development_dependency(%q<rubyforge>, [">= 2.0.4"])
|
35
|
+
s.add_development_dependency(%q<hoe>, [">= 2.6.2"])
|
36
|
+
else
|
37
|
+
s.add_dependency(%q<activesupport>, [">= 2.3.8"])
|
38
|
+
s.add_dependency(%q<xml-simple>, [">= 1.0.11"])
|
39
|
+
s.add_dependency(%q<rest-client>, [">= 0.5.1"])
|
40
|
+
s.add_dependency(%q<rubyforge>, [">= 2.0.4"])
|
41
|
+
s.add_dependency(%q<hoe>, [">= 2.6.2"])
|
42
|
+
end
|
43
|
+
else
|
44
|
+
s.add_dependency(%q<activesupport>, [">= 2.3.8"])
|
45
|
+
s.add_dependency(%q<xml-simple>, [">= 1.0.11"])
|
46
|
+
s.add_dependency(%q<rest-client>, [">= 0.5.1"])
|
47
|
+
s.add_dependency(%q<rubyforge>, [">= 2.0.4"])
|
48
|
+
s.add_dependency(%q<hoe>, [">= 2.6.2"])
|
49
|
+
end
|
50
|
+
end
|
data/lib/piwik.rb
ADDED
data/lib/piwik/base.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'cgi'
|
3
|
+
require 'active_support'
|
4
|
+
require 'rest_client'
|
5
|
+
require 'xmlsimple'
|
6
|
+
|
7
|
+
module Piwik
|
8
|
+
class ApiError < StandardError; end
|
9
|
+
class MissingConfiguration < ArgumentError; end
|
10
|
+
class UnknownSite < ArgumentError; end
|
11
|
+
class UnknownUser < ArgumentError; end
|
12
|
+
|
13
|
+
class Base
|
14
|
+
@@template = <<-EOF
|
15
|
+
# .piwik
|
16
|
+
#
|
17
|
+
# Please fill in fields like this:
|
18
|
+
#
|
19
|
+
# piwik_url: http://your.piwik.site
|
20
|
+
# auth_token: secret
|
21
|
+
#
|
22
|
+
piwik_url:
|
23
|
+
auth_token:
|
24
|
+
EOF
|
25
|
+
|
26
|
+
private
|
27
|
+
# Checks for the config, creates it if not found
|
28
|
+
def self.load_config_from_file
|
29
|
+
config = {}
|
30
|
+
if defined?(RAILS_ROOT) and RAILS_ROOT != nil
|
31
|
+
home = RAILS_ROOT
|
32
|
+
filename = "config/piwik.yml"
|
33
|
+
else
|
34
|
+
home = ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH'] || "."
|
35
|
+
filename = ".piwik"
|
36
|
+
end
|
37
|
+
temp_config = if File.exists?(File.join(home,filename))
|
38
|
+
YAML::load(open(File.join(home,filename)))
|
39
|
+
else
|
40
|
+
open(File.join(home,filename),'w') { |f| f.puts @@template }
|
41
|
+
YAML::load(@@template)
|
42
|
+
end
|
43
|
+
temp_config.each { |k,v| config[k.to_sym] = v } if temp_config
|
44
|
+
if config[:piwik_url] == nil || config[:auth_token] == nil
|
45
|
+
if defined?(RAILS_ROOT) and RAILS_ROOT != nil
|
46
|
+
raise MissingConfiguration, "Please edit ./config/piwik.yml to include your piwik url and auth_key"
|
47
|
+
else
|
48
|
+
raise MissingConfiguration, "Please edit ~/.piwik to include your piwik url and auth_key"
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
config
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/piwik/site.rb
ADDED
@@ -0,0 +1,252 @@
|
|
1
|
+
module Piwik
|
2
|
+
class Site < Piwik::Base
|
3
|
+
attr_accessor :name, :main_url
|
4
|
+
attr_reader :id, :created_at, :config
|
5
|
+
|
6
|
+
# Initializes a new <tt>Piwik::Site</tt> object, with the supplied attributes.
|
7
|
+
#
|
8
|
+
# You can pass the URL for your Piwik install and an authorization token as
|
9
|
+
# the second and third parameters. If you don't, than it will try to find
|
10
|
+
# them in a <tt>'~/.piwik'</tt> (and create the file with an empty template if it
|
11
|
+
# doesn't exists).
|
12
|
+
#
|
13
|
+
# Valid (and required) attributes are:
|
14
|
+
# * <tt>:name</tt> - the site's name
|
15
|
+
# * <tt>:main_url</tt> - the site's url
|
16
|
+
def initialize(attributes={}, piwik_url=nil, auth_token=nil)
|
17
|
+
raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
|
18
|
+
@config = if piwik_url.nil? || auth_token.nil?
|
19
|
+
self.class.load_config_from_file
|
20
|
+
else
|
21
|
+
{:piwik_url => piwik_url, :auth_token => auth_token}
|
22
|
+
end
|
23
|
+
load_attributes(attributes)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns an instance of <tt>Piwik::Site</tt> representing the site identified by
|
27
|
+
# the supplied <tt>site_id</tt>. Raises a <tt>Piwik::ApiError</tt> if the site doesn't
|
28
|
+
# exists or if the user associated with the supplied auth_token does not
|
29
|
+
# have at least 'view' access to the site.
|
30
|
+
#
|
31
|
+
# You can pass the URL for your Piwik install and an authorization token as
|
32
|
+
# the second and third parameters. If you don't, than it will try to find
|
33
|
+
# them in a <tt>'~/.piwik'</tt> (and create the file with an empty template if it
|
34
|
+
# doesn't exists).
|
35
|
+
def self.load(site_id, piwik_url=nil, auth_token=nil)
|
36
|
+
raise ArgumentError, "expected a site Id" if site_id.nil?
|
37
|
+
@config = if piwik_url.nil? || auth_token.nil?
|
38
|
+
load_config_from_file
|
39
|
+
else
|
40
|
+
{:piwik_url => piwik_url, :auth_token => auth_token}
|
41
|
+
end
|
42
|
+
attributes = get_site_attributes_by_id(site_id, @config[:piwik_url], @config[:auth_token])
|
43
|
+
new(attributes, @config[:piwik_url], @config[:auth_token])
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns <tt>true</tt> if the current site does not exists in the Piwik yet.
|
47
|
+
def new?
|
48
|
+
id.nil? && created_at.nil?
|
49
|
+
end
|
50
|
+
|
51
|
+
# Saves the current site in Piwik.
|
52
|
+
#
|
53
|
+
# Calls <tt>create</tt> it it's a new site, <tt>update</tt> otherwise.
|
54
|
+
def save
|
55
|
+
new? ? create : update
|
56
|
+
end
|
57
|
+
|
58
|
+
# Saves the current new site in Piwik.
|
59
|
+
#
|
60
|
+
# Equivalent Piwik API call: SitesManager.addSite (siteName, urls)
|
61
|
+
def create
|
62
|
+
raise ArgumentError, "Site already exists in Piwik, call 'update' instead" unless new?
|
63
|
+
raise ArgumentError, "Name can not be blank" if name.blank?
|
64
|
+
raise ArgumentError, "Main URL can not be blank" if main_url.blank?
|
65
|
+
xml = call('SitesManager.addSite', :siteName => name, :urls => main_url)
|
66
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
67
|
+
@id = result["result"].to_i
|
68
|
+
@created_at = Time.current
|
69
|
+
id && id > 0 ? true : false
|
70
|
+
end
|
71
|
+
|
72
|
+
# Saves the current site in Piwik, updating it's data.
|
73
|
+
#
|
74
|
+
# Equivalent Piwik API call: SitesManager.updateSite (idSite, siteName, urls)
|
75
|
+
def update
|
76
|
+
raise UnknownSite, "Site not existent in Piwik yet, call 'save' first" if new?
|
77
|
+
raise ArgumentError, "Name can not be blank" if name.blank?
|
78
|
+
raise ArgumentError, "Main URL can not be blank" if main_url.blank?
|
79
|
+
xml = call('SitesManager.updateSite', :idSite => id, :siteName => name, :urls => main_url)
|
80
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
81
|
+
result['success'] ? true : false
|
82
|
+
end
|
83
|
+
|
84
|
+
def reload
|
85
|
+
#TODO
|
86
|
+
end
|
87
|
+
|
88
|
+
# Deletes the current site from Piwik.
|
89
|
+
#
|
90
|
+
# Equivalent Piwik API call: SitesManager.deleteSite (idSite)
|
91
|
+
def destroy
|
92
|
+
raise UnknownSite, "Site not existent in Piwik yet, call 'save' first" if new?
|
93
|
+
xml = call('SitesManager.deleteSite', :idSite => id)
|
94
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
95
|
+
freeze
|
96
|
+
result['success'] ? true : false
|
97
|
+
end
|
98
|
+
|
99
|
+
# Gives read access (<tt>'view'</tt>) to the supplied user login for the current
|
100
|
+
# site.
|
101
|
+
def give_view_access_to(login)
|
102
|
+
give_access_to(:view, login)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Gives read and write access (<tt>'admin'</tt>) for the supplied user login for the
|
106
|
+
# current site.
|
107
|
+
def give_admin_access_to(login)
|
108
|
+
give_access_to(:admin, login)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Removes all access (gives an <tt>'noaccess'</tt>) for the supplied user login for
|
112
|
+
# the current site.
|
113
|
+
def give_no_access_to(login)
|
114
|
+
give_access_to(:noaccess, login)
|
115
|
+
end
|
116
|
+
alias_method :remove_access_from, :give_no_access_to
|
117
|
+
|
118
|
+
# Returns a hash with a summary of access information for the current site
|
119
|
+
# (visits, unique visitors, actions / pageviews, maximum actions per visit,
|
120
|
+
# bounces and total time spent in all visits in seconds), filtered by the
|
121
|
+
# supplied period and date.
|
122
|
+
#
|
123
|
+
# * <tt>period</tt> should be one of <tt>:day</tt>, <tt>:week</tt>, <tt>:month</tt> or <tt>:year</tt> (default: <tt>:day</tt>)
|
124
|
+
# * <tt>date</tt> should be a <tt>Date</tt> object (default: <tt>Date.today</tt>)
|
125
|
+
#
|
126
|
+
# Equivalent Piwik API call: VisitsSummary.get (idSite, period, date)
|
127
|
+
def summary(period=:day, date=Date.today)
|
128
|
+
raise UnknownSite, "Site not existent in Piwik yet, call 'save' first" if new?
|
129
|
+
xml = call('VisitsSummary.get', :idSite => id, :period => period, :date => date)
|
130
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
131
|
+
{
|
132
|
+
:visits => result['nb_visits'].to_i,
|
133
|
+
:unique_visitors => result['nb_uniq_visitors'].to_i,
|
134
|
+
:actions => result['nb_actions'].to_i,
|
135
|
+
:max_actions_per_visit => result['max_actions'].to_i,
|
136
|
+
:bounces => result['bounce_count'].to_i,
|
137
|
+
:total_time_spent => result['sum_visit_length'].to_i, # in seconds
|
138
|
+
}
|
139
|
+
end
|
140
|
+
|
141
|
+
# Returns the amount of visits for the current site, filtered by the
|
142
|
+
# supplied period and date.
|
143
|
+
#
|
144
|
+
# * <tt>period</tt> should be one of <tt>:day</tt>, <tt>:week</tt>, <tt>:month</tt> or <tt>:year</tt> (default: <tt>:day</tt>)
|
145
|
+
# * <tt>date</tt> should be a <tt>Date</tt> object (default: <tt>Date.today</tt>)
|
146
|
+
#
|
147
|
+
# Equivalent Piwik API call: VisitsSummary.getVisits (idSite, period, date)
|
148
|
+
def visits(period=:day, date=Date.today)
|
149
|
+
raise UnknownSite, "Site not existent in Piwik yet, call 'save' first" if new?
|
150
|
+
xml = call('VisitsSummary.getVisits', :idSite => id, :period => period, :date => date)
|
151
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
152
|
+
result.to_i
|
153
|
+
end
|
154
|
+
|
155
|
+
# Returns the amount of unique visitors for the current site, filtered by
|
156
|
+
# the supplied period and date.
|
157
|
+
#
|
158
|
+
# * <tt>period</tt> should be one of <tt>:day</tt>, <tt>:week</tt>, <tt>:month</tt> or <tt>:year</tt> (default: <tt>:day</tt>)
|
159
|
+
# * <tt>date</tt> should be a <tt>Date</tt> object (default: <tt>Date.today</tt>)
|
160
|
+
#
|
161
|
+
# Equivalent Piwik API call: VisitsSummary.getUniqueVisitors (idSite, period, date)
|
162
|
+
def unique_visitors(period=:day, date=Date.today)
|
163
|
+
raise UnknownSite, "Site not existent in Piwik yet, call 'save' first" if new?
|
164
|
+
xml = call('VisitsSummary.getUniqueVisitors', :idSite => id, :period => period, :date => date)
|
165
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
166
|
+
result.to_i
|
167
|
+
end
|
168
|
+
|
169
|
+
# Returns the amount of actions (pageviews) for the current site, filtered
|
170
|
+
# by the supplied period and date.
|
171
|
+
#
|
172
|
+
# * <tt>period</tt> should be one of <tt>:day</tt>, <tt>:week</tt>, <tt>:month</tt> or <tt>:year</tt> (default: <tt>:day</tt>)
|
173
|
+
# * <tt>date</tt> should be a <tt>Date</tt> object (default: <tt>Date.today</tt>)
|
174
|
+
#
|
175
|
+
# Equivalent Piwik API call: VisitsSummary.getActions (idSite, period, date)
|
176
|
+
def actions(period=:day, date=Date.today)
|
177
|
+
raise UnknownSite, "Site not existent in Piwik yet, call 'save' first" if new?
|
178
|
+
xml = call('VisitsSummary.getActions', :idSite => id, :period => period, :date => date)
|
179
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
180
|
+
result.to_i
|
181
|
+
end
|
182
|
+
alias_method :pageviews, :actions
|
183
|
+
|
184
|
+
private
|
185
|
+
# Calls the supplied Piwik API method, with the supplied parameters.
|
186
|
+
#
|
187
|
+
# Returns a string containing the XML reply from Piwik, or raises a
|
188
|
+
# <tt>Piwik::ApiError</tt> exception with the error message returned by Piwik
|
189
|
+
# in case it receives an error.
|
190
|
+
def call(method, params={})
|
191
|
+
self.class.call(method, params, config[:piwik_url], config[:auth_token])
|
192
|
+
end
|
193
|
+
|
194
|
+
# Loads the attributes in the instance variables.
|
195
|
+
def load_attributes(attributes)
|
196
|
+
@id = attributes[:id]
|
197
|
+
@name = attributes[:name]
|
198
|
+
@main_url = attributes[:main_url] ? attributes[:main_url].gsub(/\/$/, '') : nil
|
199
|
+
@created_at = attributes[:created_at]
|
200
|
+
end
|
201
|
+
|
202
|
+
# Gives the supplied access for the supplied user, for the current site.
|
203
|
+
#
|
204
|
+
# * <tt>access</tt> can be one of <tt>:view</tt>, <tt>:admin</tt> or <tt>:noaccess</tt>
|
205
|
+
# * <tt>login</tt> is the user login on Piwik
|
206
|
+
#
|
207
|
+
# Equivalent Piwik API call: UsersManager.setUserAccess (userLogin, access, idSites)
|
208
|
+
def give_access_to(access, login)
|
209
|
+
raise UnknownSite, "Site not existent in Piwik yet, call 'save' first" if new?
|
210
|
+
xml = call('UsersManager.setUserAccess', :idSites => id, :access => access.to_s, :userLogin => login.to_s)
|
211
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
212
|
+
result['success'] ? true : false
|
213
|
+
end
|
214
|
+
|
215
|
+
# Calls the supplied Piwik API method, with the supplied parameters.
|
216
|
+
#
|
217
|
+
# Returns a string containing the XML reply from Piwik, or raises a
|
218
|
+
# <tt>Piwik::ApiError</tt> exception with the error message returned by Piwik
|
219
|
+
# in case it receives an error.
|
220
|
+
def self.call(method, params={}, piwik_url=nil, auth_token=nil)
|
221
|
+
raise MissingConfiguration, "Please edit ~/.piwik to include your piwik url and auth_key" if piwik_url.nil? || auth_token.nil?
|
222
|
+
url = "#{piwik_url}/?module=API&format=xml&method=#{method}"
|
223
|
+
url << "&token_auth=#{auth_token}" unless auth_token.nil?
|
224
|
+
params.each { |k, v| url << "&#{k}=#{CGI.escape(v.to_s)}" }
|
225
|
+
verbose_obj_save = $VERBOSE
|
226
|
+
$VERBOSE = nil # Suppress "warning: peer certificate won't be verified in this SSL session"
|
227
|
+
xml = RestClient.get(url)
|
228
|
+
$VERBOSE = verbose_obj_save
|
229
|
+
if xml =~ /error message=/
|
230
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
231
|
+
raise ApiError, result['error']['message'] if result['error']
|
232
|
+
end
|
233
|
+
xml
|
234
|
+
end
|
235
|
+
|
236
|
+
# Returns a hash with the attributes of the supplied site, identified
|
237
|
+
# by it's Id in <tt>site_id</tt>.
|
238
|
+
#
|
239
|
+
# Equivalent Piwik API call: SitesManager.getSiteFromId (idSite)
|
240
|
+
def self.get_site_attributes_by_id(site_id, piwik_url, auth_token)
|
241
|
+
xml = call('SitesManager.getSiteFromId', {:idSite => site_id}, piwik_url, auth_token)
|
242
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
243
|
+
attributes = {
|
244
|
+
:id => result['row']['idsite'].to_i,
|
245
|
+
:name => result['row']['name'],
|
246
|
+
:main_url => result['row']['main_url'],
|
247
|
+
:created_at => Time.parse(result['row']['ts_created']),
|
248
|
+
}
|
249
|
+
attributes
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|