wordpress 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +11 -3
- data/README.txt +26 -27
- data/Rakefile +2 -2
- data/lib/wordpress.rb +17 -21
- data/test/test_wordpress.rb +59 -50
- metadata +3 -3
data/History.txt
CHANGED
@@ -1,16 +1,24 @@
|
|
1
|
-
=== 1.
|
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
|
-
===
|
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
|
-
===
|
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
|
-
*
|
25
|
+
* Provide just the username and password as strings
|
26
26
|
|
27
|
-
|
27
|
+
user_post = Wordpress::Post.new('username', 'password')
|
28
28
|
|
29
|
-
*
|
29
|
+
* OR Provide the url of your login page if it's self hosted
|
30
30
|
|
31
|
-
|
31
|
+
user_post = Wordpress::Post.new('username', 'password', 'http://blog.mysite.com/wp-login.php')
|
32
32
|
|
33
|
-
2.
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
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.
|
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 =
|
59
|
+
response = user_post.submit
|
64
60
|
|
65
|
-
5. You get a success or error hash back
|
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
|
-
|
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
|
74
|
+
* mechanize, & Mocha (For Testing)
|
76
75
|
|
77
76
|
== INSTALL:
|
78
77
|
|
79
|
-
* sudo gem install wordpress
|
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
|
9
|
+
p.extra_deps = ['mechanize']
|
10
|
+
p.extra_dev_deps = ['mocha']
|
11
11
|
end
|
12
12
|
|
13
13
|
# vim: syntax=Ruby
|
data/lib/wordpress.rb
CHANGED
@@ -3,13 +3,14 @@ require 'mechanize'
|
|
3
3
|
|
4
4
|
module Wordpress
|
5
5
|
|
6
|
-
VERSION = '0.1.
|
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
|
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
|
-
|
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
|
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
|
-
|
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
|
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
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
97
|
-
if
|
98
|
-
url =
|
99
|
-
pid =
|
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
|
data/test/test_wordpress.rb
CHANGED
@@ -2,27 +2,24 @@ require "test/unit"
|
|
2
2
|
require "wordpress"
|
3
3
|
require "mocha"
|
4
4
|
|
5
|
-
|
6
|
-
|
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::
|
23
|
-
@account_bad = Wordpress::
|
24
|
-
@account_invalid_login_page = Wordpress::
|
25
|
-
@account_hosted_account = Wordpress::
|
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::
|
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::
|
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::
|
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::
|
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::
|
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::
|
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::
|
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::
|
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
|
-
|
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
|
-
|
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
|
126
|
+
def test_update_raises_without_title_or_body
|
135
127
|
assert_raise Wordpress::PostError do
|
136
|
-
@account.
|
128
|
+
@account.submit
|
137
129
|
end
|
138
130
|
end
|
139
131
|
|
140
|
-
def
|
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.
|
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
|
-
|
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
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
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.
|
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
|
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.
|
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
|
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.
|
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
|
+
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-
|
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: :
|
27
|
+
type: :development
|
28
28
|
version_requirement:
|
29
29
|
version_requirements: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|