Class: WTForum

Inherits:
Object
  • Object
show all
Defined in:
lib/wtforum.rb,
lib/wtforum/user.rb,
lib/wtforum/admin.rb,
lib/wtforum/session.rb,
lib/wtforum/version.rb

Defined Under Namespace

Classes: Admin, Session, User, WTForumError

Constant Summary collapse

VERSION =
"0.8.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ WTForum

Returns a new instance of WTForum.



25
26
27
28
29
# File 'lib/wtforum.rb', line 25

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.



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

def admin_password
  @admin_password
end

#admin_usernameObject

Returns the value of attribute admin_username.



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

def admin_username
  @admin_username
end

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#domainObject

Returns the value of attribute domain.



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

def domain
  @domain
end

Class Method Details

.extract_value(key, options) ⇒ Object



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

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

#adminObject



2
3
4
# File 'lib/wtforum/admin.rb', line 2

def admin
  Admin.new(username: admin_username, password: admin_password)
end

#count_usersObject



123
124
125
126
127
# File 'lib/wtforum.rb', line 123

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



31
32
33
34
35
36
# File 'lib/wtforum.rb', line 31

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



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

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_and_email($1, attributes[:email])
    else
      raise
    end
  end
end

#destroy_user(user_id) ⇒ Object



129
130
131
# File 'lib/wtforum.rb', line 129

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

#edit_user(user_id) ⇒ Object



111
112
113
# File 'lib/wtforum.rb', line 111

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



119
120
121
# File 'lib/wtforum.rb', line 119

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



115
116
117
# File 'lib/wtforum.rb', line 115

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:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wtforum.rb', line 73

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.try(:[], "value"),
    field276179: body.css(".tables textarea[name='field276179']").first.try(:text)
  }
  User.new(self, attributes)
end

#find_user_by_username(username, email = nil) ⇒ Object Also known as: find_user_by_username_and_email



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/wtforum.rb', line 90

def find_user_by_username username, email=nil
  query = "action=members&search=true&s_username=#{username}"
  query += "&s_email=#{email}" if email
  response = authorized_agent.get uri(path: "/register", query: query)
  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