Class: Wordpress::Client
- Inherits:
-
Object
- Object
- Wordpress::Client
- 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
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#login_url ⇒ Object
readonly
Returns the value of attribute login_url.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #blog_url ⇒ Object
-
#initialize(usr, pwd, login_url = DEFAULT_URL) ⇒ Client
constructor
A new instance of Client.
- #post(title, body, tags = nil) ⇒ Object
- #valid_login_page? ⇒ Boolean
- #valid_user? ⇒ Boolean
Constructor Details
#initialize(usr, pwd, login_url = DEFAULT_URL) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/wordpress.rb', line 23 def initialize usr, pwd, login_url = 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 login_url =~ /\/wp-login[.]php$/ @username = usr @password = pwd @login_url = login_url @agent = WWW::Mechanize.new end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
21 22 23 |
# File 'lib/wordpress.rb', line 21 def agent @agent end |
#login_url ⇒ Object (readonly)
Returns the value of attribute login_url.
21 22 23 |
# File 'lib/wordpress.rb', line 21 def login_url @login_url end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
21 22 23 |
# File 'lib/wordpress.rb', line 21 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
21 22 23 |
# File 'lib/wordpress.rb', line 21 def username @username end |
Instance Method Details
#blog_url ⇒ Object
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
50 51 52 53 54 55 56 57 |
# File 'lib/wordpress.rb', line 50 def post title, body, =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 = .join(", ") if post_form = build_post(post_form, title, body, ) post_response @agent.submit(post_form, post_form..last), title end |
#valid_login_page? ⇒ Boolean
38 39 40 |
# File 'lib/wordpress.rb', line 38 def valid_login_page? !login_page.search("form[name=#{LOGIN_FORM}]").empty? end |
#valid_user? ⇒ Boolean
42 43 44 |
# File 'lib/wordpress.rb', line 42 def valid_user? logged_into? dashboard_page end |