jordandobson-posterous 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +12 -0
- data/Manifest.txt +7 -0
- data/README.txt +104 -0
- data/Rakefile +13 -0
- data/bin/wordpress +3 -0
- data/lib/wordpress.rb +99 -0
- data/test/test_wordpress.rb +133 -0
- metadata +72 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
= wordpress
|
2
|
+
|
3
|
+
* http://JordanDobson.com
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
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.
|
8
|
+
|
9
|
+
Posting images with posts, posting only images and pulling down your posts will be available very soon.
|
10
|
+
|
11
|
+
== FEATURES/PROBLEMS:
|
12
|
+
|
13
|
+
* Either Title or Body is optional
|
14
|
+
* Adding Images are not yet implemented
|
15
|
+
* Posting Only, Reading & Images are not yet included
|
16
|
+
* Check if a username and password are valid
|
17
|
+
* Check if a provided login_url is valid
|
18
|
+
* Get the users blog url
|
19
|
+
* This is very throughly tested
|
20
|
+
|
21
|
+
== SYNOPSIS:
|
22
|
+
|
23
|
+
1. Instantiate your account
|
24
|
+
|
25
|
+
* You can provide just the username and password
|
26
|
+
|
27
|
+
account = Wordpress::Client.new('username', 'password')
|
28
|
+
|
29
|
+
* Or you can provide the ID as a string or integer
|
30
|
+
|
31
|
+
account = Wordpress::Client.new('username', 'password', 'http://blog.mysite.com/wp-login.php')
|
32
|
+
|
33
|
+
2. Get more info about the user's account if you need it
|
34
|
+
|
35
|
+
* Check if the user is valid
|
36
|
+
|
37
|
+
account.valid_user?
|
38
|
+
|
39
|
+
* Check if the specified login page is valid
|
40
|
+
|
41
|
+
account.valid_login_page?
|
42
|
+
|
43
|
+
* Get the users blog page url
|
44
|
+
|
45
|
+
account.blog_url
|
46
|
+
|
47
|
+
* Get a list of your sites and additional info
|
48
|
+
|
49
|
+
account.account_info
|
50
|
+
|
51
|
+
3. Setup your post
|
52
|
+
|
53
|
+
* You must at least include the title or body
|
54
|
+
|
55
|
+
account.title = "My Title"
|
56
|
+
account.body = "My Body Text"
|
57
|
+
account.tags = ["Glue", "Posterous", "Ruby", "Made By Squad"]
|
58
|
+
|
59
|
+
4. Add your post to Posterous.com
|
60
|
+
|
61
|
+
* Set this to a variable to work with the response
|
62
|
+
|
63
|
+
response = account.add_post
|
64
|
+
|
65
|
+
5. You get a success or error hash back or nil
|
66
|
+
|
67
|
+
* Your response should look something like this if successful
|
68
|
+
|
69
|
+
response #=> { "rsp" => { "post" => { "title" => "My Title", "url" => "http://getglue.wordpress.com/2009/06/06/my-title/", "id" => "69" }, "stat" => "ok" } }
|
70
|
+
|
71
|
+
* See the tests for this gem for failure responses and responses for other methods
|
72
|
+
|
73
|
+
== REQUIREMENTS:
|
74
|
+
|
75
|
+
* mechanize, & Mocha (For Tests)
|
76
|
+
|
77
|
+
== INSTALL:
|
78
|
+
|
79
|
+
* sudo gem install wordpress -include-dependencies
|
80
|
+
|
81
|
+
== LICENSE:
|
82
|
+
|
83
|
+
(The MIT License)
|
84
|
+
|
85
|
+
Copyright (c) 2009 Jordan Dobson
|
86
|
+
|
87
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
88
|
+
a copy of this software and associated documentation files (the
|
89
|
+
'Software'), to deal in the Software without restriction, including
|
90
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
91
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
92
|
+
permit persons to whom the Software is furnished to do so, subject to
|
93
|
+
the following conditions:
|
94
|
+
|
95
|
+
The above copyright notice and this permission notice shall be
|
96
|
+
included in all copies or substantial portions of the Software.
|
97
|
+
|
98
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
99
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
100
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
101
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
102
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
103
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
104
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/wordpress.rb'
|
6
|
+
|
7
|
+
Hoe.new('wordpress', Wordpress::VERSION) do |p|
|
8
|
+
# p.rubyforge_name = 'wordpressx' # if different than lowercase project name
|
9
|
+
p.developer('Jordan Dobson', 'jordan.dobson@madebysquad.com')
|
10
|
+
p.extra_deps = ['mechanize', 'mocha']
|
11
|
+
end
|
12
|
+
|
13
|
+
# vim: syntax=Ruby
|
data/bin/wordpress
ADDED
data/lib/wordpress.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'mechanize'
|
3
|
+
|
4
|
+
module Wordpress
|
5
|
+
|
6
|
+
VERSION = '0.1.1'
|
7
|
+
|
8
|
+
class AuthError < StandardError; end
|
9
|
+
class PostError < StandardError; end
|
10
|
+
|
11
|
+
class Client
|
12
|
+
|
13
|
+
DEFAULT_URL = 'http://wordpress.com/wp-login.php'
|
14
|
+
LOGIN_FORM = 'loginform'
|
15
|
+
POST_FORM = 'post'
|
16
|
+
IS_ADMIN = 'body.wp-admin'
|
17
|
+
IS_LOGIN = 'body.login'
|
18
|
+
|
19
|
+
attr_accessor :title, :body
|
20
|
+
attr_reader :login_url, :username, :password, :tags, :post_url
|
21
|
+
|
22
|
+
def initialize usr, pwd, login_url = DEFAULT_URL
|
23
|
+
raise AuthError, "Blank Username or Password or not a string." \
|
24
|
+
if !usr.is_a?(String) || !pwd.is_a?(String) || usr == '' || pwd == ''
|
25
|
+
|
26
|
+
raise AuthError, "Url should end with wp-login.php" \
|
27
|
+
unless login_url =~ /\/wp-login[.]php$/
|
28
|
+
|
29
|
+
@username = usr
|
30
|
+
@password = pwd
|
31
|
+
@login_url = login_url
|
32
|
+
@agent = nil
|
33
|
+
@post_url = nil
|
34
|
+
end
|
35
|
+
|
36
|
+
def tags= ary
|
37
|
+
raise TagError, 'Tags must added using an array' if !ary.is_a?(Array)
|
38
|
+
@tags = ary.join(", ")
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid_login_page?
|
42
|
+
lf = login_page.form(LOGIN_FORM)
|
43
|
+
lf && lf.log ? true : false
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid_user?
|
47
|
+
logged_into? dashboard_page
|
48
|
+
end
|
49
|
+
|
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
|
54
|
+
end
|
55
|
+
|
56
|
+
def add_post
|
57
|
+
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
|
60
|
+
post_form = build_post(post_form)
|
61
|
+
build_response @agent.submit(post_form, post_form.buttons.last)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def login_page
|
67
|
+
@agent = WWW::Mechanize.new #if !@agent
|
68
|
+
@agent.get @login_url
|
69
|
+
end
|
70
|
+
|
71
|
+
def dashboard_page
|
72
|
+
page = login_page
|
73
|
+
login_form = page.form(LOGIN_FORM)
|
74
|
+
if login_form
|
75
|
+
login_form.log = @username
|
76
|
+
login_form.pwd = @password
|
77
|
+
page = @agent.submit login_form
|
78
|
+
end
|
79
|
+
page
|
80
|
+
end
|
81
|
+
|
82
|
+
def logged_into? page
|
83
|
+
!page.search(IS_ADMIN).empty?
|
84
|
+
end
|
85
|
+
|
86
|
+
def build_post f
|
87
|
+
f.post_title = @title
|
88
|
+
f.content = @body
|
89
|
+
f.tags_input = @tags
|
90
|
+
f
|
91
|
+
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
|
+
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "wordpress"
|
3
|
+
#
|
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
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
class TestWordpress < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
@u = 'jordandobson'
|
21
|
+
@p = 'password'
|
22
|
+
|
23
|
+
@account = Wordpress::Client.new @u, @p
|
24
|
+
@account_bad = Wordpress::Client.new @u, 'x'
|
25
|
+
@account_invalid_login_page = Wordpress::Client.new @u, @p, 'http://is.gd/wp-login.php'
|
26
|
+
|
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 )
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_sets_account_info_on_initialize
|
32
|
+
actual = Wordpress::Client.new @u, @p
|
33
|
+
assert_equal [@u, @p], [actual.username, actual.password]
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_raises_if_username_is_blank
|
37
|
+
assert_raise Wordpress::AuthError do
|
38
|
+
Wordpress::Client.new "", @p
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_raises_if_password_is_blank
|
43
|
+
assert_raise Wordpress::AuthError do
|
44
|
+
Wordpress::Client.new @u, ""
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_raises_if_password_is_not_srting
|
49
|
+
assert_raise Wordpress::AuthError do
|
50
|
+
Wordpress::Client.new @u, 00
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_raises_if_username_is_not_srting
|
55
|
+
assert_raise Wordpress::AuthError do
|
56
|
+
Wordpress::Client.new 00, @p
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_login_url_uses_default_if_witheld
|
61
|
+
assert_equal Wordpress::Client::DEFAULT_URL, @account.login_url
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_uses_url_does_not_raise
|
65
|
+
assert_equal 'http://is.gd/wp-login.php', @account_invalid_login_page.login_url
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_raises_on_bad_login_url
|
69
|
+
assert_raise Wordpress::AuthError do
|
70
|
+
Wordpress::Client.new @u, @p, 'http://bad.login/url.php'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_login_page_is_valid
|
75
|
+
actual = Wordpress::Client.new @u, @p
|
76
|
+
assert_equal true, actual.valid_login_page?
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_login_page_is_invalid
|
80
|
+
assert_equal false, @account_invalid_login_page.valid_login_page?
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_is_a_valid_user
|
84
|
+
assert_equal true, @account.valid_user?
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_is_an_invalid_user
|
88
|
+
assert_equal false, @account_bad.valid_user?
|
89
|
+
end
|
90
|
+
|
91
|
+
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
|
100
|
+
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
|
+
|
107
|
+
def test_private_logged_in_is_true
|
108
|
+
Wordpress::Client.private_methods { assert_equal true, @account.logged_into?(@admin_pg) }
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_private_logged_in_is_false
|
112
|
+
Wordpress::Client.private_methods { assert_equal false, @account.logged_into?(@login_pg) }
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_add_post_raises_without_title_or_body
|
116
|
+
assert_raise Wordpress::PostError do
|
117
|
+
@account.add_post
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_add_post_raises_without_post_form
|
122
|
+
assert_raise Wordpress::PostError do
|
123
|
+
@account_bad.add_post
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_add_post_adds_post
|
128
|
+
@account.title = Time.now
|
129
|
+
@account.body = "updated next"
|
130
|
+
@account.add_post
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jordandobson-posterous
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordan Dobson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-05 00:00:00 -07:00
|
13
|
+
default_executable: wordpress
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.3
|
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.
|
26
|
+
email:
|
27
|
+
- jordan.dobson@madebysquad.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- README.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- README.txt
|
40
|
+
- Rakefile
|
41
|
+
- bin/wordpress
|
42
|
+
- lib/wordpress.rb
|
43
|
+
- test/test_wordpress.rb
|
44
|
+
has_rdoc: false
|
45
|
+
homepage: http://github.com/jordandobson/wordpress/tree/master
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --main
|
49
|
+
- README.txt
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: wordpress
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
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."
|
71
|
+
test_files:
|
72
|
+
- test/test_wordpress.rb
|