Class: Piwik::User
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#email ⇒ Object
Returns the value of attribute email.
-
#login ⇒ Object
Returns the value of attribute login.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user_alias ⇒ Object
Returns the value of attribute user_alias.
Class Method Summary collapse
-
.load(user_login, piwik_url = nil, auth_token = nil) ⇒ Object
Returns an instance of
Piwik::User
representing the user identified by the supplieduserLogin
.
Instance Method Summary collapse
-
#create ⇒ Object
Saves the current new user in Piwik.
-
#destroy ⇒ Object
Deletes the current user from Piwik.
-
#initialize(attributes = {}, piwik_url = nil, auth_token = nil) ⇒ User
constructor
Initializes a new
Piwik::User
object, with the supplied attributes. -
#new? ⇒ Boolean
Returns
true
if the current site does not exists in the Piwik yet. -
#save ⇒ Object
Saves the current user in Piwik.
-
#update ⇒ Object
Saves the current user in Piwik, updating it’s data.
Methods inherited from Base
config_file, parse_json, #parse_json
Constructor Details
#initialize(attributes = {}, piwik_url = nil, auth_token = nil) ⇒ User
Initializes a new Piwik::User
object, with the supplied attributes.
You can pass the URL for your Piwik install and an authorization token as the second and third parameters. If you don’t, than it will try to find them in a '~/.piwik'
or RAILS_ROOT/config/piwik.yml
(and create the file with an empty template if it doesn’t exists).
Valid (and required) attributes are:
-
:login
- the user login -
:password
- the user password -
:email
- the user email -
:alias
- the user alias
18 19 20 21 22 23 24 25 26 |
# File 'lib/piwik/user.rb', line 18 def initialize(attributes={}, piwik_url=nil, auth_token=nil) raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) @config = if piwik_url.nil? || auth_token.nil? self.class.load_config else {:piwik_url => piwik_url, :auth_token => auth_token} end load_attributes(attributes) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/piwik/user.rb', line 4 def config @config end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
4 5 6 |
# File 'lib/piwik/user.rb', line 4 def created_at @created_at end |
#email ⇒ Object
Returns the value of attribute email.
3 4 5 |
# File 'lib/piwik/user.rb', line 3 def email @email end |
#login ⇒ Object
Returns the value of attribute login.
3 4 5 |
# File 'lib/piwik/user.rb', line 3 def login @login end |
#password ⇒ Object
Returns the value of attribute password.
3 4 5 |
# File 'lib/piwik/user.rb', line 3 def password @password end |
#user_alias ⇒ Object
Returns the value of attribute user_alias.
3 4 5 |
# File 'lib/piwik/user.rb', line 3 def user_alias @user_alias end |
Class Method Details
.load(user_login, piwik_url = nil, auth_token = nil) ⇒ Object
Returns an instance of Piwik::User
representing the user identified by the supplied userLogin
. Raises a Piwik::ApiError
if the user doesn’t exists or if the user associated with the supplied auth_token does not have ‘admin’ access.
You can pass the URL for your Piwik install and an authorization token as the second and third parameters. If you don’t, than it will try to find them in a '~/.piwik'
or RAILS_ROOT/config/piwik.yml
(and create the file with an empty template if it doesn’t exists).
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/piwik/user.rb', line 94 def self.load(user_login, piwik_url=nil, auth_token=nil) raise ArgumentError, "expected a user Login" if user_login.nil? @config = if piwik_url.nil? || auth_token.nil? load_config else {:piwik_url => piwik_url, :auth_token => auth_token} end attributes = get_user_attributes_by_login(user_login, @config[:piwik_url], @config[:auth_token]) new(attributes, @config[:piwik_url], @config[:auth_token]) end |
Instance Method Details
#create ⇒ Object
Saves the current new user in Piwik.
Equivalent Piwik API call: UsersManager.addUser (userLogin, password, email, alias)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/piwik/user.rb', line 43 def create raise ArgumentError, "User already exists in Piwik, call 'update' instead" unless new? raise ArgumentError, "Login can not be blank" if login.blank? raise ArgumentError, "Password can not be blank" if password.blank? raise ArgumentError, "Email can not be blank" if email.blank? user_alias = login if user_alias.blank? result = call('UsersManager.addUser', :userLogin => login, :password => password, :email => email, :alias => user_alias) @created_at = Time.current #puts "\n create #{result} \n" if result["result"] result['message'] == 'ok' ? true : false else false end end |
#destroy ⇒ Object
Deletes the current user from Piwik.
Equivalent Piwik API call: UsersManager.deleteUser (userLogin)
78 79 80 81 82 83 |
# File 'lib/piwik/user.rb', line 78 def destroy raise UnknownUser, "User not existent in Piwik yet, call 'save' first" if new? result = call('UsersManager.deleteUser', :userLogin => login) freeze result['result'] == 'success' ? true : false end |
#new? ⇒ Boolean
Returns true
if the current site does not exists in the Piwik yet.
29 30 31 |
# File 'lib/piwik/user.rb', line 29 def new? created_at.nil? or created_at.blank? end |
#save ⇒ Object
Saves the current user in Piwik.
Calls create
it it’s a new user, update
otherwise.
36 37 38 |
# File 'lib/piwik/user.rb', line 36 def save new? ? create : update end |
#update ⇒ Object
Saves the current user in Piwik, updating it’s data.
Equivalent Piwik API call: UsersManager.updateUser (userLogin, password, email, alias)
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/piwik/user.rb', line 64 def update raise UnknownUser, "User not existent in Piwik yet, call 'save' first" if new? raise ArgumentError, "Login can not be blank" if login.blank? raise ArgumentError, "Password can not be blank" if password.blank? raise ArgumentError, "Email can not be blank" if email.blank? user_alias = login if user_alias.blank? result = call('UsersManager.updateUser', :userLogin => login, :password => password, :email => email, :alias => user_alias) result['result'] == 'success' ? true : false end |