wordpress 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,24 @@
1
- === 1.0.3 / 2009-06-05
1
+ === 0.1.5 / 2009-06-09
2
+
3
+ * 3 major enhancements
4
+
5
+ * Refactored code based on a few suggestions from Ryan Davis
6
+ * Changed Wordpress::Client to Wordpress::Post and updated tests accordingly
7
+ * Added tests for tags method
8
+
9
+ === 0.1.3 / 2009-06-05
2
10
 
3
11
  * 1 major enhancement
4
12
 
5
13
  * Stubbed test and refactored code
6
14
 
7
- === 1.0.1 / 2009-06-05
15
+ === 0.1.1 / 2009-06-05
8
16
 
9
17
  * 1 major enhancement
10
18
 
11
19
  * Added the ability to post and check account properties
12
20
 
13
- === 1.0.0 / 2009-05-28
21
+ === 0.0.1 / 2009-05-28
14
22
 
15
23
  * 1 major enhancement
16
24
 
data/README.txt CHANGED
@@ -20,63 +20,62 @@ Posting images with posts, posting only images and pulling down your posts will
20
20
 
21
21
  == SYNOPSIS:
22
22
 
23
- 1. Instantiate your account
23
+ 1. Instantiate your post with your account info
24
24
 
25
- * You can provide just the username and password
25
+ * Provide just the username and password as strings
26
26
 
27
- account = Wordpress::Client.new('username', 'password')
27
+ user_post = Wordpress::Post.new('username', 'password')
28
28
 
29
- * Or you can provide the ID as a string or integer
29
+ * OR Provide the url of your login page if it's self hosted
30
30
 
31
- account = Wordpress::Client.new('username', 'password', 'http://blog.mysite.com/wp-login.php')
31
+ user_post = Wordpress::Post.new('username', 'password', 'http://blog.mysite.com/wp-login.php')
32
32
 
33
- 2. Get more info about the user's account if you need it
33
+ 2. Validate the provided account info and request the users blog url homepage if needed
34
34
 
35
- * Check if the user is valid
35
+ * Check if the user is valid. Returns true or false
36
36
 
37
- account.valid_user?
37
+ user_post.valid_user?
38
38
 
39
- * Check if the specified login page is valid
39
+ * Check if the specified login page is valid. Returns true or false
40
40
 
41
- account.valid_login_page?
41
+ user_post.valid_login_page?
42
42
 
43
- * Get the users blog page url
43
+ * Get the users blog page url. Returns nil if it can't be found.
44
44
 
45
- account.blog_url
46
-
47
- * Get a list of your sites and additional info
48
-
49
- account.account_info
45
+ user_post.blog_url
50
46
 
51
47
  3. Setup your post
52
48
 
53
- * You must at least include the title or body
49
+ * You must at least include the title or body. Tags must be an array.
54
50
 
55
- account.title = "My Title"
56
- account.body = "My Body Text"
57
- account.tags = ["Glue", "Posterous", "Ruby", "Made By Squad"]
51
+ user_post.title = "My Title"
52
+ user_post.body = "My Body Text"
53
+ user_post.tags = ["Glue", "Wordpress", "Ruby", "Made By Squad"]
58
54
 
59
- 4. Add your post to Posterous.com
55
+ 4. Update your Wordpress Blog with your post
60
56
 
61
57
  * Set this to a variable to work with the response
62
58
 
63
- response = account.add_post
59
+ response = user_post.submit
64
60
 
65
- 5. You get a success or error hash back or nil
61
+ 5. You get a success or error hash back
66
62
 
67
63
  * Your response should look something like this if successful
68
64
 
69
- response #=> { "rsp" => { "post" => { "title" => "My Title", "url" => "http://getglue.wordpress.com/2009/06/06/my-title/", "id" => "69" }, "stat" => "ok" } }
65
+ response #=> { "rsp" => { "post" => { "title" => "My Title", "url" => "http://getglue.wordpress.com/2009/06/06/my-title/", "id" => "69" }, "stat" => "ok" } }
66
+
67
+ * Your response would look like this if it failed
68
+
69
+ response #=> { "rsp" => { "err" => { "msg" => "Post was unsuccessful.", "title" => "My Title" }, "stat" => "fail" } }
70
70
 
71
- * See the tests for this gem for failure responses and responses for other methods
72
71
 
73
72
  == REQUIREMENTS:
74
73
 
75
- * mechanize, & Mocha (For Tests)
74
+ * mechanize, & Mocha (For Testing)
76
75
 
77
76
  == INSTALL:
78
77
 
79
- * sudo gem install wordpress -include-dependencies
78
+ * sudo gem install wordpress
80
79
 
81
80
  == LICENSE:
82
81
 
data/Rakefile CHANGED
@@ -5,9 +5,9 @@ require 'hoe'
5
5
  require './lib/wordpress.rb'
6
6
 
7
7
  Hoe.new('wordpress', Wordpress::VERSION) do |p|
8
- # p.rubyforge_name = 'wordpressx' # if different than lowercase project name
9
8
  p.developer('Jordan Dobson', 'jordan.dobson@madebysquad.com')
10
- p.extra_deps = ['mechanize', 'mocha']
9
+ p.extra_deps = ['mechanize']
10
+ p.extra_dev_deps = ['mocha']
11
11
  end
12
12
 
13
13
  # vim: syntax=Ruby
@@ -3,13 +3,14 @@ require 'mechanize'
3
3
 
4
4
  module Wordpress
5
5
 
6
- VERSION = '0.1.4'
6
+ VERSION = '0.1.5'
7
7
 
8
8
  class AuthError < StandardError; end
9
9
  class PostError < StandardError; end
10
10
  class HostError < StandardError; end
11
+ class TagsError < StandardError; end
11
12
 
12
- class Client
13
+ class Post
13
14
 
14
15
  DEFAULT_URL = 'http://wordpress.com/wp-login.php'
15
16
  LOGIN_FORM = 'loginform'
@@ -22,7 +23,7 @@ module Wordpress
22
23
 
23
24
  def initialize usr, pwd, login_url = DEFAULT_URL
24
25
  raise AuthError, "Blank Username or Password or not a string." \
25
- if !usr.is_a?(String) || !pwd.is_a?(String) || usr == '' || pwd == ''
26
+ unless usr.is_a?(String) && pwd.is_a?(String) && !usr.empty? && !pwd.empty?
26
27
 
27
28
  raise AuthError, "Url should end with wp-login.php" \
28
29
  unless login_url =~ /\/wp-login[.]php$/
@@ -35,7 +36,7 @@ module Wordpress
35
36
  end
36
37
 
37
38
  def tags= ary
38
- raise TagError, 'Tags must added using an array' if !ary.is_a?(Array)
39
+ raise TagsError, 'Tags must added using an array' unless ary.is_a?(Array)
39
40
  @tags = ary.join(", ")
40
41
  end
41
42
 
@@ -48,14 +49,10 @@ module Wordpress
48
49
  end
49
50
 
50
51
  def blog_url
51
- begin
52
- a = dashboard_page.search("#{IS_ADMIN} #wphead h1 a")
53
- rescue SocketError
54
- end
55
- a && a.first && a.first['href'] ? a.first['href'] : nil
52
+ dashboard_page.search("#{IS_ADMIN} #wphead h1 a").first['href'] rescue nil
56
53
  end
57
54
 
58
- def add_post
55
+ def submit
59
56
  raise PostError, "A post requires a title or body." unless @title || @body
60
57
  post_form = dashboard_page.form(POST_FORM)
61
58
  raise HostError, "Missing QuickPress on dashboard page or bad account." unless post_form
@@ -77,7 +74,6 @@ module Wordpress
77
74
  login_form.pwd = @password
78
75
  page = @agent.submit login_form
79
76
  end
80
- puts page.inspect
81
77
  page
82
78
  end
83
79
 
@@ -85,23 +81,23 @@ module Wordpress
85
81
  !page.search(IS_ADMIN).empty?
86
82
  end
87
83
 
88
- def build_post f
89
- f.post_title = @title
90
- f.content = @body
91
- f.tags_input = @tags
92
- f
84
+ def build_post form
85
+ form.post_title = @title
86
+ form.content = @body
87
+ form.tags_input = @tags
88
+ form
93
89
  end
94
90
 
95
91
  def post_response page
96
- a = page.search("div.message p a")
97
- if a && a.first && a.last
98
- url = a.first['href'] ? a.first['href'].gsub("?preview=1", "") : nil
99
- pid = a.last['href'] ? a.last['href'].sub(/.*post=(\d*)/,'\1') : nil
92
+ links = page.search("div.message p a")
93
+ if links.first && links.last
94
+ url = links.first['href'] ? links.first['href'].gsub("?preview=1", "") : nil
95
+ pid = links.last['href'] ? links.last['href'].sub(/.*post=(\d*)/,'\1') : nil
100
96
  if pid && url
101
97
  return { "rsp" => { "post" => { "title" => "#{@title}", "url" => "#{url}", "id" => "#{pid}" }, "stat" => "ok" }}
102
98
  end
103
99
  end
104
- { "rsp" => { "err" => { "msg" => "Post was unsuccessful.", "title" => "#{@title}" }, "stat" => "fail" }}
100
+ { "rsp" => { "err" => { "msg" => "Post was unsuccessful.", "title" => "#{@title}" }, "stat" => "fail" } }
105
101
  end
106
102
  end
107
103
  end
@@ -2,27 +2,24 @@ require "test/unit"
2
2
  require "wordpress"
3
3
  require "mocha"
4
4
 
5
- ######
6
- # USED TO TEST PRIVATE METHODS
7
- class Class
8
- def private_methods
9
- m = self.private_instance_methods
10
- self.class_eval { public( *m ) }
11
- yield
12
- self.class_eval { private( *m ) }
13
- end
5
+ class Wordpress::Post
6
+ public :login_page, :dashboard_page, :logged_into?, :build_post, :post_response
14
7
  end
15
8
 
9
+ # Need to test these methods
10
+ # * build_post
11
+ # * dashboard_page
12
+
16
13
  class TestWordpress < Test::Unit::TestCase
17
14
 
18
15
  def setup
19
16
  @u = 'jordandobson'
20
17
  @p = 'password'
21
18
 
22
- @account = Wordpress::Client.new @u, @p
23
- @account_bad = Wordpress::Client.new @u, 'x'
24
- @account_invalid_login_page = Wordpress::Client.new @u, @p, 'http://notapage.gd/wp-login.php'
25
- @account_hosted_account = Wordpress::Client.new @u, @p, 'http://blog.getglue.net/wp-login.php'
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'
26
23
 
27
24
  login_html = '<html><body class="login"><form name="loginform"></form></body></html>'
28
25
  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>'
@@ -33,7 +30,6 @@ class TestWordpress < Test::Unit::TestCase
33
30
  @admin_pg = setup_mock_mechanize_pg admin_html
34
31
  @success_pg = setup_mock_mechanize_pg success_html
35
32
  @fail_pg = setup_mock_mechanize_pg fail_html
36
-
37
33
  end
38
34
 
39
35
  def setup_mock_mechanize_pg html
@@ -41,36 +37,36 @@ class TestWordpress < Test::Unit::TestCase
41
37
  end
42
38
 
43
39
  def test_sets_account_info_on_initialize
44
- actual = Wordpress::Client.new @u, @p
40
+ actual = Wordpress::Post.new @u, @p
45
41
  assert_equal [@u, @p], [actual.username, actual.password]
46
42
  end
47
43
 
48
44
  def test_raises_if_username_is_blank
49
45
  assert_raise Wordpress::AuthError do
50
- Wordpress::Client.new "", @p
46
+ Wordpress::Post.new "", @p
51
47
  end
52
48
  end
53
49
 
54
50
  def test_raises_if_password_is_blank
55
51
  assert_raise Wordpress::AuthError do
56
- Wordpress::Client.new @u, ""
52
+ Wordpress::Post.new @u, ""
57
53
  end
58
54
  end
59
55
 
60
56
  def test_raises_if_password_is_not_srting
61
57
  assert_raise Wordpress::AuthError do
62
- Wordpress::Client.new @u, 00
58
+ Wordpress::Post.new @u, 00
63
59
  end
64
60
  end
65
61
 
66
62
  def test_raises_if_username_is_not_srting
67
63
  assert_raise Wordpress::AuthError do
68
- Wordpress::Client.new 00, @p
64
+ Wordpress::Post.new 00, @p
69
65
  end
70
66
  end
71
67
 
72
68
  def test_login_url_uses_default_if_witheld
73
- assert_equal Wordpress::Client::DEFAULT_URL, @account.login_url
69
+ assert_equal Wordpress::Post::DEFAULT_URL, @account.login_url
74
70
  end
75
71
 
76
72
  def test_users_url_does_not_raise
@@ -79,12 +75,12 @@ class TestWordpress < Test::Unit::TestCase
79
75
 
80
76
  def test_raises_on_bad_login_url
81
77
  assert_raise Wordpress::AuthError do
82
- Wordpress::Client.new @u, @p, 'http://bad.login/url.php'
78
+ Wordpress::Post.new @u, @p, 'http://bad.login/url.php'
83
79
  end
84
80
  end
85
81
 
86
82
  def test_login_page_is_valid
87
- actual = Wordpress::Client.new @u, @p
83
+ actual = Wordpress::Post.new @u, @p
88
84
  actual.stubs(:login_page).returns(@login_pg)
89
85
  assert_equal true, actual.valid_login_page?
90
86
  end
@@ -110,15 +106,11 @@ class TestWordpress < Test::Unit::TestCase
110
106
  end
111
107
 
112
108
  def test_private_logged_in_is_true
113
- Wordpress::Client.private_methods {
114
- assert_equal true, @account.logged_into?(@admin_pg)
115
- }
109
+ assert_equal true, @account.logged_into?(@admin_pg)
116
110
  end
117
111
 
118
112
  def test_private_logged_in_is_false
119
- Wordpress::Client.private_methods {
120
- assert_equal false, @account.logged_into?(@login_pg)
121
- }
113
+ assert_equal false, @account.logged_into?(@login_pg)
122
114
  end
123
115
 
124
116
  def test_returns_blog_url
@@ -131,65 +123,82 @@ class TestWordpress < Test::Unit::TestCase
131
123
  assert_nil @account_invalid_login_page.blog_url
132
124
  end
133
125
 
134
- def test_add_post_raises_without_title_or_body
126
+ def test_update_raises_without_title_or_body
135
127
  assert_raise Wordpress::PostError do
136
- @account.add_post
128
+ @account.submit
137
129
  end
138
130
  end
139
131
 
140
- def test_add_post_raises_without_post_form
132
+ def test_update_raises_without_post_form
141
133
  @account_bad.stubs(:dashboard_page).returns(@fail_pg)
142
134
  @account_bad.title = "Fail"
143
135
  assert_raise Wordpress::HostError do
144
- @account_bad.add_post
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, "
145
158
  end
146
159
  end
147
160
 
148
161
  def test_post_response_returns_good_response
149
- Wordpress::Client.private_methods {
150
- assert_equal "ok", @account.post_response(@success_pg)["rsp"]["stat"]
151
- }
162
+ assert_equal "ok", @account.post_response(@success_pg)["rsp"]["stat"]
152
163
  end
153
164
 
154
- def test_add_post_returns_fail
155
- Wordpress::Client.private_methods {
156
- title = "My Title"
157
- @account.title = title
158
- res = @account.post_response(@fail_pg)
159
- assert_equal "fail", res["rsp"]["stat"]
160
- assert_equal "Post was unsuccessful.", res["rsp"]["err"]["msg"]
161
- assert_equal title, res["rsp"]["err"]["title"]
162
- }
165
+ def test_update_returns_fail
166
+ 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"]
163
172
  end
164
173
 
165
- def test_add_post_returns_ok
174
+ def test_update_returns_ok
166
175
  @account.stubs(:dashboard_page).returns(@admin_pg)
167
176
  @account.agent.stubs(:submit).returns(@success_pg)
168
177
  title = "My Title"
169
178
  @account.title = title
170
179
  @account.body = "Body Text ..."
171
- actual = @account.add_post
180
+ actual = @account.submit
172
181
  assert_equal "ok", actual["rsp"]["stat"]
173
182
  assert_equal title, actual["rsp"]["post"]["title"]
174
183
  assert_equal "99", actual["rsp"]["post"]["id"]
175
184
  assert_equal "http://success.com/2009/", actual["rsp"]["post"]["url"]
176
185
  end
177
186
 
178
- def test_add_post_returns_ok_with_only_title
187
+ def test_update_returns_ok_with_only_title
179
188
  @account.stubs(:dashboard_page).returns(@admin_pg)
180
189
  @account.agent.stubs(:submit).returns(@success_pg)
181
190
  title = "My Title"
182
191
  @account.title = "My Title"
183
- actual = @account.add_post
192
+ actual = @account.submit
184
193
  assert_equal "ok", actual["rsp"]["stat"]
185
194
  assert_equal title, actual["rsp"]["post"]["title"]
186
195
  end
187
196
 
188
- def test_add_post_returns_ok_with_only_body
197
+ def test_update_returns_ok_with_only_body
189
198
  @account.stubs(:dashboard_page).returns(@admin_pg)
190
199
  @account.agent.stubs(:submit).returns(@success_pg)
191
200
  @account.body = "Body Text ..."
192
- actual = @account.add_post
201
+ actual = @account.submit
193
202
  assert_equal "ok", actual["rsp"]["stat"]
194
203
  assert_equal "", actual["rsp"]["post"]["title"]
195
204
  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.4
4
+ version: 0.1.5
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-08 00:00:00 -07:00
12
+ date: 2009-06-09 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -24,7 +24,7 @@ dependencies:
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- type: :runtime
27
+ type: :development
28
28
  version_requirement:
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements: