wordpress 0.1.5 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.txt +14 -16
  2. data/lib/wordpress.rb +22 -26
  3. data/test/test_wordpress.rb +35 -68
  4. metadata +2 -2
data/README.txt CHANGED
@@ -24,41 +24,39 @@ Posting images with posts, posting only images and pulling down your posts will
24
24
 
25
25
  * Provide just the username and password as strings
26
26
 
27
- user_post = Wordpress::Post.new('username', 'password')
27
+ account = Wordpress::Client.new('username', 'password')
28
28
 
29
29
  * OR Provide the url of your login page if it's self hosted
30
30
 
31
- user_post = Wordpress::Post.new('username', 'password', 'http://blog.mysite.com/wp-login.php')
31
+ account = Wordpress::Client.new('username', 'password', 'http://blog.mysite.com/wp-login.php')
32
32
 
33
33
  2. Validate the provided account info and request the users blog url homepage if needed
34
34
 
35
35
  * Check if the user is valid. Returns true or false
36
36
 
37
- user_post.valid_user?
37
+ account.valid_user?
38
38
 
39
39
  * Check if the specified login page is valid. Returns true or false
40
40
 
41
- user_post.valid_login_page?
41
+ account.valid_login_page?
42
42
 
43
43
  * Get the users blog page url. Returns nil if it can't be found.
44
44
 
45
- user_post.blog_url
45
+ account.blog_url
46
46
 
47
- 3. Setup your post
47
+ 3. Send your post to your Wordpress Blog
48
48
 
49
- * You must at least include the title or body. Tags must be an array.
49
+ * Set to a variable to work with the response
50
50
 
51
- user_post.title = "My Title"
52
- user_post.body = "My Body Text"
53
- user_post.tags = ["Glue", "Wordpress", "Ruby", "Made By Squad"]
51
+ response = account.post("My title", "Body Text", ["Glue", "Wordpress"])
52
+
53
+ response = account.post("", "Body Text", ["Glue", "Wordpress"])
54
+
55
+ response = account.post("My title", "")
54
56
 
55
- 4. Update your Wordpress Blog with your post
57
+ response = account.post("", "Body Text")
56
58
 
57
- * Set this to a variable to work with the response
58
-
59
- response = user_post.submit
60
-
61
- 5. You get a success or error hash back
59
+ 4. You get a success or error hash back
62
60
 
63
61
  * Your response should look something like this if successful
64
62
 
@@ -3,14 +3,14 @@ require 'mechanize'
3
3
 
4
4
  module Wordpress
5
5
 
6
- VERSION = '0.1.5'
6
+ VERSION = '0.1.7'
7
7
 
8
8
  class AuthError < StandardError; end
9
9
  class PostError < StandardError; end
10
10
  class HostError < StandardError; end
11
11
  class TagsError < StandardError; end
12
12
 
13
- class Post
13
+ class Client
14
14
 
15
15
  DEFAULT_URL = 'http://wordpress.com/wp-login.php'
16
16
  LOGIN_FORM = 'loginform'
@@ -18,26 +18,21 @@ module Wordpress
18
18
  IS_ADMIN = 'body.wp-admin'
19
19
  IS_LOGIN = 'body.login'
20
20
 
21
- attr_accessor :title, :body
22
- attr_reader :login_url, :username, :password, :tags, :post_url, :agent
21
+ attr_reader :login_url, :username, :password, :agent
23
22
 
24
23
  def initialize usr, pwd, login_url = DEFAULT_URL
25
- raise AuthError, "Blank Username or Password or not a string." \
26
- unless usr.is_a?(String) && pwd.is_a?(String) && !usr.empty? && !pwd.empty?
24
+
25
+ # should I
26
+ raise AuthError, "You must provide a username and password" \
27
+ if usr.empty? || pwd.empty?
27
28
 
28
- raise AuthError, "Url should end with wp-login.php" \
29
+ raise AuthError, "Login Url should end with wp-login.php" \
29
30
  unless login_url =~ /\/wp-login[.]php$/
30
31
 
31
32
  @username = usr
32
33
  @password = pwd
33
34
  @login_url = login_url
34
35
  @agent = WWW::Mechanize.new
35
- @post_url = @tags = @title = @body = nil
36
- end
37
-
38
- def tags= ary
39
- raise TagsError, 'Tags must added using an array' unless ary.is_a?(Array)
40
- @tags = ary.join(", ")
41
36
  end
42
37
 
43
38
  def valid_login_page?
@@ -49,15 +44,16 @@ module Wordpress
49
44
  end
50
45
 
51
46
  def blog_url
52
- dashboard_page.search("#{IS_ADMIN} #wphead h1 a").first['href'] rescue nil
47
+ dashboard_page.at("#{IS_ADMIN} #wphead h1 a")['href'] rescue nil
53
48
  end
54
49
 
55
- def submit
56
- raise PostError, "A post requires a title or body." unless @title || @body
50
+ def post title, body, tags=nil
51
+ raise PostError, "A post requires a title or body." if title.empty? && body.empty?
57
52
  post_form = dashboard_page.form(POST_FORM)
58
- raise HostError, "Missing QuickPress on dashboard page or bad account." unless post_form
59
- post_form = build_post(post_form)
60
- post_response @agent.submit(post_form, post_form.buttons.last)
53
+ raise HostError, "Missing QuickPress on dashboard page or bad account." unless post_form
54
+ tags = tags.join(", ") if tags
55
+ post_form = build_post(post_form, title, body, tags)
56
+ post_response @agent.submit(post_form, post_form.buttons.last), title
61
57
  end
62
58
 
63
59
  private
@@ -81,23 +77,23 @@ module Wordpress
81
77
  !page.search(IS_ADMIN).empty?
82
78
  end
83
79
 
84
- def build_post form
85
- form.post_title = @title
86
- form.content = @body
87
- form.tags_input = @tags
80
+ def build_post form, title, body, tags
81
+ form.post_title = title
82
+ form.content = body
83
+ form.tags_input = tags
88
84
  form
89
85
  end
90
86
 
91
- def post_response page
87
+ def post_response page, title
92
88
  links = page.search("div.message p a")
93
89
  if links.first && links.last
94
90
  url = links.first['href'] ? links.first['href'].gsub("?preview=1", "") : nil
95
91
  pid = links.last['href'] ? links.last['href'].sub(/.*post=(\d*)/,'\1') : nil
96
92
  if pid && url
97
- return { "rsp" => { "post" => { "title" => "#{@title}", "url" => "#{url}", "id" => "#{pid}" }, "stat" => "ok" }}
93
+ return {"rsp" => {"post" => {"title" => "#{title}", "url" => "#{url}", "id" => "#{pid}"}, "stat" => "ok" }}
98
94
  end
99
95
  end
100
- { "rsp" => { "err" => { "msg" => "Post was unsuccessful.", "title" => "#{@title}" }, "stat" => "fail" } }
96
+ {"rsp" => {"err" => {"msg" => "Post was unsuccessful.", "title" => "#{title}"}, "stat" => "fail"}}
101
97
  end
102
98
  end
103
99
  end
@@ -2,13 +2,17 @@ require "test/unit"
2
2
  require "wordpress"
3
3
  require "mocha"
4
4
 
5
- class Wordpress::Post
5
+ class Wordpress::Client
6
6
  public :login_page, :dashboard_page, :logged_into?, :build_post, :post_response
7
7
  end
8
8
 
9
- # Need to test these methods
9
+ # Should Test The Following
10
10
  # * build_post
11
11
  # * dashboard_page
12
+ # * Post Response failure
13
+ # * Tags error without being an array
14
+ # * Body and tags are returned in response
15
+ # * Testing that username, password & login url respond to empty
12
16
 
13
17
  class TestWordpress < Test::Unit::TestCase
14
18
 
@@ -16,10 +20,10 @@ class TestWordpress < Test::Unit::TestCase
16
20
  @u = 'jordandobson'
17
21
  @p = 'password'
18
22
 
19
- @account = Wordpress::Post.new @u, @p
20
- @account_bad = Wordpress::Post.new @u, 'x'
21
- @account_invalid_login_page = Wordpress::Post.new @u, @p, 'http://notapage.gd/wp-login.php'
22
- @account_hosted_account = Wordpress::Post.new @u, @p, 'http://blog.getglue.net/wp-login.php'
23
+ @account = Wordpress::Client.new @u, @p
24
+ @account_bad = Wordpress::Client.new @u, 'x'
25
+ @account_invalid_login_page = Wordpress::Client.new @u, @p, 'http://notapage.gd/wp-login.php'
26
+ @account_hosted_account = Wordpress::Client.new @u, @p, 'http://blog.getglue.net/wp-login.php'
23
27
 
24
28
  login_html = '<html><body class="login"><form name="loginform"></form></body></html>'
25
29
  admin_html = '<html><body class="wp-admin"><div id="wphead"><h1><a href="http://getglue.wordpress.com/" title="Visit Site">Get Glue</a></h1></div><form name="post"><input type="text" name="post_title"/><textarea name="content"></textarea><input type="text" name="tags_input"/><input type="submit" name="publish" /></form></body></html>'
@@ -37,50 +41,38 @@ class TestWordpress < Test::Unit::TestCase
37
41
  end
38
42
 
39
43
  def test_sets_account_info_on_initialize
40
- actual = Wordpress::Post.new @u, @p
44
+ actual = Wordpress::Client.new @u, @p
41
45
  assert_equal [@u, @p], [actual.username, actual.password]
42
46
  end
43
47
 
44
48
  def test_raises_if_username_is_blank
45
49
  assert_raise Wordpress::AuthError do
46
- Wordpress::Post.new "", @p
50
+ Wordpress::Client.new "", @p
47
51
  end
48
52
  end
49
53
 
50
54
  def test_raises_if_password_is_blank
51
55
  assert_raise Wordpress::AuthError do
52
- Wordpress::Post.new @u, ""
53
- end
54
- end
55
-
56
- def test_raises_if_password_is_not_srting
57
- assert_raise Wordpress::AuthError do
58
- Wordpress::Post.new @u, 00
59
- end
60
- end
61
-
62
- def test_raises_if_username_is_not_srting
63
- assert_raise Wordpress::AuthError do
64
- Wordpress::Post.new 00, @p
56
+ Wordpress::Client.new @u, ""
65
57
  end
66
58
  end
67
59
 
68
60
  def test_login_url_uses_default_if_witheld
69
- assert_equal Wordpress::Post::DEFAULT_URL, @account.login_url
61
+ assert_equal Wordpress::Client::DEFAULT_URL, @account.login_url
70
62
  end
71
-
63
+
72
64
  def test_users_url_does_not_raise
73
65
  assert_equal 'http://notapage.gd/wp-login.php', @account_invalid_login_page.login_url
74
66
  end
75
67
 
76
68
  def test_raises_on_bad_login_url
77
69
  assert_raise Wordpress::AuthError do
78
- Wordpress::Post.new @u, @p, 'http://bad.login/url.php'
70
+ Wordpress::Client.new @u, @p, 'http://bad.login/url.php'
79
71
  end
80
72
  end
81
73
 
82
74
  def test_login_page_is_valid
83
- actual = Wordpress::Post.new @u, @p
75
+ actual = Wordpress::Client.new @u, @p
84
76
  actual.stubs(:login_page).returns(@login_pg)
85
77
  assert_equal true, actual.valid_login_page?
86
78
  end
@@ -123,82 +115,57 @@ class TestWordpress < Test::Unit::TestCase
123
115
  assert_nil @account_invalid_login_page.blog_url
124
116
  end
125
117
 
126
- def test_update_raises_without_title_or_body
118
+ def test_post_raises_without_title_or_body
127
119
  assert_raise Wordpress::PostError do
128
- @account.submit
120
+ @account.post("", "")
129
121
  end
130
122
  end
131
123
 
132
- def test_update_raises_without_post_form
124
+ def test_post_raises_without_post_form
133
125
  @account_bad.stubs(:dashboard_page).returns(@fail_pg)
134
- @account_bad.title = "Fail"
135
126
  assert_raise Wordpress::HostError do
136
- @account_bad.submit
137
- end
138
- end
139
-
140
- def test_tags_are_added_correctly
141
- @account.tags = []
142
- assert_equal @account.tags, ""
143
- end
144
-
145
- def test_tags_single_is_correctly
146
- @account.tags = ["Glue"]
147
- assert_equal @account.tags, "Glue"
148
- end
149
-
150
- def test_tags_multiple_is_correctly_joined
151
- @account.tags = ["Glue", "Wordpress", "Ruby on Rails"]
152
- assert_equal @account.tags, "Glue, Wordpress, Ruby on Rails"
153
- end
154
-
155
- def test_raises_if_tags_not_set_as_array
156
- assert_raise Wordpress::TagsError do
157
- @account.tags = "hello, "
127
+ @account_bad.post("My Title", "")
158
128
  end
159
129
  end
160
130
 
161
131
  def test_post_response_returns_good_response
162
- assert_equal "ok", @account.post_response(@success_pg)["rsp"]["stat"]
132
+ assert_equal "ok", @account.post_response(@success_pg, "")["rsp"]["stat"]
163
133
  end
164
134
 
165
- def test_update_returns_fail
135
+ def test_post_returns_fail
166
136
  title = "My Title"
167
- @account.title = title
168
- res = @account.post_response(@fail_pg)
169
- assert_equal "fail", res["rsp"]["stat"]
170
- assert_equal "Post was unsuccessful.", res["rsp"]["err"]["msg"]
171
- assert_equal title, res["rsp"]["err"]["title"]
137
+ res = @account.post_response(@fail_pg, title)
138
+ assert_equal "fail", res["rsp"]["stat"]
139
+ assert_equal "Post was unsuccessful.", res["rsp"]["err"]["msg"]
140
+ assert_equal title, res["rsp"]["err"]["title"]
172
141
  end
173
142
 
174
- def test_update_returns_ok
143
+ def test_post_returns_ok
175
144
  @account.stubs(:dashboard_page).returns(@admin_pg)
176
145
  @account.agent.stubs(:submit).returns(@success_pg)
177
146
  title = "My Title"
178
- @account.title = title
179
- @account.body = "Body Text ..."
180
- actual = @account.submit
147
+ body = "Body Text"
148
+ actual = @account.post(title, body)
181
149
  assert_equal "ok", actual["rsp"]["stat"]
182
150
  assert_equal title, actual["rsp"]["post"]["title"]
183
151
  assert_equal "99", actual["rsp"]["post"]["id"]
184
152
  assert_equal "http://success.com/2009/", actual["rsp"]["post"]["url"]
185
153
  end
186
154
 
187
- def test_update_returns_ok_with_only_title
155
+ def test_post_returns_ok_with_only_title
188
156
  @account.stubs(:dashboard_page).returns(@admin_pg)
189
157
  @account.agent.stubs(:submit).returns(@success_pg)
190
158
  title = "My Title"
191
- @account.title = "My Title"
192
- actual = @account.submit
159
+ actual = @account.post(title, "")
193
160
  assert_equal "ok", actual["rsp"]["stat"]
194
161
  assert_equal title, actual["rsp"]["post"]["title"]
195
162
  end
196
163
 
197
- def test_update_returns_ok_with_only_body
164
+ def test_post_returns_ok_with_only_body
198
165
  @account.stubs(:dashboard_page).returns(@admin_pg)
199
166
  @account.agent.stubs(:submit).returns(@success_pg)
200
- @account.body = "Body Text ..."
201
- actual = @account.submit
167
+ body = "Body Text"
168
+ actual = @account.post("", body)
202
169
  assert_equal "ok", actual["rsp"]["stat"]
203
170
  assert_equal "", actual["rsp"]["post"]["title"]
204
171
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordpress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Dobson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-09 00:00:00 -07:00
12
+ date: 2009-06-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency