Class: Piwik::User

Inherits:
Base
  • Object
show all
Defined in:
lib/piwik/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

parse_xml, #parse_xml

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

Raises:

  • (ArgumentError)


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_from_file
  else
    {:piwik_url => piwik_url, :auth_token => auth_token}
  end
  load_attributes(attributes)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/piwik/user.rb', line 4

def config
  @config
end

#created_atObject (readonly)

Returns the value of attribute created_at.



4
5
6
# File 'lib/piwik/user.rb', line 4

def created_at
  @created_at
end

#emailObject

Returns the value of attribute email.



3
4
5
# File 'lib/piwik/user.rb', line 3

def email
  @email
end

#loginObject

Returns the value of attribute login.



3
4
5
# File 'lib/piwik/user.rb', line 3

def 
  @login
end

#passwordObject

Returns the value of attribute password.



3
4
5
# File 'lib/piwik/user.rb', line 3

def password
  @password
end

#user_aliasObject

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).

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
# File 'lib/piwik/user.rb', line 95

def self.load(, piwik_url=nil, auth_token=nil)
  raise ArgumentError, "expected a user Login" if .nil?
  @config = if piwik_url.nil? || auth_token.nil?
    load_config_from_file
  else
    {:piwik_url => piwik_url, :auth_token => auth_token}
  end
  attributes = (, @config[:piwik_url], @config[:auth_token])
  new(attributes, @config[:piwik_url], @config[:auth_token])
end

Instance Method Details

#createObject

Saves the current new user in Piwik.

Equivalent Piwik API call: UsersManager.addUser (userLogin, password, email, alias)

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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 .blank?
  raise ArgumentError, "Password can not be blank" if password.blank?
  raise ArgumentError, "Email can not be blank" if email.blank?
  user_alias =  if user_alias.blank?
  
  xml = call('UsersManager.addUser', :userLogin => , :password => password, :email => email, :alias => user_alias)
  result = parse_xml(xml)
  @created_at = Time.current
  if result["success"]
    result["success"]["message"] == "ok" ? true : false
  else
    false
  end
end

#destroyObject

Deletes the current user from Piwik.

Equivalent Piwik API call: UsersManager.deleteUser (userLogin)

Raises:



78
79
80
81
82
83
84
# File 'lib/piwik/user.rb', line 78

def destroy
  raise UnknownUser, "User not existent in Piwik yet, call 'save' first" if new?
  xml = call('UsersManager.deleteUser', :userLogin => )
  result = parse_xml(xml)
  freeze
  result['success'] ? true : false
end

#new?Boolean

Returns true if the current site does not exists in the Piwik yet.

Returns:

  • (Boolean)


29
30
31
# File 'lib/piwik/user.rb', line 29

def new?
  created_at.nil? or created_at.blank?
end

#saveObject

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

#updateObject

Saves the current user in Piwik, updating it’s data.

Equivalent Piwik API call: UsersManager.updateUser (userLogin, password, email, alias)

Raises:



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/piwik/user.rb', line 63

def update
  raise UnknownUser, "User not existent in Piwik yet, call 'save' first" if new?
  raise ArgumentError, "Login can not be blank" if .blank?
  raise ArgumentError, "Password can not be blank" if password.blank?
  raise ArgumentError, "Email can not be blank" if email.blank?
  user_alias =  if user_alias.blank?
  
  xml = call('UsersManager.updateUser', :userLogin => , :password => password, :email => email, :alias => user_alias)
  result = parse_xml(xml)
  result['success'] ? true : false
end