autometal-piwik 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -0
- data/autometal-piwik.gemspec +3 -3
- data/lib/piwik.rb +1 -1
- data/lib/piwik/base.rb +54 -24
- data/lib/piwik/site.rb +8 -33
- data/lib/piwik/user.rb +127 -1
- data/test/files/config/piwik.yml +2 -2
- data/test/piwik_test.rb +34 -1
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -24,6 +24,11 @@ this API in a Ruby-friendly way. For example:
|
|
24
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
25
|
site.pageviews(:month, Date.today)
|
26
26
|
=> 88
|
27
|
+
|
28
|
+
user = Piwik::User.load(1, 'http://your.piwi.install', 'some_auth_key')
|
29
|
+
=> #<Piwik::User:0xb66bf544 @login="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>
|
30
|
+
site.pageviews(:month, Date.today)
|
31
|
+
=> 88
|
27
32
|
|
28
33
|
Currently, only Piwik::Site simple new / create / save / update methods have been implemented.
|
29
34
|
|
@@ -43,6 +48,8 @@ RubyGems and the following gems (installed automatically if necessary):
|
|
43
48
|
sudo gem install autometal-piwik --source=http://gemcutter.org
|
44
49
|
|
45
50
|
== CHANGELOG:
|
51
|
+
* 0.3.0
|
52
|
+
UsersManager CRUD implementation, with tests
|
46
53
|
* 0.2.3
|
47
54
|
Started adding some tests
|
48
55
|
* 0.2.0
|
data/autometal-piwik.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{autometal-piwik}
|
5
|
-
s.version = "0.
|
5
|
+
s.version = "0.3.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Achillefs Charmpilas"]
|
9
|
-
s.date = %q{2011-01-
|
9
|
+
s.date = %q{2011-01-18}
|
10
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
11
|
s.email = ["ac@humbuckercode.co.uk"]
|
12
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", "
|
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", "test/files/config/piwik.yml", "test/piwik_test.rb", "test/test_helper.rb"]
|
14
14
|
s.homepage = %q{http://github.com/riopro/Achillefs/tree/master}
|
15
15
|
s.post_install_message = %q{
|
16
16
|
For more information on piwik, see http://piwik.rubyforge.org or
|
data/lib/piwik.rb
CHANGED
data/lib/piwik/base.rb
CHANGED
@@ -23,33 +23,63 @@ piwik_url:
|
|
23
23
|
auth_token:
|
24
24
|
EOF
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
private
|
27
|
+
# Calls the supplied Piwik API method, with the supplied parameters.
|
28
|
+
#
|
29
|
+
# Returns a string containing the XML reply from Piwik, or raises a
|
30
|
+
# <tt>Piwik::ApiError</tt> exception with the error message returned by Piwik
|
31
|
+
# in case it receives an error.
|
32
|
+
def call(method, params={})
|
33
|
+
self.class.call(method, params, config[:piwik_url], config[:auth_token])
|
34
|
+
end
|
35
|
+
|
36
|
+
# Calls the supplied Piwik API method, with the supplied parameters.
|
37
|
+
#
|
38
|
+
# Returns a string containing the XML reply from Piwik, or raises a
|
39
|
+
# <tt>Piwik::ApiError</tt> exception with the error message returned by Piwik
|
40
|
+
# in case it receives an error.
|
41
|
+
def self.call(method, params={}, piwik_url=nil, auth_token=nil)
|
42
|
+
raise MissingConfiguration, "Please edit ~/.piwik to include your piwik url and auth_key" if piwik_url.nil? || auth_token.nil?
|
43
|
+
url = "#{piwik_url}/?module=API&format=xml&method=#{method}"
|
44
|
+
url << "&token_auth=#{auth_token}" unless auth_token.nil?
|
45
|
+
params.each { |k, v| url << "&#{k}=#{CGI.escape(v.to_s)}" }
|
46
|
+
verbose_obj_save = $VERBOSE
|
47
|
+
$VERBOSE = nil # Suppress "warning: peer certificate won't be verified in this SSL session"
|
48
|
+
xml = RestClient.get(url)
|
49
|
+
$VERBOSE = verbose_obj_save
|
50
|
+
if xml =~ /error message=/
|
51
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
52
|
+
raise ApiError, result['error']['message'] if result['error']
|
53
|
+
end
|
54
|
+
xml
|
55
|
+
end
|
56
|
+
|
57
|
+
# Checks for the config, creates it if not found
|
58
|
+
def self.load_config_from_file
|
59
|
+
config = {}
|
60
|
+
if defined?(RAILS_ROOT) and RAILS_ROOT != nil
|
61
|
+
home = RAILS_ROOT
|
62
|
+
filename = "config/piwik.yml"
|
63
|
+
else
|
64
|
+
home = ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH'] || "."
|
65
|
+
filename = ".piwik"
|
66
|
+
end
|
67
|
+
temp_config = if File.exists?(File.join(home,filename))
|
68
|
+
YAML::load(open(File.join(home,filename)))
|
69
|
+
else
|
70
|
+
open(File.join(home,filename),'w') { |f| f.puts @@template }
|
71
|
+
YAML::load(@@template)
|
72
|
+
end
|
73
|
+
temp_config.each { |k,v| config[k.to_sym] = v } if temp_config
|
74
|
+
if config[:piwik_url] == nil || config[:auth_token] == nil
|
30
75
|
if defined?(RAILS_ROOT) and RAILS_ROOT != nil
|
31
|
-
|
32
|
-
filename = "config/piwik.yml"
|
76
|
+
raise MissingConfiguration, "Please edit ./config/piwik.yml to include your piwik url and auth_key"
|
33
77
|
else
|
34
|
-
|
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
|
-
|
78
|
+
raise MissingConfiguration, "Please edit ~/.piwik to include your piwik url and auth_key"
|
51
79
|
end
|
52
|
-
|
80
|
+
|
53
81
|
end
|
82
|
+
config
|
83
|
+
end
|
54
84
|
end
|
55
85
|
end
|
data/lib/piwik/site.rb
CHANGED
@@ -7,8 +7,8 @@ module Piwik
|
|
7
7
|
#
|
8
8
|
# You can pass the URL for your Piwik install and an authorization token as
|
9
9
|
# the second and third parameters. If you don't, than it will try to find
|
10
|
-
# them in a <tt>'~/.piwik'</tt>
|
11
|
-
# doesn't exists).
|
10
|
+
# them in a <tt>'~/.piwik'</tt> or <tt>RAILS_ROOT/config/piwik.yml</tt>
|
11
|
+
# (and create the file with an empty template if it doesn't exists).
|
12
12
|
#
|
13
13
|
# Valid (and required) attributes are:
|
14
14
|
# * <tt>:name</tt> - the site's name
|
@@ -64,7 +64,12 @@ module Piwik
|
|
64
64
|
raise ArgumentError, "Main URL can not be blank" if main_url.blank?
|
65
65
|
xml = call('SitesManager.addSite', :siteName => name, :urls => main_url)
|
66
66
|
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
67
|
-
|
67
|
+
case result.class
|
68
|
+
when Hash
|
69
|
+
@id = result["result"].to_i
|
70
|
+
else
|
71
|
+
@id = result.to_i
|
72
|
+
end
|
68
73
|
@created_at = Time.current
|
69
74
|
id && id > 0 ? true : false
|
70
75
|
end
|
@@ -182,15 +187,6 @@ module Piwik
|
|
182
187
|
alias_method :pageviews, :actions
|
183
188
|
|
184
189
|
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
190
|
# Loads the attributes in the instance variables.
|
195
191
|
def load_attributes(attributes)
|
196
192
|
@id = attributes[:id]
|
@@ -212,27 +208,6 @@ module Piwik
|
|
212
208
|
result['success'] ? true : false
|
213
209
|
end
|
214
210
|
|
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
211
|
# Returns a hash with the attributes of the supplied site, identified
|
237
212
|
# by it's Id in <tt>site_id</tt>.
|
238
213
|
#
|
data/lib/piwik/user.rb
CHANGED
@@ -1,5 +1,131 @@
|
|
1
1
|
module Piwik
|
2
2
|
class User < Piwik::Base
|
3
|
-
|
3
|
+
attr_accessor :login, :password, :email, :user_alias
|
4
|
+
attr_reader :created_at, :config
|
5
|
+
|
6
|
+
# Initializes a new <tt>Piwik::User</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> or <tt>RAILS_ROOT/config/piwik.yml</tt>
|
11
|
+
# (and create the file with an empty template if it doesn't exists).
|
12
|
+
#
|
13
|
+
# Valid (and required) attributes are:
|
14
|
+
# * <tt>:login</tt> - the user login
|
15
|
+
# * <tt>:password</tt> - the user password
|
16
|
+
# * <tt>:email</tt> - the user email
|
17
|
+
# * <tt>:alias</tt> - the user alias
|
18
|
+
def initialize(attributes={}, piwik_url=nil, auth_token=nil)
|
19
|
+
raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
|
20
|
+
@config = if piwik_url.nil? || auth_token.nil?
|
21
|
+
self.class.load_config_from_file
|
22
|
+
else
|
23
|
+
{:piwik_url => piwik_url, :auth_token => auth_token}
|
24
|
+
end
|
25
|
+
load_attributes(attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns <tt>true</tt> if the current site does not exists in the Piwik yet.
|
29
|
+
def new?
|
30
|
+
created_at.nil? or created_at.blank?
|
31
|
+
end
|
32
|
+
|
33
|
+
# Saves the current user in Piwik.
|
34
|
+
#
|
35
|
+
# Calls <tt>create</tt> it it's a new user, <tt>update</tt> otherwise.
|
36
|
+
def save
|
37
|
+
new? ? create : update
|
38
|
+
end
|
39
|
+
|
40
|
+
# Saves the current new user in Piwik.
|
41
|
+
#
|
42
|
+
# Equivalent Piwik API call: UsersManager.addUser (userLogin, password, email, alias)
|
43
|
+
def create
|
44
|
+
raise ArgumentError, "User already exists in Piwik, call 'update' instead" unless new?
|
45
|
+
raise ArgumentError, "Login can not be blank" if login.blank?
|
46
|
+
raise ArgumentError, "Password can not be blank" if password.blank?
|
47
|
+
raise ArgumentError, "Email can not be blank" if email.blank?
|
48
|
+
user_alias = login if user_alias.blank?
|
49
|
+
|
50
|
+
xml = call('UsersManager.addUser', :userLogin => login, :password => password, :email => email, :alias => user_alias)
|
51
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
52
|
+
@created_at = Time.current
|
53
|
+
result["success"]["message"] == "ok" ? true : false
|
54
|
+
end
|
55
|
+
|
56
|
+
# Saves the current user in Piwik, updating it's data.
|
57
|
+
#
|
58
|
+
# Equivalent Piwik API call: UsersManager.updateUser (userLogin, password, email, alias)
|
59
|
+
def update
|
60
|
+
raise UnknownUser, "User not existent in Piwik yet, call 'save' first" if new?
|
61
|
+
raise ArgumentError, "Login can not be blank" if login.blank?
|
62
|
+
raise ArgumentError, "Password can not be blank" if password.blank?
|
63
|
+
raise ArgumentError, "Email can not be blank" if email.blank?
|
64
|
+
user_alias = login if user_alias.blank?
|
65
|
+
|
66
|
+
xml = call('UsersManager.updateUser', :userLogin => login, :password => password, :email => email, :alias => user_alias)
|
67
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
68
|
+
result['success'] ? true : false
|
69
|
+
end
|
70
|
+
|
71
|
+
# Deletes the current user from Piwik.
|
72
|
+
#
|
73
|
+
# Equivalent Piwik API call: UsersManager.deleteUser (userLogin)
|
74
|
+
def destroy
|
75
|
+
raise UnknownUser, "User not existent in Piwik yet, call 'save' first" if new?
|
76
|
+
xml = call('UsersManager.deleteUser', :userLogin => login)
|
77
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
78
|
+
freeze
|
79
|
+
result['success'] ? true : false
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns an instance of <tt>Piwik::User</tt> representing the user identified by
|
83
|
+
# the supplied <tt>userLogin</tt>. Raises a <tt>Piwik::ApiError</tt> if the user doesn't
|
84
|
+
# exists or if the user associated with the supplied auth_token does not
|
85
|
+
# have 'admin' access.
|
86
|
+
#
|
87
|
+
# You can pass the URL for your Piwik install and an authorization token as
|
88
|
+
# the second and third parameters. If you don't, than it will try to find
|
89
|
+
# them in a <tt>'~/.piwik'</tt> or <tt>RAILS_ROOT/config/piwik.yml</tt>
|
90
|
+
# (and create the file with an empty template if it doesn't exists).
|
91
|
+
def self.load(user_login, piwik_url=nil, auth_token=nil)
|
92
|
+
raise ArgumentError, "expected a user Login" if user_login.nil?
|
93
|
+
@config = if piwik_url.nil? || auth_token.nil?
|
94
|
+
load_config_from_file
|
95
|
+
else
|
96
|
+
{:piwik_url => piwik_url, :auth_token => auth_token}
|
97
|
+
end
|
98
|
+
attributes = get_user_attributes_by_login(user_login, @config[:piwik_url], @config[:auth_token])
|
99
|
+
new(attributes, @config[:piwik_url], @config[:auth_token])
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
# Loads the attributes in the instance variables.
|
104
|
+
def load_attributes(attributes)
|
105
|
+
@login = attributes[:login]
|
106
|
+
@password = attributes[:password]
|
107
|
+
@email = attributes[:email]
|
108
|
+
@user_alias = attributes[:user_alias]
|
109
|
+
@created_at = attributes[:created_at]
|
110
|
+
end
|
111
|
+
|
112
|
+
# Returns a hash with the attributes of the supplied user, identified
|
113
|
+
# by it's Login in <tt>user_login</tt>.
|
114
|
+
#
|
115
|
+
# Equivalent Piwik API call: UsersManager.getUser (userLogin)
|
116
|
+
def self.get_user_attributes_by_login(user_login, piwik_url, auth_token)
|
117
|
+
xml = call('UsersManager.getUser', {:userLogin => user_login}, piwik_url, auth_token)
|
118
|
+
result = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
119
|
+
|
120
|
+
puts result.inspect
|
121
|
+
|
122
|
+
attributes = {
|
123
|
+
:login => result['row']['login'],
|
124
|
+
:user_alias => result['row']['alias'],
|
125
|
+
:email => result['row']['email'],
|
126
|
+
:created_at => Time.parse(result['row']['date_registered']),
|
127
|
+
}
|
128
|
+
attributes
|
129
|
+
end
|
4
130
|
end
|
5
131
|
end
|
data/test/files/config/piwik.yml
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
auth_token:
|
2
|
-
piwik_url: http://
|
1
|
+
auth_token: 95b15f2ef6dfdc40b5604fb4d453b69b
|
2
|
+
piwik_url: http://analytics.product-wave.com
|
data/test/piwik_test.rb
CHANGED
@@ -2,7 +2,39 @@ require File.join(File.dirname(__FILE__),"test_helper")
|
|
2
2
|
|
3
3
|
class PiwikTest < Test::Unit::TestCase
|
4
4
|
def setup
|
5
|
-
|
5
|
+
@domain = "http://test.local"
|
6
|
+
@name = "Test Site"
|
7
|
+
@site = Piwik::Site.new(:name => @name, :main_url => @domain)
|
8
|
+
@login = "test_user"
|
9
|
+
@password = "changeme"
|
10
|
+
@email = "test@pwave.com"
|
11
|
+
@user_alias = "Test User"
|
12
|
+
@user = Piwik::User.new(:login => @login, :password => @password, :email => @email, :user_alias => @user_alias)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_can_instantiate_site
|
16
|
+
assert_equal @name, @site.name
|
17
|
+
assert_equal @domain, @site.main_url
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_can_save_and_destroy_site
|
21
|
+
assert_equal nil, @site.id
|
22
|
+
@site.save
|
23
|
+
assert_not_equal 0, @site.id
|
24
|
+
assert_not_equal nil, @site.id
|
25
|
+
assert_equal true, @site.destroy
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_can_instantiate_user
|
29
|
+
assert_equal @login, @user.login
|
30
|
+
assert_equal @password, @user.password
|
31
|
+
assert_equal @email, @user.email
|
32
|
+
assert_equal @user_alias, @user.user_alias
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_can_save_and_destroy_user
|
36
|
+
@user.save
|
37
|
+
assert_equal true, @user.destroy
|
6
38
|
end
|
7
39
|
|
8
40
|
def test_can_read_standalone_config
|
@@ -12,6 +44,7 @@ class PiwikTest < Test::Unit::TestCase
|
|
12
44
|
end
|
13
45
|
end
|
14
46
|
|
47
|
+
|
15
48
|
def test_can_read_rails_config
|
16
49
|
stub_rails_env do
|
17
50
|
assert_nothing_raised do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: autometal-piwik
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 2
|
9
8
|
- 3
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Achillefs Charmpilas
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-18 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|