jordandobson-wordpress 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README.txt +9 -11
  2. data/lib/wordpress.rb +40 -44
  3. data/test/test_wordpress.rb +50 -84
  4. metadata +1 -1
data/README.txt CHANGED
@@ -44,21 +44,19 @@ Posting images with posts, posting only images and pulling down your posts will
44
44
 
45
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
- account.title = "My Title"
52
- account.body = "My Body Text"
53
- account.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. Send your post to your Wordpress Blog
57
+ response = account.post("", "Body Text")
56
58
 
57
- * Set this to a variable to work with the response
58
-
59
- response = account.post
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
 
data/lib/wordpress.rb CHANGED
@@ -1,71 +1,67 @@
1
1
  require 'rubygems'
2
2
  require 'mechanize'
3
-
3
+
4
4
  module Wordpress
5
-
6
- VERSION = '0.1.6'
7
-
5
+
6
+ VERSION = '0.1.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
13
  class Client
14
-
14
+
15
15
  DEFAULT_URL = 'http://wordpress.com/wp-login.php'
16
16
  LOGIN_FORM = 'loginform'
17
17
  POST_FORM = 'post'
18
18
  IS_ADMIN = 'body.wp-admin'
19
19
  IS_LOGIN = 'body.login'
20
-
21
- attr_accessor :title, :body
22
- attr_reader :login_url, :username, :password, :tags, :post_url, :agent
23
-
20
+
21
+ attr_reader :login_url, :username, :password, :agent
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?
27
-
28
- raise AuthError, "Url should end with wp-login.php" \
24
+
25
+ # should I
26
+ raise AuthError, "You must provide a username and password" \
27
+ if usr.empty? || pwd.empty?
28
+
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
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
- end
42
-
37
+
43
38
  def valid_login_page?
44
39
  !login_page.search("form[name=#{LOGIN_FORM}]").empty?
45
40
  end
46
-
41
+
47
42
  def valid_user?
48
43
  logged_into? dashboard_page
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
-
55
- def post
56
- raise PostError, "A post requires a title or body." unless @title || @body
49
+
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
64
-
60
+
65
61
  def login_page
66
62
  @agent.get @login_url
67
63
  end
68
-
64
+
69
65
  def dashboard_page
70
66
  page = login_page
71
67
  login_form = page.form(LOGIN_FORM)
@@ -76,28 +72,28 @@ module Wordpress
76
72
  end
77
73
  page
78
74
  end
79
-
75
+
80
76
  def logged_into? page
81
77
  !page.search(IS_ADMIN).empty?
82
78
  end
83
-
84
- def build_post form
85
- form.post_title = @title
86
- form.content = @body
87
- form.tags_input = @tags
79
+
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
-
91
- def post_response page
86
+
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
@@ -1,205 +1,171 @@
1
1
  require "test/unit"
2
2
  require "wordpress"
3
3
  require "mocha"
4
-
4
+
5
5
  class Wordpress::Client
6
6
  public :login_page, :dashboard_page, :logged_into?, :build_post, :post_response
7
7
  end
8
-
9
- # Need to test these methods
8
+
9
+ # Should Test The Following
10
10
  # * build_post
11
11
  # * dashboard_page
12
12
  # * Post Response failure
13
-
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
16
+
14
17
  class TestWordpress < Test::Unit::TestCase
15
-
18
+
16
19
  def setup
17
20
  @u = 'jordandobson'
18
21
  @p = 'password'
19
-
22
+
20
23
  @account = Wordpress::Client.new @u, @p
21
24
  @account_bad = Wordpress::Client.new @u, 'x'
22
25
  @account_invalid_login_page = Wordpress::Client.new @u, @p, 'http://notapage.gd/wp-login.php'
23
26
  @account_hosted_account = Wordpress::Client.new @u, @p, 'http://blog.getglue.net/wp-login.php'
24
-
27
+
25
28
  login_html = '<html><body class="login"><form name="loginform"></form></body></html>'
26
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>'
27
30
  success_html = '<div class="message"><p><a href="http://success.com/2009/?preview=1">preview</a><a href="http://success.com/wp-admin/post.php?post=99">edit</a></p></div>'
28
31
  fail_html = '<div class="message"><p></p></div>'
29
-
32
+
30
33
  @login_pg = setup_mock_mechanize_pg login_html
31
34
  @admin_pg = setup_mock_mechanize_pg admin_html
32
35
  @success_pg = setup_mock_mechanize_pg success_html
33
36
  @fail_pg = setup_mock_mechanize_pg fail_html
34
37
  end
35
-
38
+
36
39
  def setup_mock_mechanize_pg html
37
40
  WWW::Mechanize::Page.new(nil, {'content-type' => 'text/html'}, html, 200)
38
41
  end
39
-
42
+
40
43
  def test_sets_account_info_on_initialize
41
- actual = Wordpress::Client.new @u, @p
44
+ actual = Wordpress::Client.new @u, @p
42
45
  assert_equal [@u, @p], [actual.username, actual.password]
43
46
  end
44
-
47
+
45
48
  def test_raises_if_username_is_blank
46
49
  assert_raise Wordpress::AuthError do
47
- Wordpress::Client.new "", @p
50
+ Wordpress::Client.new "", @p
48
51
  end
49
52
  end
50
-
53
+
51
54
  def test_raises_if_password_is_blank
52
55
  assert_raise Wordpress::AuthError do
53
56
  Wordpress::Client.new @u, ""
54
57
  end
55
58
  end
56
-
57
- def test_raises_if_password_is_not_srting
58
- assert_raise Wordpress::AuthError do
59
- Wordpress::Client.new @u, 00
60
- end
61
- end
62
-
63
- def test_raises_if_username_is_not_srting
64
- assert_raise Wordpress::AuthError do
65
- Wordpress::Client.new 00, @p
66
- end
67
- end
68
-
59
+
69
60
  def test_login_url_uses_default_if_witheld
70
61
  assert_equal Wordpress::Client::DEFAULT_URL, @account.login_url
71
62
  end
72
-
63
+
73
64
  def test_users_url_does_not_raise
74
65
  assert_equal 'http://notapage.gd/wp-login.php', @account_invalid_login_page.login_url
75
66
  end
76
-
67
+
77
68
  def test_raises_on_bad_login_url
78
69
  assert_raise Wordpress::AuthError do
79
70
  Wordpress::Client.new @u, @p, 'http://bad.login/url.php'
80
71
  end
81
72
  end
82
-
73
+
83
74
  def test_login_page_is_valid
84
75
  actual = Wordpress::Client.new @u, @p
85
76
  actual.stubs(:login_page).returns(@login_pg)
86
77
  assert_equal true, actual.valid_login_page?
87
78
  end
88
-
79
+
89
80
  def test_login_page_is_invalid
90
81
  @account_invalid_login_page.stubs(:login_page).returns(@fail_pg)
91
82
  assert_equal false, @account_invalid_login_page.valid_login_page?
92
83
  end
93
-
84
+
94
85
  def test_is_a_valid_user
95
86
  @account.stubs(:dashboard_page).returns(@admin_pg)
96
87
  assert_equal true, @account.valid_user?
97
88
  end
98
-
89
+
99
90
  def test_is_an_invalid_user
100
91
  @account_bad.stubs(:dashboard_page).returns(@login_pg)
101
92
  assert_equal false, @account_bad.valid_user?
102
93
  end
103
-
94
+
104
95
  def test_is_a_valid_hosted_user
105
96
  @account_hosted_account.stubs(:dashboard_page).returns(@admin_pg)
106
97
  assert_equal true, @account_hosted_account.valid_user?
107
98
  end
108
-
99
+
109
100
  def test_private_logged_in_is_true
110
101
  assert_equal true, @account.logged_into?(@admin_pg)
111
102
  end
112
-
103
+
113
104
  def test_private_logged_in_is_false
114
105
  assert_equal false, @account.logged_into?(@login_pg)
115
106
  end
116
-
107
+
117
108
  def test_returns_blog_url
118
109
  @account_hosted_account.stubs(:dashboard_page).returns(@admin_pg)
119
110
  assert_equal 'http://getglue.wordpress.com/', @account_hosted_account.blog_url
120
111
  end
121
-
112
+
122
113
  def test_returns_blog_url_bad
123
114
  @account_invalid_login_page.stubs(:dashboard_page).raises(SocketError)
124
115
  assert_nil @account_invalid_login_page.blog_url
125
116
  end
126
-
127
- def test_update_raises_without_title_or_body
117
+
118
+ def test_post_raises_without_title_or_body
128
119
  assert_raise Wordpress::PostError do
129
- @account.post
120
+ @account.post("", "")
130
121
  end
131
122
  end
132
-
133
- def test_update_raises_without_post_form
123
+
124
+ def test_post_raises_without_post_form
134
125
  @account_bad.stubs(:dashboard_page).returns(@fail_pg)
135
- @account_bad.title = "Fail"
136
126
  assert_raise Wordpress::HostError do
137
- @account_bad.post
138
- end
139
- end
140
-
141
- def test_tags_are_added_correctly
142
- @account.tags = []
143
- assert_equal @account.tags, ""
144
- end
145
-
146
- def test_tags_single_is_correctly
147
- @account.tags = ["Glue"]
148
- assert_equal @account.tags, "Glue"
149
- end
150
-
151
- def test_tags_multiple_is_correctly_joined
152
- @account.tags = ["Glue", "Wordpress", "Ruby on Rails"]
153
- assert_equal @account.tags, "Glue, Wordpress, Ruby on Rails"
154
- end
155
-
156
- def test_raises_if_tags_not_set_as_array
157
- assert_raise Wordpress::TagsError do
158
- @account.tags = "hello, "
127
+ @account_bad.post("My Title", "")
159
128
  end
160
129
  end
161
-
130
+
162
131
  def test_post_response_returns_good_response
163
- assert_equal "ok", @account.post_response(@success_pg)["rsp"]["stat"]
132
+ assert_equal "ok", @account.post_response(@success_pg, "")["rsp"]["stat"]
164
133
  end
165
-
134
+
166
135
  def test_post_returns_fail
167
136
  title = "My Title"
168
- @account.title = title
169
- res = @account.post_response(@fail_pg)
170
- assert_equal "fail", res["rsp"]["stat"]
171
- assert_equal "Post was unsuccessful.", res["rsp"]["err"]["msg"]
172
- 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"]
173
141
  end
174
-
142
+
175
143
  def test_post_returns_ok
176
144
  @account.stubs(:dashboard_page).returns(@admin_pg)
177
145
  @account.agent.stubs(:submit).returns(@success_pg)
178
146
  title = "My Title"
179
- @account.title = title
180
- @account.body = "Body Text ..."
181
- actual = @account.post
147
+ body = "Body Text"
148
+ actual = @account.post(title, body)
182
149
  assert_equal "ok", actual["rsp"]["stat"]
183
150
  assert_equal title, actual["rsp"]["post"]["title"]
184
151
  assert_equal "99", actual["rsp"]["post"]["id"]
185
152
  assert_equal "http://success.com/2009/", actual["rsp"]["post"]["url"]
186
153
  end
187
-
154
+
188
155
  def test_post_returns_ok_with_only_title
189
156
  @account.stubs(:dashboard_page).returns(@admin_pg)
190
157
  @account.agent.stubs(:submit).returns(@success_pg)
191
158
  title = "My Title"
192
- @account.title = "My Title"
193
- actual = @account.post
159
+ actual = @account.post(title, "")
194
160
  assert_equal "ok", actual["rsp"]["stat"]
195
161
  assert_equal title, actual["rsp"]["post"]["title"]
196
162
  end
197
-
163
+
198
164
  def test_post_returns_ok_with_only_body
199
165
  @account.stubs(:dashboard_page).returns(@admin_pg)
200
166
  @account.agent.stubs(:submit).returns(@success_pg)
201
- @account.body = "Body Text ..."
202
- actual = @account.post
167
+ body = "Body Text"
168
+ actual = @account.post("", body)
203
169
  assert_equal "ok", actual["rsp"]["stat"]
204
170
  assert_equal "", actual["rsp"]["post"]["title"]
205
171
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jordandobson-wordpress
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Dobson