Class: WTForum
- Inherits:
-
Object
show all
- Defined in:
- lib/wtforum.rb,
lib/wtforum/user.rb,
lib/wtforum/session.rb,
lib/wtforum/version.rb
Defined Under Namespace
Classes: Session, User, WTForumError
Constant Summary
collapse
- VERSION =
"0.7.0"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(credentials) ⇒ WTForum
Returns a new instance of WTForum.
24
25
26
27
28
|
# File 'lib/wtforum.rb', line 24
def initialize credentials
credentials.each do |key, value|
self.send :"#{key}=", value
end
end
|
Instance Attribute Details
#admin_password ⇒ Object
Returns the value of attribute admin_password.
22
23
24
|
# File 'lib/wtforum.rb', line 22
def admin_password
@admin_password
end
|
#admin_username ⇒ Object
Returns the value of attribute admin_username.
22
23
24
|
# File 'lib/wtforum.rb', line 22
def admin_username
@admin_username
end
|
#api_key ⇒ Object
Returns the value of attribute api_key.
22
23
24
|
# File 'lib/wtforum.rb', line 22
def api_key
@api_key
end
|
#domain ⇒ Object
Returns the value of attribute domain.
22
23
24
|
# File 'lib/wtforum.rb', line 22
def domain
@domain
end
|
Class Method Details
12
13
14
15
16
17
18
19
20
|
# File 'lib/wtforum.rb', line 12
def self. key, options
xml = Nokogiri::XML.parse(options[:from])
node = xml.css(key.to_s)
if node.present?
node.text
else
raise WTForumError, xml.css("errormessage, error, .errorMsg").text
end
end
|
Instance Method Details
#count_users ⇒ Object
118
119
120
121
122
|
# File 'lib/wtforum.rb', line 118
def count_users
response = agent.get uri(path: "/register/members")
count = response.body.match(/Members\s+\(([\d,]+)\)/m)[1]
count.gsub(",", "").to_i
end
|
#create_session(user_id) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/wtforum.rb', line 30
def create_session user_id
uri = base_api_uri(userid: user_id)
uri.path = "/register/setauthtoken"
response = agent.get uri
Session.create self, response
end
|
#create_user(attributes) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/wtforum.rb', line 37
def create_user attributes
keep_trying = attributes.has_key?(:retry) ? attributes.delete(:retry) : true
original_name = attributes[:username]
defaults = { pw: Digest::MD5.hexdigest(attributes.to_s) }
attributes[:member] ||= attributes.delete(:username)
attributes[:field276177] ||= attributes.delete(:gender)
attributes[:field276178] ||= attributes.delete(:location)
attributes[:field276179] ||= attributes.delete(:about)
attributes.reverse_merge! defaults
begin
uri = base_api_uri(attributes)
uri.path = "/register/create_account"
response = agent.get uri
User.create self, response, attributes
rescue WTForum::WTForumError => e
if e.message =~ /^Error: The username "(.+?)" has already been taken\.$/
if keep_trying
index = attributes[:member].sub(original_name,"").to_i
new_username = "#{original_name}#{index+1}"
attributes[:member] = new_username
retry
else
raise User::UsernameAlreadyTaken.new(e.message)
end
elsif e.message =~ /Error: It looks like you are already registered as "(.+?)" with that email address./
find_user_by_username($1)
else
raise
end
end
end
|
#destroy_user(user_id) ⇒ Object
124
125
126
|
# File 'lib/wtforum.rb', line 124
def destroy_user user_id
authorized_agent.get uri(path: "/register/delete", query: "mem_userid=#{user_id}")
end
|
#edit_user(user_id) ⇒ Object
106
107
108
|
# File 'lib/wtforum.rb', line 106
def edit_user user_id
response = authorized_agent.get uri(path: "/register/register", query: "edit=1&userid=#{user_id}")
end
|
#edit_user_email(user_id) ⇒ Object
114
115
116
|
# File 'lib/wtforum.rb', line 114
def edit_user_email user_id
authorized_agent.get uri(path: "/register/edit_password", query: "userid=#{user_id}")
end
|
#edit_user_username(user_id) ⇒ Object
110
111
112
|
# File 'lib/wtforum.rb', line 110
def edit_user_username user_id
authorized_agent.get uri(path: "/register/edit_username", query: "userid=#{user_id}")
end
|
#find_user(user_id) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/wtforum.rb', line 72
def find_user user_id
response = authorized_agent.get uri(path: "/register/register", query: "edit=1&userid=#{user_id}")
raise User::NotFound if response.body.include?("Error: The specified account was not found")
body = Nokogiri::HTML.parse(response.body)
attributes = {
id: user_id,
member: body.css(".tables td:contains('Username:') + td input").first["value"],
email: body.css(".tables td:contains('Email Address:') + td").first.text.split(" - ").first,
name: body.css(".tables td:contains('Full Name:') + td input").first["value"],
field276177: body.css(".tables select[name='field276177'] option[selected]").first.try(:text).try(:strip),
field276178: body.css(".tables input[name='field276178']").first["value"],
field276179: body.css(".tables textarea[name='field276179']").first.text
}
User.new(self, attributes)
end
|
#find_user_by_username(username) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/wtforum.rb', line 89
def find_user_by_username username
response = authorized_agent.get uri(path: "/register", query: "action=members&search=true&s_username=#{username}")
body = Nokogiri::HTML.parse(response.body)
link = body.css("a[title='View profile']:contains('#{username}')").find do |a|
a.text.strip == username
end
link or raise User::NotFound
id = link["href"].split("/").last
find_user(id)
end
|