jordandobson-posterous 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 CHANGED
@@ -1,19 +1,24 @@
1
- === 0.1.0 / 2009-05-19
1
+ === 0.1.5 / 2009-06-03
2
2
 
3
3
  * 1 major enhancement
4
4
 
5
- * Birthday!
6
-
5
+ * Moved Everything into a module and updated tests
6
+
7
+ === 0.1.3 / 2009-06-01
8
+
9
+ * 1 major enhancement
10
+
11
+ * Updated Readme and DRYed up a method to use symbols
12
+
7
13
  === 0.1.2 / 2009-05-29
8
14
 
9
15
  * 1 major enhancement
10
16
 
11
17
  * Updated the class to accept date, private, autopost, tags
12
18
 
13
-
14
- === 0.1.3 / 2009-06-01
19
+ === 0.1.0 / 2009-05-19
15
20
 
16
21
  * 1 major enhancement
17
22
 
18
- * Updated Readme and DRYed up a method to use symbols
19
-
23
+ * Birthday!
24
+
data/README.txt CHANGED
@@ -25,12 +25,12 @@ Posting images with posts, posting only images and pulling down your posts will
25
25
 
26
26
  * You can provide just the email and password
27
27
 
28
- account = Posterous.new('email_address', 'password')
28
+ account = Posterous::Client.new('email_address', 'password')
29
29
 
30
30
  * Or you can provide the ID as a string or integer
31
31
 
32
- account = Posterous.new('email_address', 'password', 68710)
33
- account = Posterous.new('email_address', 'password', '68710')
32
+ account = Posterous::Client.new('email_address', 'password', 68710)
33
+ account = Posterous::Client.new('email_address', 'password', '68710')
34
34
 
35
35
  2. Get more info about the user's account if you need it
36
36
 
@@ -44,11 +44,11 @@ Posting images with posts, posting only images and pulling down your posts will
44
44
 
45
45
  * Get the users primary site ID (In case they have multiple sites)
46
46
 
47
- account.get_primary_site
47
+ account.primary_site
48
48
 
49
49
  * Get a list of your sites and additional info
50
50
 
51
- account.get_account_info
51
+ account.account_info
52
52
 
53
53
  3. Setup your post with any or all of these optional fields
54
54
 
data/lib/posterous.rb CHANGED
@@ -1,107 +1,111 @@
1
1
  require 'rubygems'
2
2
  require 'httparty'
3
3
 
4
- class PosterousAuthError < StandardError; end
5
- class PosterousTagError < StandardError; end
6
- class PosterousSiteError < StandardError; end
7
-
8
- ###
9
- ### FUTURE PLANS
10
- ###
11
- # * Include media with your post
12
- # * Post only a media file and get a url for it back
13
- # * Allow reading in posterous posts
14
- # * Include more usage examples outside of readme
15
-
16
- class Posterous
17
-
18
- VERSION = '0.1.4'
19
- DOMAIN = 'posterous.com'
20
- POST_PATH = '/api/newpost'
21
- AUTH_PATH = '/api/getsites'
22
-
23
- include HTTParty
24
- base_uri DOMAIN
25
-
26
- attr_accessor :title, :body, :source, :source_url, :date, :tags
27
- attr_reader :site_id, :private_post, :autopost
28
-
29
- def initialize user, pass, site_id = nil
30
- raise PosterousAuthError, 'Either Username or Password is blank and/or not a string.' if \
31
- !user.is_a?(String) || !pass.is_a?(String) || user == "" || pass == ""
32
- self.class.basic_auth user, pass
33
- @site_id = site_id ? site_id.to_s : site_id
34
- @source = @body = @title = @source_url = @date = @media = @tags = @autopost = @private_post = nil
35
- end
4
+ module Posterous
36
5
 
37
- def site_id= id
38
- @site_id = id.to_s
39
- end
6
+ VERSION = '0.1.5'
7
+
8
+ class AuthError < StandardError; end
9
+ class TagError < StandardError; end
10
+ class SiteError < StandardError; end
11
+
12
+ ###
13
+ ### FUTURE PLANS
14
+ ###
15
+ # * Include media with your post
16
+ # * Post only a media file and get a url for it back
17
+ # * Allow reading in posterous posts
18
+ # * Include more usage examples outside of readme
19
+
20
+ class Client
21
+
22
+ DOMAIN = 'posterous.com'
23
+ POST_PATH = '/api/newpost'
24
+ AUTH_PATH = '/api/getsites'
40
25
 
41
- def tags= ary
42
- raise PosterousTagError, 'Tags must add from be in an array' if !ary.is_a?(Array)
43
- @tags = ary.join(", ")
44
- end
26
+ include HTTParty
27
+ base_uri DOMAIN
45
28
 
46
- def valid_user?
47
- res = get_account_info
48
- return false unless res.is_a?(Hash)
49
- res["stat"] == "ok" ? true : false
50
- end
29
+ attr_accessor :title, :body, :source, :source_url, :date
30
+ attr_reader :private_post, :autopost, :site_id, :tags
31
+
32
+ def initialize user, pass, site_id = nil
33
+ raise AuthError, 'Either Username or Password is blank and/or not a string.' if \
34
+ !user.is_a?(String) || !pass.is_a?(String) || user == "" || pass == ""
35
+ self.class.basic_auth user, pass
36
+ @site_id = site_id ? site_id.to_s : site_id
37
+ @source = @body = @title = @source_url = @date = @media = @tags = @autopost = @private_post = nil
38
+ end
39
+
40
+ def site_id= id
41
+ @site_id = id.to_s
42
+ end
43
+
44
+ def tags= ary
45
+ raise TagError, 'Tags must added using an array' if !ary.is_a?(Array)
46
+ @tags = ary.join(", ")
47
+ end
48
+
49
+ def valid_user?
50
+ res = account_info
51
+ return false unless res.is_a?(Hash)
52
+ res["stat"] == "ok"
53
+ end
51
54
 
52
- def has_site?
53
- res = get_account_info
54
- return false unless res.is_a?(Hash)
55
- if res["site"].is_a?(Hash)
56
- @site_id && @site_id == res["site"]["id"] || !@site_id ? true : false
57
- elsif res["site"].is_a?(Array)
58
- res["site"].each do |site|
59
- return true if @site_id && @site_id == site["id"]
55
+ def has_site?
56
+ res = account_info
57
+ return false unless res.is_a?(Hash)
58
+
59
+ case res["site"]
60
+ when Hash
61
+ return true unless @site_id
62
+ @site_id && @site_id == res["site"]["id"]
63
+ when Array
64
+ res["site"].each do |site|
65
+ return true if @site_id && @site_id == site["id"]
66
+ end
60
67
  end
61
68
  false
62
- else
63
- false
64
69
  end
65
- end
66
70
 
67
- def get_primary_site
68
- res = get_account_info
69
- raise PosterousSiteError, "Couldn't find a primary site. Check login and password is valid." \
70
- unless res.is_a?(Hash) && res["stat"] == "ok" && res["site"]
71
- site_list = res["site"].is_a?(Array) ? res["site"] : [res["site"]]
72
- site_list.each do |site|
73
- return site["id"] if site["primary"] == "true"
71
+ def primary_site
72
+ res = account_info
73
+ raise SiteError, "Couldn't find a primary site. Check login and password is valid." \
74
+ unless res.is_a?(Hash) && res["stat"] == "ok" && res["site"]
75
+ [res["site"]].flatten.each do |site|
76
+ return site["id"] if site["primary"] == "true"
77
+ end
78
+ nil
74
79
  end
75
- nil
76
- end
77
-
78
- def set_to on
79
- @private_post = 1 if on == :private
80
- @autopost = 1 if on == :autopost
81
- end
82
80
 
83
- def build_query
84
- options = { :site_id => @site_id,
85
- :autopost => @autopost,
86
- :private => @private_post,
87
- :date => @date,
88
- :tags => @tags }
81
+ def set_to on
82
+ @private_post = 1 if on == :private
83
+ @autopost = 1 if on == :autopost
84
+ end
89
85
 
90
- query = { :title => @title,
91
- :body => @body,
92
- :source => @source,
93
- :sourceLink => @source_url }
86
+ def build_query
87
+ options = { :site_id => @site_id,
88
+ :autopost => @autopost,
89
+ :private => @private_post,
90
+ :date => @date,
91
+ :tags => @tags }
94
92
 
95
- options.delete_if { |k,v| !v }
96
- query.merge!(options)
97
- end
93
+ query = { :title => @title,
94
+ :body => @body,
95
+ :source => @source,
96
+ :sourceLink => @source_url }
98
97
 
99
- def get_account_info
100
- self.class.post(AUTH_PATH, :query => {})["rsp"]
101
- end
102
-
103
- def add_post
104
- self.class.post(POST_PATH, :query => build_query)
98
+ options.delete_if { |k,v| !v }
99
+ query.merge!(options)
100
+ end
101
+
102
+ def account_info
103
+ self.class.post(AUTH_PATH, :query => {})["rsp"]
104
+ end
105
+
106
+ def add_post
107
+ self.class.post(POST_PATH, :query => build_query)
108
+ end
109
+
105
110
  end
106
-
107
111
  end
@@ -8,10 +8,10 @@ class TestPosterous < Test::Unit::TestCase
8
8
  @e = "email"
9
9
  @p = "password"
10
10
 
11
- @new_obj = Posterous.new(@e, @p)
12
- @new_obj_with_id = Posterous.new(@e, @p, "174966")
13
- @new_obj_with_bad_id = Posterous.new(@e, @p, "badID")
14
- @new_obj_with_invalid_id = Posterous.new(@e, @p, "666")
11
+ @new_obj = Posterous::Client.new(@e, @p)
12
+ @new_obj_with_id = Posterous::Client.new(@e, @p, "174966")
13
+ @new_obj_with_bad_id = Posterous::Client.new(@e, @p, "badID")
14
+ @new_obj_with_invalid_id = Posterous::Client.new(@e, @p, "666")
15
15
 
16
16
  @private_url_path = /[.]posterous[.]com\/private\//
17
17
 
@@ -52,21 +52,6 @@ class TestPosterous < Test::Unit::TestCase
52
52
  "id" => "174966" },
53
53
  "stat" => "ok" }
54
54
 
55
- @good_response_2_sites ={ "rsp" => {
56
- "site" => [{
57
- "name" => "ruby-posterous's posterous",
58
- "primary" => "true",
59
- "private" => "false",
60
- "url" => "http://ruby-posterous.posterous.com",
61
- "id" => "174966"
62
- }, {
63
- "name" => "uw-ruby",
64
- "primary" => "false",
65
- "private" => "false",
66
- "url" => "http://uwruby.posterous.com",
67
- "id" => "175260" }],
68
- "stat" => "ok" }}
69
-
70
55
  @bad_response ={ "err" => {
71
56
  "msg" => "Invalid Posterous email or password",
72
57
  "code" => "3001" },
@@ -113,109 +98,110 @@ class TestPosterous < Test::Unit::TestCase
113
98
  "id" => "891064",
114
99
  "longurl" => "http://glue.posterous.com/private/tGIEAateBy" },
115
100
  "stat" => "ok" }}
101
+
116
102
  end
117
103
 
118
104
  def test_raises_if_username_is_blank
119
- assert_raise PosterousAuthError do
120
- Posterous.new('', @p)
105
+ assert_raise Posterous::AuthError do
106
+ Posterous::Client.new('', @p)
121
107
  end
122
108
  end
123
109
 
124
110
  def test_raises_if_password_is_blank
125
- assert_raise PosterousAuthError do
126
- Posterous.new(@e, '')
111
+ assert_raise Posterous::AuthError do
112
+ Posterous::Client.new(@e, '')
127
113
  end
128
114
  end
129
115
 
130
116
  def test_raises_if_password_is_not_srting
131
- assert_raise PosterousAuthError do
132
- Posterous.new(@e, 666)
117
+ assert_raise Posterous::AuthError do
118
+ Posterous::Client.new(@e, 666)
133
119
  end
134
120
  end
135
121
 
136
122
  def test_raises_if_username_is_not_srting
137
- assert_raise PosterousAuthError do
138
- Posterous.new(666, @p)
123
+ assert_raise Posterous::AuthError do
124
+ Posterous::Client.new(666, @p)
139
125
  end
140
126
  end
141
127
 
142
128
  def test_site_id_can_be_witheld
143
- actual = Posterous.new(@e, @p)
129
+ actual = Posterous::Client.new(@e, @p)
144
130
  assert_equal nil, actual.site_id
145
131
  end
146
132
 
147
133
  def test_site_id_can_be_provided
148
- actual = Posterous.new(@e, @p, '174966')
134
+ actual = Posterous::Client.new(@e, @p, '174966')
149
135
  assert_equal '174966', actual.site_id
150
136
  end
151
137
 
152
138
  def test_site_id_is_converted_to_string
153
- actual = Posterous.new(@e, @p, 174966)
139
+ actual = Posterous::Client.new(@e, @p, 174966)
154
140
  assert_equal '174966', actual.site_id
155
- end
141
+ end
156
142
 
157
143
  def test_user_is_valid
158
- Posterous.stubs(:post).returns(@resp_ok)
144
+ Posterous::Client.stubs(:post).returns(@resp_ok)
159
145
  assert_equal true, @new_obj.valid_user?
160
146
  end
161
147
 
162
148
  def test_user_is_invalid
163
- Posterous.stubs(:post).returns(@resp_fail)
149
+ Posterous::Client.stubs(:post).returns(@resp_fail)
164
150
  assert_equal false, @new_obj.valid_user?
165
151
  end
166
152
 
167
153
  def test_user_is_invalid_when_response_isnt_hash
168
- Posterous.stubs(:post).returns("666")
154
+ Posterous::Client.stubs(:post).returns("666")
169
155
  assert_equal false, @new_obj.valid_user?
170
156
  end
171
157
 
172
158
  def test_ping_success_hash_adjustment
173
- Posterous.stubs(:post).returns(@resp_ok)
174
- assert_equal @good_response, @new_obj.get_account_info
159
+ Posterous::Client.stubs(:post).returns(@resp_ok)
160
+ assert_equal @good_response, @new_obj.account_info
175
161
  end
176
162
 
177
163
  def test_ping_fail_hash_adjustment
178
- Posterous.stubs(:post).returns(@resp_fail)
179
- assert_equal @bad_response, @new_obj.get_account_info
164
+ Posterous::Client.stubs(:post).returns(@resp_fail)
165
+ assert_equal @bad_response, @new_obj.account_info
180
166
  end
181
167
 
182
168
  def test_has_site_is_successful
183
- Posterous.stubs(:post).returns(@resp_ok)
169
+ Posterous::Client.stubs(:post).returns(@resp_ok)
184
170
  assert_equal true, @new_obj.has_site?
185
171
  end
186
172
 
187
173
  def test_has_site_successful_if_site_id_matches_only_result
188
- Posterous.stubs(:post).returns(@resp_ok_2_sites)
174
+ Posterous::Client.stubs(:post).returns(@resp_ok_2_sites)
189
175
  assert_equal true, @new_obj_with_id.has_site?
190
176
  end
191
177
 
192
178
  def test_has_site_fails_if_site_id_doesnt_match_only_result
193
- Posterous.stubs(:post).returns(@resp_ok)
179
+ Posterous::Client.stubs(:post).returns(@resp_ok)
194
180
  assert_equal false, @new_obj_with_bad_id.has_site?
195
181
  end
196
182
 
197
183
  def test_has_site_is_successful_on_multiple_when_specified
198
- Posterous.stubs(:post).returns(@resp_ok_2_sites)
184
+ Posterous::Client.stubs(:post).returns(@resp_ok_2_sites)
199
185
  assert_equal true, @new_obj_with_id.has_site?
200
186
  end
201
187
 
202
188
  def test_has_site_fails_if_specified_and_site_id_not_listed
203
- Posterous.stubs(:post).returns(@resp_ok_2_sites)
189
+ Posterous::Client.stubs(:post).returns(@resp_ok_2_sites)
204
190
  assert_equal false, @new_obj_with_bad_id.has_site?
205
191
  end
206
192
 
207
193
  def test_has_site_fails_when_multiple_and_site_not_specified
208
- Posterous.stubs(:post).returns(@resp_ok_2_sites)
194
+ Posterous::Client.stubs(:post).returns(@resp_ok_2_sites)
209
195
  assert_equal false, @new_obj.has_site?
210
196
  end
211
197
 
212
198
  def test_has_site_fails_with_error_response
213
- Posterous.stubs(:post).returns(@resp_fail)
199
+ Posterous::Client.stubs(:post).returns(@resp_fail)
214
200
  assert_equal false, @new_obj.has_site?
215
201
  end
216
202
 
217
203
  def test_has_site_fails_if_response_isnt_hash
218
- Posterous.stubs(:post).returns("666")
204
+ Posterous::Client.stubs(:post).returns("666")
219
205
  assert_equal false, @new_obj.has_site?
220
206
  end
221
207
 
@@ -235,41 +221,41 @@ class TestPosterous < Test::Unit::TestCase
235
221
  end
236
222
 
237
223
  def test_raises_if_tags_not_set_as_array
238
- assert_raise PosterousTagError do
224
+ assert_raise Posterous::TagError do
239
225
  @new_obj.tags = "hello, "
240
226
  end
241
227
  end
242
228
 
243
229
  def test_gets_primary_single_site
244
- Posterous.stubs(:post).returns(@resp_ok)
245
- assert_equal @resp_ok["rsp"]["site"]["id"], @new_obj.get_primary_site
230
+ Posterous::Client.stubs(:post).returns(@resp_ok)
231
+ assert_equal @resp_ok["rsp"]["site"]["id"], @new_obj.primary_site
246
232
  end
247
233
 
248
234
  def test_gets_primary_site_from_multiple_listing
249
- Posterous.stubs(:post).returns(@resp_ok_2_sites)
250
- assert_equal @resp_ok_2_sites["rsp"]["site"][1]["id"], @new_obj.get_primary_site
235
+ Posterous::Client.stubs(:post).returns(@resp_ok_2_sites)
236
+ assert_equal @resp_ok_2_sites["rsp"]["site"][1]["id"], @new_obj.primary_site
251
237
  end
252
238
 
253
239
  def test_gets_primary_site_raises_on_error
254
- Posterous.stubs(:post).returns(@resp_fail)
255
- assert_raise PosterousSiteError do
256
- @new_obj.get_primary_site
240
+ Posterous::Client.stubs(:post).returns(@resp_fail)
241
+ assert_raise Posterous::SiteError do
242
+ @new_obj.primary_site
257
243
  end
258
244
  end
259
245
 
260
246
  def test_gets_primary_site_is_passed_to_overide_site_id
261
- Posterous.stubs(:post).returns(@resp_ok_2_sites)
247
+ Posterous::Client.stubs(:post).returns(@resp_ok_2_sites)
262
248
  original = @new_obj_with_id.site_id
263
- @new_obj_with_id.site_id = @new_obj_with_id.get_primary_site
249
+ @new_obj_with_id.site_id = @new_obj_with_id.primary_site
264
250
  updated = @new_obj_with_id.site_id
265
251
  assert_not_equal original, updated
266
252
  assert_equal @resp_ok_2_sites["rsp"]["site"][1]["id"], updated
267
253
  end
268
254
 
269
255
  def test_gets_primary_site_is_set_to_site_id
270
- Posterous.stubs(:post).returns(@resp_ok)
256
+ Posterous::Client.stubs(:post).returns(@resp_ok)
271
257
  original = @new_obj.site_id
272
- @new_obj.site_id = @new_obj.get_primary_site
258
+ @new_obj.site_id = @new_obj.primary_site
273
259
  updated = @new_obj_with_id.site_id
274
260
  assert_not_equal original, updated
275
261
  assert_equal @resp_ok["rsp"]["site"]["id"], updated
@@ -338,13 +324,13 @@ class TestPosterous < Test::Unit::TestCase
338
324
  end
339
325
 
340
326
  def test_add_post_successful
341
- Posterous.stubs(:post).returns(@post_success)
327
+ Posterous::Client.stubs(:post).returns(@post_success)
342
328
  expected = @post_success
343
329
  assert_equal expected, @new_obj.add_post
344
330
  end
345
331
 
346
332
  def test_add_post_successful_no_content
347
- Posterous.stubs(:post).returns(@post_success)
333
+ Posterous::Client.stubs(:post).returns(@post_success)
348
334
  actual = @new_obj.add_post
349
335
  assert actual["rsp"]["post"].is_a?(Hash)
350
336
  assert_equal "ok", actual["rsp"]["stat"]
@@ -352,14 +338,14 @@ class TestPosterous < Test::Unit::TestCase
352
338
  end
353
339
 
354
340
  def test_add_post_successful_with_title_content
355
- Posterous.stubs(:post).returns(@post_title_success)
341
+ Posterous::Client.stubs(:post).returns(@post_title_success)
356
342
  @new_obj.title = "My Title"
357
343
  actual = @new_obj.add_post
358
344
  assert_equal "My Title", actual["rsp"]["post"]["title"]
359
345
  end
360
346
 
361
347
  def test_add_post_invalid_site_id_fails
362
- Posterous.stubs(:post).returns(@post_invalid_site)
348
+ Posterous::Client.stubs(:post).returns(@post_invalid_site)
363
349
  actual = @new_obj_with_bad_id.add_post
364
350
  assert_equal "fail", actual["rsp"]["stat"]
365
351
  assert_equal nil, actual["rsp"]["post"]
@@ -367,7 +353,7 @@ class TestPosterous < Test::Unit::TestCase
367
353
  end
368
354
 
369
355
  def test_add_post_no_access_fails
370
- Posterous.stubs(:post).returns(@post_access_error)
356
+ Posterous::Client.stubs(:post).returns(@post_access_error)
371
357
  actual = @new_obj_with_invalid_id.add_post
372
358
  assert_equal "fail", actual["rsp"]["stat"]
373
359
  assert_equal nil, actual["rsp"]["post"]
@@ -375,22 +361,22 @@ class TestPosterous < Test::Unit::TestCase
375
361
  end
376
362
 
377
363
  def test_add_post_invalid_account_info
378
- Posterous.stubs(:post).returns(@post_bad_account)
379
- actual = Posterous.new("666", "666").add_post
364
+ Posterous::Client.stubs(:post).returns(@post_bad_account)
365
+ actual = Posterous::Client.new("666", "666").add_post
380
366
  assert_equal "fail", actual["rsp"]["stat"]
381
367
  assert_equal nil, actual["rsp"]["post"]
382
368
  assert_match "Invalid Posterous", actual["rsp"]["err"]["msg"]
383
369
  end
384
370
 
385
371
  def test_add_post_is_private_by_default
386
- Posterous.stubs(:post).returns(@post_success)
372
+ Posterous::Client.stubs(:post).returns(@post_success)
387
373
  actual = @new_obj.add_post
388
374
  assert_equal "ok", actual["rsp"]["stat"]
389
375
  assert_no_match @private_url_path, actual["rsp"]["post"]["longurl"]
390
376
  end
391
377
 
392
378
  def test_add_post_is_made_private
393
- Posterous.stubs(:post).returns(@post_private_good)
379
+ Posterous::Client.stubs(:post).returns(@post_private_good)
394
380
  @new_obj.set_to :private
395
381
  actual = @new_obj.add_post
396
382
  assert_equal "ok", actual["rsp"]["stat"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jordandobson-posterous
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Dobson