Class: WTForum

Inherits:
Object
  • 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.4.1"

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_passwordObject

Returns the value of attribute admin_password.



22
23
24
# File 'lib/wtforum.rb', line 22

def admin_password
  @admin_password
end

#admin_usernameObject

Returns the value of attribute admin_username.



22
23
24
# File 'lib/wtforum.rb', line 22

def admin_username
  @admin_username
end

#api_keyObject

Returns the value of attribute api_key.



22
23
24
# File 'lib/wtforum.rb', line 22

def api_key
  @api_key
end

#domainObject

Returns the value of attribute domain.



22
23
24
# File 'lib/wtforum.rb', line 22

def domain
  @domain
end

Class Method Details

.extract_value(key, options) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/wtforum.rb', line 12

def self.extract_value 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_usersObject



97
98
99
100
101
# File 'lib/wtforum.rb', line 97

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
# File 'lib/wtforum.rb', line 37

def create_user attributes
  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

  uri = base_api_uri(attributes)
  uri.path = "/register/create_account"
  response = agent.get uri
  User.create self, response, attributes
end

#destroy_user(user_id) ⇒ Object



103
104
105
# File 'lib/wtforum.rb', line 103

def destroy_user user_id
  authorized_agent.get uri(path: "/register/delete", query: "mem_userid=#{user_id}")
end

#edit_user(user_id) ⇒ Object



85
86
87
# File 'lib/wtforum.rb', line 85

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



93
94
95
# File 'lib/wtforum.rb', line 93

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



89
90
91
# File 'lib/wtforum.rb', line 89

def edit_user_username user_id
  authorized_agent.get uri(path: "/register/edit_username", query: "userid=#{user_id}")
end

#find_user(user_id) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wtforum.rb', line 51

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wtforum.rb', line 68

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)

  # scrape markup: <a href="/profile/1234567" title="View profile">username\t\n</a>
  # search returns partial matches :( so find the exact match.
  # hopefully there aren't more than 50 matches!
  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