jordandobson-wordpress 0.1.2 → 0.1.3

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.
data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ === 1.0.3 / 2009-06-05
2
+
3
+ * 1 major enhancement
4
+
5
+ * Stubbed test and refactored code
6
+
1
7
  === 1.0.1 / 2009-06-05
2
8
 
3
9
  * 1 major enhancement
data/README.txt CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- The Wordpress gem provides posting to a Wordpress.com blog or a self hosted wordpress by provided your username, password, login url(if you host your blog) and your blog content. With this gem, you have access to add a text entry on Wordpress blug by providing these options: a title, body, and tags. You must include at least a title or body for your post.
7
+ The Wordpress gem provides posting to a Wordpress.com blog or a self hosted wordpress by providing your username, password, login url(if you host your blog) and your blog content. With this gem, you have access to add a text entry on Wordpress blog by providing these options: title text, body text, and a tag array. You must include at least title text or body text for your post.
8
8
 
9
9
  Posting images with posts, posting only images and pulling down your posts will be available very soon.
10
10
 
data/bin/wordpress CHANGED
@@ -1,3 +1 @@
1
1
  #!/usr/bin/env ruby
2
-
3
- abort "you need to write me"
data/lib/wordpress.rb CHANGED
@@ -3,10 +3,11 @@ require 'mechanize'
3
3
 
4
4
  module Wordpress
5
5
 
6
- VERSION = '0.1.2'
6
+ VERSION = '0.1.3'
7
7
 
8
8
  class AuthError < StandardError; end
9
9
  class PostError < StandardError; end
10
+ class HostError < StandardError; end
10
11
 
11
12
  class Client
12
13
 
@@ -17,57 +18,57 @@ module Wordpress
17
18
  IS_LOGIN = 'body.login'
18
19
 
19
20
  attr_accessor :title, :body
20
- attr_reader :login_url, :username, :password, :tags, :post_url
21
+ attr_reader :login_url, :username, :password, :tags, :post_url, :agent
21
22
 
22
23
  def initialize usr, pwd, login_url = DEFAULT_URL
23
24
  raise AuthError, "Blank Username or Password or not a string." \
24
25
  if !usr.is_a?(String) || !pwd.is_a?(String) || usr == '' || pwd == ''
25
-
26
+
26
27
  raise AuthError, "Url should end with wp-login.php" \
27
28
  unless login_url =~ /\/wp-login[.]php$/
28
-
29
+
29
30
  @username = usr
30
31
  @password = pwd
31
32
  @login_url = login_url
32
-
33
- @agent = @post_url = @tags = @title = @body = nil
33
+ @agent = WWW::Mechanize.new
34
+ @post_url = @tags = @title = @body = nil
34
35
  end
35
-
36
+
36
37
  def tags= ary
37
38
  raise TagError, 'Tags must added using an array' if !ary.is_a?(Array)
38
39
  @tags = ary.join(", ")
39
40
  end
40
41
 
41
42
  def valid_login_page?
42
- lf = login_page.form(LOGIN_FORM)
43
- lf && lf.log ? true : false
43
+ !login_page.search("form[name=#{LOGIN_FORM}]").empty?
44
44
  end
45
-
45
+
46
46
  def valid_user?
47
47
  logged_into? dashboard_page
48
48
  end
49
-
49
+
50
50
  def blog_url
51
- a = dashboard_page.search("#{IS_ADMIN} #wphead h1 a")
52
- return a.first['href'] if a.first && a.first['href']
53
- nil
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
54
56
  end
55
-
57
+
56
58
  def add_post
59
+ raise PostError, "A post requires a title or body." unless @title || @body
57
60
  post_form = dashboard_page.form(POST_FORM)
58
- raise PostError, "Missing QuickPress form on users dashboard page." unless post_form
59
- raise PostError, "A post requires a title or body." if !@title && !@body
61
+ raise HostError, "Missing QuickPress on dashboard page or bad account." unless post_form
60
62
  post_form = build_post(post_form)
61
- build_response @agent.submit(post_form, post_form.buttons.last)
63
+ post_response @agent.submit(post_form, post_form.buttons.last)
62
64
  end
63
65
 
64
66
  private
65
-
67
+
66
68
  def login_page
67
- @agent = WWW::Mechanize.new #if !@agent
68
- @agent.get @login_url
69
+ @agent.get @login_url
69
70
  end
70
-
71
+
71
72
  def dashboard_page
72
73
  page = login_page
73
74
  login_form = page.form(LOGIN_FORM)
@@ -76,24 +77,31 @@ module Wordpress
76
77
  login_form.pwd = @password
77
78
  page = @agent.submit login_form
78
79
  end
80
+ puts page.inspect
79
81
  page
80
82
  end
81
83
 
82
84
  def logged_into? page
83
85
  !page.search(IS_ADMIN).empty?
84
86
  end
85
-
87
+
86
88
  def build_post f
87
89
  f.post_title = @title
88
90
  f.content = @body
89
91
  f.tags_input = @tags
90
92
  f
91
93
  end
92
-
93
- def build_response page
94
- return true
95
- #get preview url & if it's not there send back error response
96
- end
97
94
 
95
+ 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
100
+ if pid && url
101
+ return { "rsp" => { "post" => { "title" => "#{@title}", "url" => "#{url}", "id" => "#{pid}" }, "stat" => "ok" }}
102
+ end
103
+ end
104
+ { "rsp" => { "err" => { "msg" => "Post was unsuccessful.", "title" => "#{@title}" }, "stat" => "fail" }}
105
+ end
98
106
  end
99
107
  end
@@ -1,19 +1,18 @@
1
1
  require "test/unit"
2
2
  require "wordpress"
3
- #
3
+ require "mocha"
4
4
 
5
5
  ######
6
6
  # USED TO TEST PRIVATE METHODS
7
7
  class Class
8
8
  def private_methods
9
9
  m = self.private_instance_methods
10
- self.class_eval { public(*m) }
10
+ self.class_eval { public( *m ) }
11
11
  yield
12
- self.class_eval { private(*m) }
12
+ self.class_eval { private( *m ) }
13
13
  end
14
14
  end
15
15
 
16
-
17
16
  class TestWordpress < Test::Unit::TestCase
18
17
 
19
18
  def setup
@@ -22,12 +21,25 @@ class TestWordpress < Test::Unit::TestCase
22
21
 
23
22
  @account = Wordpress::Client.new @u, @p
24
23
  @account_bad = Wordpress::Client.new @u, 'x'
25
- @account_invalid_login_page = Wordpress::Client.new @u, @p, 'http://is.gd/wp-login.php'
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'
26
+
27
+ login_html = '<html><body class="login"><form name="loginform"></form></body></html>'
28
+ 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>'
29
+ 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>'
30
+ fail_html = '<div class="message"><p></p></div>'
31
+
32
+ @login_pg = setup_mock_mechanize_pg login_html
33
+ @admin_pg = setup_mock_mechanize_pg admin_html
34
+ @success_pg = setup_mock_mechanize_pg success_html
35
+ @fail_pg = setup_mock_mechanize_pg fail_html
36
+
37
+ end
26
38
 
27
- @admin_pg = Nokogiri::HTML( Nokogiri::HTML::Builder.new { html { body( :class => 'wp-admin') } }.to_html )
28
- @login_pg = Nokogiri::HTML( Nokogiri::HTML::Builder.new { html { body( :class => 'login' ) } }.to_html )
39
+ def setup_mock_mechanize_pg html
40
+ WWW::Mechanize::Page.new(nil, {'content-type' => 'text/html'}, html, 200)
29
41
  end
30
-
42
+
31
43
  def test_sets_account_info_on_initialize
32
44
  actual = Wordpress::Client.new @u, @p
33
45
  assert_equal [@u, @p], [actual.username, actual.password]
@@ -61,73 +73,124 @@ class TestWordpress < Test::Unit::TestCase
61
73
  assert_equal Wordpress::Client::DEFAULT_URL, @account.login_url
62
74
  end
63
75
 
64
- def test_uses_url_does_not_raise
65
- assert_equal 'http://is.gd/wp-login.php', @account_invalid_login_page.login_url
76
+ def test_users_url_does_not_raise
77
+ assert_equal 'http://notapage.gd/wp-login.php', @account_invalid_login_page.login_url
66
78
  end
67
-
79
+
68
80
  def test_raises_on_bad_login_url
69
81
  assert_raise Wordpress::AuthError do
70
82
  Wordpress::Client.new @u, @p, 'http://bad.login/url.php'
71
83
  end
72
84
  end
73
-
85
+
74
86
  def test_login_page_is_valid
75
87
  actual = Wordpress::Client.new @u, @p
88
+ actual.stubs(:login_page).returns(@login_pg)
76
89
  assert_equal true, actual.valid_login_page?
77
90
  end
78
91
 
79
92
  def test_login_page_is_invalid
93
+ @account_invalid_login_page.stubs(:login_page).returns(@fail_pg)
80
94
  assert_equal false, @account_invalid_login_page.valid_login_page?
81
95
  end
82
-
96
+
83
97
  def test_is_a_valid_user
84
- assert_equal true, @account.valid_user?
98
+ @account.stubs(:dashboard_page).returns(@admin_pg)
99
+ assert_equal true, @account.valid_user?
85
100
  end
86
-
101
+
87
102
  def test_is_an_invalid_user
88
- assert_equal false, @account_bad.valid_user?
103
+ @account_bad.stubs(:dashboard_page).returns(@login_pg)
104
+ assert_equal false, @account_bad.valid_user?
89
105
  end
90
-
106
+
91
107
  def test_is_a_valid_hosted_user
92
- account = Wordpress::Client.new('nonbreakablespace', 'Password1', 'http://blog.nonbreakablespace.com/wp-login.php')
93
- assert_equal true, account.valid_user?
94
- end
95
- def test_returns_blog_url
96
- expected = 'http://blog.nonbreakablespace.com/'
97
- account = Wordpress::Client.new('nonbreakablespace', 'Password1', "#{expected}wp-login.php")
98
- assert_equal expected, account.blog_url
99
- # Need to stub dashboard_page
108
+ @account_hosted_account.stubs(:dashboard_page).returns(@admin_pg)
109
+ assert_equal true, @account_hosted_account.valid_user?
100
110
  end
101
-
102
- def test_returns_blog_url_bad
103
- account = Wordpress::Client.new(@u, @p, 'http://is.gd/wp-login.php')
104
- assert_nil account.blog_url
105
- end
106
-
111
+
107
112
  def test_private_logged_in_is_true
108
- Wordpress::Client.private_methods { assert_equal true, @account.logged_into?(@admin_pg) }
113
+ Wordpress::Client.private_methods {
114
+ assert_equal true, @account.logged_into?(@admin_pg)
115
+ }
109
116
  end
110
117
 
111
118
  def test_private_logged_in_is_false
112
- Wordpress::Client.private_methods { assert_equal false, @account.logged_into?(@login_pg) }
119
+ Wordpress::Client.private_methods {
120
+ assert_equal false, @account.logged_into?(@login_pg)
121
+ }
122
+ end
123
+
124
+ def test_returns_blog_url
125
+ @account_hosted_account.stubs(:dashboard_page).returns(@admin_pg)
126
+ assert_equal 'http://getglue.wordpress.com/', @account_hosted_account.blog_url
127
+ end
128
+
129
+ def test_returns_blog_url_bad
130
+ @account_invalid_login_page.stubs(:dashboard_page).raises(SocketError)
131
+ assert_nil @account_invalid_login_page.blog_url
113
132
  end
114
-
133
+
115
134
  def test_add_post_raises_without_title_or_body
116
135
  assert_raise Wordpress::PostError do
117
136
  @account.add_post
118
137
  end
119
138
  end
120
-
139
+
121
140
  def test_add_post_raises_without_post_form
122
- assert_raise Wordpress::PostError do
141
+ @account_bad.stubs(:dashboard_page).returns(@fail_pg)
142
+ @account_bad.title = "Fail"
143
+ assert_raise Wordpress::HostError do
123
144
  @account_bad.add_post
124
- end
145
+ end
125
146
  end
126
-
127
- def test_add_post_adds_post
128
- @account.title = Time.now
129
- @account.body = "updated next"
130
- @account.add_post
147
+
148
+ def test_post_response_returns_good_response
149
+ Wordpress::Client.private_methods {
150
+ assert_equal "ok", @account.post_response(@success_pg)["rsp"]["stat"]
151
+ }
131
152
  end
132
153
 
133
- end
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
+ }
163
+ end
164
+
165
+ def test_add_post_returns_ok
166
+ @account.stubs(:dashboard_page).returns(@admin_pg)
167
+ @account.agent.stubs(:submit).returns(@success_pg)
168
+ title = "My Title"
169
+ @account.title = title
170
+ @account.body = "Body Text ..."
171
+ actual = @account.add_post
172
+ assert_equal "ok", actual["rsp"]["stat"]
173
+ assert_equal title, actual["rsp"]["post"]["title"]
174
+ assert_equal "99", actual["rsp"]["post"]["id"]
175
+ assert_equal "http://success.com/2009/", actual["rsp"]["post"]["url"]
176
+ end
177
+
178
+ def test_add_post_returns_ok_with_only_title
179
+ @account.stubs(:dashboard_page).returns(@admin_pg)
180
+ @account.agent.stubs(:submit).returns(@success_pg)
181
+ title = "My Title"
182
+ @account.title = "My Title"
183
+ actual = @account.add_post
184
+ assert_equal "ok", actual["rsp"]["stat"]
185
+ assert_equal title, actual["rsp"]["post"]["title"]
186
+ end
187
+
188
+ def test_add_post_returns_ok_with_only_body
189
+ @account.stubs(:dashboard_page).returns(@admin_pg)
190
+ @account.agent.stubs(:submit).returns(@success_pg)
191
+ @account.body = "Body Text ..."
192
+ actual = @account.add_post
193
+ assert_equal "ok", actual["rsp"]["stat"]
194
+ assert_equal "", actual["rsp"]["post"]["title"]
195
+ end
196
+ 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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Dobson
@@ -22,7 +22,7 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.8.3
24
24
  version:
25
- description: The Wordpress gem provides posting to a Wordpress.com blog or a self hosted wordpress by provided your username, password, login url(if you host your blog) and your blog content.
25
+ description: The Wordpress gem provides posting to a Wordpress.com blog or a self hosted wordpress by providing your username, password, login url(if you host your blog) and your blog content.
26
26
  email:
27
27
  - jordan.dobson@madebysquad.com
28
28
  executables: []
@@ -67,6 +67,6 @@ rubyforge_project: wordpress
67
67
  rubygems_version: 1.2.0
68
68
  signing_key:
69
69
  specification_version: 2
70
- summary: "The Wordpress gem provides posting to a Wordpress.com blog or a self hosted wordpress by provided your username, password, login url(if you host your blog) and your blog content. With this gem, you have access to add a text entry on Wordpress blug by providing these options: a title, body, and tags. You must include at least a title or body for your post."
70
+ summary: "The Wordpress gem provides posting to a Wordpress.com blog or a self hosted wordpress by providing your username, password, login url(if you host your blog) and your blog content. With this gem, you have access to add a text entry on Wordpress blog by providing these options: title text, body text, and a tag array. You must include at least title text or body text for your post."
71
71
  test_files:
72
72
  - test/test_wordpress.rb