Class: Wordpress::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wordpress.rb

Constant Summary collapse

DEFAULT_URL =
'http://wordpress.com/wp-login.php'
LOGIN_FORM =
'loginform'
POST_FORM =
'post'
IS_ADMIN =
'body.wp-admin'
IS_LOGIN =
'body.login'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(usr, pwd, login_url = DEFAULT_URL) ⇒ Client

Returns a new instance of Client.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wordpress.rb', line 23

def initialize usr, pwd,  = DEFAULT_URL

  # should I 
  raise   AuthError, "You must provide a username and password" \
    if      usr.empty? || pwd.empty?

  raise   AuthError, "Login Url should end with wp-login.php" \
    unless   =~ /\/wp-login[.]php$/

  @username  = usr
  @password  = pwd
  @login_url = 
  @agent     = WWW::Mechanize.new
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



21
22
23
# File 'lib/wordpress.rb', line 21

def agent
  @agent
end

#login_urlObject (readonly)

Returns the value of attribute login_url.



21
22
23
# File 'lib/wordpress.rb', line 21

def 
  @login_url
end

#passwordObject (readonly)

Returns the value of attribute password.



21
22
23
# File 'lib/wordpress.rb', line 21

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



21
22
23
# File 'lib/wordpress.rb', line 21

def username
  @username
end

Instance Method Details

#blog_urlObject



46
47
48
# File 'lib/wordpress.rb', line 46

def blog_url
  dashboard_page.at("#{IS_ADMIN} #wphead h1 a")['href'] rescue nil
end

#post(title, body, tags = nil) ⇒ Object

Raises:



50
51
52
53
54
55
56
57
# File 'lib/wordpress.rb', line 50

def post title, body, tags=nil
  raise PostError, "A post requires a title or body."                       if  title.empty? && body.empty?
  post_form      = dashboard_page.form(POST_FORM)
  raise HostError, "Missing QuickPress on dashboard page or bad account."   unless  post_form
  tags           = tags.join(", ") if tags
  post_form      = build_post(post_form, title, body, tags)
  post_response    @agent.submit(post_form, post_form.buttons.last), title
end

#valid_login_page?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/wordpress.rb', line 38

def 
  !.search("form[name=#{LOGIN_FORM}]").empty?
end

#valid_user?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/wordpress.rb', line 42

def valid_user?
  logged_into? dashboard_page
end