Class: Wordpress::Post

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) ⇒ Post

Returns a new instance of Post.

Raises:



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

def initialize usr, pwd,  = DEFAULT_URL
  raise   AuthError, "Blank Username or Password or not a string." \
    unless  usr.is_a?(String) && pwd.is_a?(String) && !usr.empty? && !pwd.empty?

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

  @username  = usr
  @password  = pwd
  @login_url = 
  @agent     = WWW::Mechanize.new
  @post_url  = @tags = @title = @body = nil
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



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

def agent
  @agent
end

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#login_urlObject (readonly)

Returns the value of attribute login_url.



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

def 
  @login_url
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#post_urlObject (readonly)

Returns the value of attribute post_url.



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

def post_url
  @post_url
end

#tagsObject

Returns the value of attribute tags.



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

def tags
  @tags
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#blog_urlObject



51
52
53
# File 'lib/wordpress.rb', line 51

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

#submitObject

Raises:



55
56
57
58
59
60
61
# File 'lib/wordpress.rb', line 55

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

#valid_login_page?Boolean

Returns:

  • (Boolean)


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

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

#valid_user?Boolean

Returns:

  • (Boolean)


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

def valid_user?
  logged_into? dashboard_page
end