jordandobson-wordpress 0.1.5 → 0.1.6

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 +10 -10
  2. data/lib/wordpress.rb +23 -23
  3. data/test/test_wordpress.rb +59 -58
  4. metadata +1 -1
data/README.txt CHANGED
@@ -24,39 +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
47
  3. Setup your post
48
48
 
49
49
  * You must at least include the title or body. Tags must be an array.
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
+ account.title = "My Title"
52
+ account.body = "My Body Text"
53
+ account.tags = ["Glue", "Wordpress", "Ruby", "Made By Squad"]
54
54
 
55
- 4. Update your Wordpress Blog with your post
55
+ 4. Send your post to your Wordpress Blog
56
56
 
57
57
  * Set this to a variable to work with the response
58
58
 
59
- response = user_post.submit
59
+ response = account.post
60
60
 
61
61
  5. You get a success or error hash back
62
62
 
data/lib/wordpress.rb CHANGED
@@ -1,71 +1,71 @@
1
1
  require 'rubygems'
2
2
  require 'mechanize'
3
-
3
+
4
4
  module Wordpress
5
-
6
- VERSION = '0.1.5'
7
-
5
+
6
+ VERSION = '0.1.6'
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
-
13
- class Post
14
-
12
+
13
+ class Client
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
-
20
+
21
21
  attr_accessor :title, :body
22
22
  attr_reader :login_url, :username, :password, :tags, :post_url, :agent
23
-
23
+
24
24
  def initialize usr, pwd, login_url = DEFAULT_URL
25
25
  raise AuthError, "Blank Username or Password or not a string." \
26
26
  unless usr.is_a?(String) && pwd.is_a?(String) && !usr.empty? && !pwd.empty?
27
-
27
+
28
28
  raise AuthError, "Url should end with wp-login.php" \
29
29
  unless login_url =~ /\/wp-login[.]php$/
30
-
30
+
31
31
  @username = usr
32
32
  @password = pwd
33
33
  @login_url = login_url
34
34
  @agent = WWW::Mechanize.new
35
35
  @post_url = @tags = @title = @body = nil
36
36
  end
37
-
37
+
38
38
  def tags= ary
39
39
  raise TagsError, 'Tags must added using an array' unless ary.is_a?(Array)
40
40
  @tags = ary.join(", ")
41
41
  end
42
-
42
+
43
43
  def valid_login_page?
44
44
  !login_page.search("form[name=#{LOGIN_FORM}]").empty?
45
45
  end
46
-
46
+
47
47
  def valid_user?
48
48
  logged_into? dashboard_page
49
49
  end
50
-
50
+
51
51
  def blog_url
52
52
  dashboard_page.search("#{IS_ADMIN} #wphead h1 a").first['href'] rescue nil
53
53
  end
54
-
55
- def submit
54
+
55
+ def post
56
56
  raise PostError, "A post requires a title or body." unless @title || @body
57
57
  post_form = dashboard_page.form(POST_FORM)
58
58
  raise HostError, "Missing QuickPress on dashboard page or bad account." unless post_form
59
59
  post_form = build_post(post_form)
60
60
  post_response @agent.submit(post_form, post_form.buttons.last)
61
61
  end
62
-
62
+
63
63
  private
64
-
64
+
65
65
  def login_page
66
66
  @agent.get @login_url
67
67
  end
68
-
68
+
69
69
  def dashboard_page
70
70
  page = login_page
71
71
  login_form = page.form(LOGIN_FORM)
@@ -76,18 +76,18 @@ module Wordpress
76
76
  end
77
77
  page
78
78
  end
79
-
79
+
80
80
  def logged_into? page
81
81
  !page.search(IS_ADMIN).empty?
82
82
  end
83
-
83
+
84
84
  def build_post form
85
85
  form.post_title = @title
86
86
  form.content = @body
87
87
  form.tags_input = @tags
88
88
  form
89
89
  end
90
-
90
+
91
91
  def post_response page
92
92
  links = page.search("div.message p a")
93
93
  if links.first && links.last
@@ -1,168 +1,169 @@
1
1
  require "test/unit"
2
2
  require "wordpress"
3
3
  require "mocha"
4
-
5
- class Wordpress::Post
4
+
5
+ class Wordpress::Client
6
6
  public :login_page, :dashboard_page, :logged_into?, :build_post, :post_response
7
7
  end
8
-
8
+
9
9
  # Need to test these methods
10
10
  # * build_post
11
11
  # * dashboard_page
12
-
12
+ # * Post Response failure
13
+
13
14
  class TestWordpress < Test::Unit::TestCase
14
-
15
+
15
16
  def setup
16
17
  @u = 'jordandobson'
17
18
  @p = 'password'
18
-
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
-
19
+
20
+ @account = Wordpress::Client.new @u, @p
21
+ @account_bad = Wordpress::Client.new @u, 'x'
22
+ @account_invalid_login_page = Wordpress::Client.new @u, @p, 'http://notapage.gd/wp-login.php'
23
+ @account_hosted_account = Wordpress::Client.new @u, @p, 'http://blog.getglue.net/wp-login.php'
24
+
24
25
  login_html = '<html><body class="login"><form name="loginform"></form></body></html>'
25
26
  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>'
26
27
  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>'
27
28
  fail_html = '<div class="message"><p></p></div>'
28
-
29
+
29
30
  @login_pg = setup_mock_mechanize_pg login_html
30
31
  @admin_pg = setup_mock_mechanize_pg admin_html
31
32
  @success_pg = setup_mock_mechanize_pg success_html
32
33
  @fail_pg = setup_mock_mechanize_pg fail_html
33
34
  end
34
-
35
+
35
36
  def setup_mock_mechanize_pg html
36
37
  WWW::Mechanize::Page.new(nil, {'content-type' => 'text/html'}, html, 200)
37
38
  end
38
-
39
+
39
40
  def test_sets_account_info_on_initialize
40
- actual = Wordpress::Post.new @u, @p
41
+ actual = Wordpress::Client.new @u, @p
41
42
  assert_equal [@u, @p], [actual.username, actual.password]
42
43
  end
43
-
44
+
44
45
  def test_raises_if_username_is_blank
45
46
  assert_raise Wordpress::AuthError do
46
- Wordpress::Post.new "", @p
47
+ Wordpress::Client.new "", @p
47
48
  end
48
49
  end
49
-
50
+
50
51
  def test_raises_if_password_is_blank
51
52
  assert_raise Wordpress::AuthError do
52
- Wordpress::Post.new @u, ""
53
+ Wordpress::Client.new @u, ""
53
54
  end
54
55
  end
55
-
56
+
56
57
  def test_raises_if_password_is_not_srting
57
58
  assert_raise Wordpress::AuthError do
58
- Wordpress::Post.new @u, 00
59
+ Wordpress::Client.new @u, 00
59
60
  end
60
61
  end
61
-
62
+
62
63
  def test_raises_if_username_is_not_srting
63
64
  assert_raise Wordpress::AuthError do
64
- Wordpress::Post.new 00, @p
65
+ Wordpress::Client.new 00, @p
65
66
  end
66
67
  end
67
-
68
+
68
69
  def test_login_url_uses_default_if_witheld
69
- assert_equal Wordpress::Post::DEFAULT_URL, @account.login_url
70
+ assert_equal Wordpress::Client::DEFAULT_URL, @account.login_url
70
71
  end
71
-
72
+
72
73
  def test_users_url_does_not_raise
73
74
  assert_equal 'http://notapage.gd/wp-login.php', @account_invalid_login_page.login_url
74
75
  end
75
-
76
+
76
77
  def test_raises_on_bad_login_url
77
78
  assert_raise Wordpress::AuthError do
78
- Wordpress::Post.new @u, @p, 'http://bad.login/url.php'
79
+ Wordpress::Client.new @u, @p, 'http://bad.login/url.php'
79
80
  end
80
81
  end
81
-
82
+
82
83
  def test_login_page_is_valid
83
- actual = Wordpress::Post.new @u, @p
84
+ actual = Wordpress::Client.new @u, @p
84
85
  actual.stubs(:login_page).returns(@login_pg)
85
86
  assert_equal true, actual.valid_login_page?
86
87
  end
87
-
88
+
88
89
  def test_login_page_is_invalid
89
90
  @account_invalid_login_page.stubs(:login_page).returns(@fail_pg)
90
91
  assert_equal false, @account_invalid_login_page.valid_login_page?
91
92
  end
92
-
93
+
93
94
  def test_is_a_valid_user
94
95
  @account.stubs(:dashboard_page).returns(@admin_pg)
95
96
  assert_equal true, @account.valid_user?
96
97
  end
97
-
98
+
98
99
  def test_is_an_invalid_user
99
100
  @account_bad.stubs(:dashboard_page).returns(@login_pg)
100
101
  assert_equal false, @account_bad.valid_user?
101
102
  end
102
-
103
+
103
104
  def test_is_a_valid_hosted_user
104
105
  @account_hosted_account.stubs(:dashboard_page).returns(@admin_pg)
105
106
  assert_equal true, @account_hosted_account.valid_user?
106
107
  end
107
-
108
+
108
109
  def test_private_logged_in_is_true
109
110
  assert_equal true, @account.logged_into?(@admin_pg)
110
111
  end
111
-
112
+
112
113
  def test_private_logged_in_is_false
113
114
  assert_equal false, @account.logged_into?(@login_pg)
114
115
  end
115
-
116
+
116
117
  def test_returns_blog_url
117
118
  @account_hosted_account.stubs(:dashboard_page).returns(@admin_pg)
118
119
  assert_equal 'http://getglue.wordpress.com/', @account_hosted_account.blog_url
119
120
  end
120
-
121
+
121
122
  def test_returns_blog_url_bad
122
123
  @account_invalid_login_page.stubs(:dashboard_page).raises(SocketError)
123
124
  assert_nil @account_invalid_login_page.blog_url
124
125
  end
125
-
126
+
126
127
  def test_update_raises_without_title_or_body
127
128
  assert_raise Wordpress::PostError do
128
- @account.submit
129
+ @account.post
129
130
  end
130
131
  end
131
-
132
+
132
133
  def test_update_raises_without_post_form
133
134
  @account_bad.stubs(:dashboard_page).returns(@fail_pg)
134
135
  @account_bad.title = "Fail"
135
136
  assert_raise Wordpress::HostError do
136
- @account_bad.submit
137
+ @account_bad.post
137
138
  end
138
139
  end
139
-
140
+
140
141
  def test_tags_are_added_correctly
141
142
  @account.tags = []
142
143
  assert_equal @account.tags, ""
143
144
  end
144
-
145
+
145
146
  def test_tags_single_is_correctly
146
147
  @account.tags = ["Glue"]
147
148
  assert_equal @account.tags, "Glue"
148
149
  end
149
-
150
+
150
151
  def test_tags_multiple_is_correctly_joined
151
152
  @account.tags = ["Glue", "Wordpress", "Ruby on Rails"]
152
153
  assert_equal @account.tags, "Glue, Wordpress, Ruby on Rails"
153
154
  end
154
-
155
+
155
156
  def test_raises_if_tags_not_set_as_array
156
157
  assert_raise Wordpress::TagsError do
157
158
  @account.tags = "hello, "
158
159
  end
159
160
  end
160
-
161
+
161
162
  def test_post_response_returns_good_response
162
163
  assert_equal "ok", @account.post_response(@success_pg)["rsp"]["stat"]
163
164
  end
164
-
165
- def test_update_returns_fail
165
+
166
+ def test_post_returns_fail
166
167
  title = "My Title"
167
168
  @account.title = title
168
169
  res = @account.post_response(@fail_pg)
@@ -170,35 +171,35 @@ class TestWordpress < Test::Unit::TestCase
170
171
  assert_equal "Post was unsuccessful.", res["rsp"]["err"]["msg"]
171
172
  assert_equal title, res["rsp"]["err"]["title"]
172
173
  end
173
-
174
- def test_update_returns_ok
174
+
175
+ def test_post_returns_ok
175
176
  @account.stubs(:dashboard_page).returns(@admin_pg)
176
177
  @account.agent.stubs(:submit).returns(@success_pg)
177
178
  title = "My Title"
178
179
  @account.title = title
179
180
  @account.body = "Body Text ..."
180
- actual = @account.submit
181
+ actual = @account.post
181
182
  assert_equal "ok", actual["rsp"]["stat"]
182
183
  assert_equal title, actual["rsp"]["post"]["title"]
183
184
  assert_equal "99", actual["rsp"]["post"]["id"]
184
185
  assert_equal "http://success.com/2009/", actual["rsp"]["post"]["url"]
185
186
  end
186
-
187
- def test_update_returns_ok_with_only_title
187
+
188
+ def test_post_returns_ok_with_only_title
188
189
  @account.stubs(:dashboard_page).returns(@admin_pg)
189
190
  @account.agent.stubs(:submit).returns(@success_pg)
190
191
  title = "My Title"
191
192
  @account.title = "My Title"
192
- actual = @account.submit
193
+ actual = @account.post
193
194
  assert_equal "ok", actual["rsp"]["stat"]
194
195
  assert_equal title, actual["rsp"]["post"]["title"]
195
196
  end
196
-
197
- def test_update_returns_ok_with_only_body
197
+
198
+ def test_post_returns_ok_with_only_body
198
199
  @account.stubs(:dashboard_page).returns(@admin_pg)
199
200
  @account.agent.stubs(:submit).returns(@success_pg)
200
201
  @account.body = "Body Text ..."
201
- actual = @account.submit
202
+ actual = @account.post
202
203
  assert_equal "ok", actual["rsp"]["stat"]
203
204
  assert_equal "", actual["rsp"]["post"]["title"]
204
205
  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.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Dobson