tumblr-rb 0.0.1 → 0.1.0

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.
@@ -0,0 +1,48 @@
1
+ ---
2
+ - !ruby/struct:VCR::RecordedResponse
3
+ method: :get
4
+ uri: http://mwunsch.tumblr.com:80/api/read/?type=video&num=5
5
+ response: !ruby/object:Net::HTTPOK
6
+ body: |
7
+ <?xml version="1.0" encoding="UTF-8"?>
8
+ <tumblr version="1.0"><tumblelog name="mwunsch" timezone="US/Eastern" title="M. Wunsch">Collected visual and aural interests of &lt;a href="http://markwunsch.com" rel="me"&gt;Mark Wunsch&lt;/a&gt;.</tumblelog><posts type="video" start="0" total="33"><post id="385851242" url="http://mwunsch.tumblr.com/post/385851242" url-with-slug="http://mwunsch.tumblr.com/post/385851242/eric-royer-a-one-man-band-performs-shortnin" type="video" date-gmt="2010-02-12 19:00:00 GMT" date="Fri, 12 Feb 2010 14:00:00" unix-timestamp="1266001200" format="markdown" reblog-key="gymWGGbw" slug="eric-royer-a-one-man-band-performs-shortnin"><video-caption>&lt;p&gt;&lt;a href="http://guitarmachine.com/"&gt;Eric Royer&lt;/a&gt;, a one-man band performs &lt;a href="http://en.wikipedia.org/wiki/Shortnin%27_Bread"&gt;&lt;em&gt;Shortnin&amp;#8217; Bread&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;</video-caption><video-source>http://www.youtube.com/watch?v=i7CgL7c4LeE</video-source><video-player>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/i7CgL7c4LeE&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/i7CgL7c4LeE&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;</video-player><tag>one-man-band</tag></post><post id="380262584" url="http://mwunsch.tumblr.com/post/380262584" url-with-slug="http://mwunsch.tumblr.com/post/380262584/tiger-rag-performed-by-professor-gizmo-one-man" type="video" date-gmt="2010-02-09 18:00:00 GMT" date="Tue, 09 Feb 2010 13:00:00" unix-timestamp="1265738400" format="markdown" reblog-key="EJbEaOC4" slug="tiger-rag-performed-by-professor-gizmo-one-man"><video-caption>&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Tiger_Rag"&gt;&lt;em&gt;Tiger Rag&lt;/em&gt;&lt;/a&gt;, performed by Professor Gizmo, one-man band.&lt;/p&gt;</video-caption><video-source>http://www.youtube.com/watch?v=azb9kN1fgwc</video-source><video-player>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/azb9kN1fgwc&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/azb9kN1fgwc&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;</video-player><tag>one-man-band</tag></post><post id="378242257" url="http://mwunsch.tumblr.com/post/378242257" url-with-slug="http://mwunsch.tumblr.com/post/378242257/cigo-man-band-a-one-man-band-performing-in" type="video" date-gmt="2010-02-08 16:00:00 GMT" date="Mon, 08 Feb 2010 11:00:00" unix-timestamp="1265644800" format="markdown" reblog-key="KEeTYx6M" slug="cigo-man-band-a-one-man-band-performing-in"><video-caption>&lt;p&gt;Cigo Man Band, a &lt;a href="http://en.wikipedia.org/wiki/One-man_band"&gt;one-man band&lt;/a&gt;, performing in Croatia&lt;/p&gt;</video-caption><video-source>http://www.youtube.com/watch?v=uXMuWi0dUBc</video-source><video-player>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/uXMuWi0dUBc&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/uXMuWi0dUBc&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;</video-player><tag>one-man-band</tag></post><post id="372768179" url="http://mwunsch.tumblr.com/post/372768179" url-with-slug="http://mwunsch.tumblr.com/post/372768179/flight-of-the-bumblebee-by-nikolai" type="video" date-gmt="2010-02-05 20:15:00 GMT" date="Fri, 05 Feb 2010 15:15:00" unix-timestamp="1265400900" format="markdown" reblog-key="C8oKiE8V" slug="flight-of-the-bumblebee-by-nikolai"><video-caption>&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Flight_of_the_Bumblebee"&gt;Flight of the Bumblebee&lt;/a&gt; by Nikolai Rimsky-Korsakov. On an accordion. &lt;a href="http://mwunsch.tumblr.com/tagged/shredding-on-an-accordion"&gt;WTF&lt;/a&gt;.&lt;/p&gt;</video-caption><video-source>http://www.youtube.com/watch?v=j2saw900LdU</video-source><video-player>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/j2saw900LdU&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/j2saw900LdU&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;</video-player><tag>shredding-on-an-accordion</tag></post><post id="354629654" url="http://mwunsch.tumblr.com/post/354629654" url-with-slug="http://mwunsch.tumblr.com/post/354629654/when-i-first-switched-to-apple-computers-in-2001" type="video" date-gmt="2010-01-26 17:57:36 GMT" date="Tue, 26 Jan 2010 12:57:36" unix-timestamp="1264528656" format="markdown" reblog-key="xp1zSaiE" slug="when-i-first-switched-to-apple-computers-in-2001"><video-caption>&lt;p&gt;When I first switched to Apple computers in 2001, I desperately wanted a G4 Cube. Unfortunately, my enthusiasm for the Mac came right at the end of the Cube&amp;#8217;s life cycle, and I ended up choosing a Power Mac G4 (which is still happily running).&lt;/p&gt;
9
+
10
+ &lt;p&gt;What&amp;#8217;s fascinating to me about this video is that this computer is so beautiful &amp;#8212; the height of industrial design (belongs in &lt;a href="http://www.moma.org/collection/browse_results.php?criteria=O%3AAD%3AE%3A22559&amp;amp;page_number=1&amp;amp;template_id=6&amp;amp;sort_order=1"&gt;a museum&lt;/a&gt;); yet this product failed to take off, presumably because it was only filler &amp;#8212; a middle ground between the big desktop computer and the laptop. It didn&amp;#8217;t solve any problems.&lt;/p&gt;
11
+
12
+ &lt;p&gt;I have the same apprehensions about a Rumored Apple Tablet Device. Will it be a beautiful thing that only fills middle ground between a laptop computer and a mobile device? Apple has been down that road before.&lt;/p&gt;
13
+
14
+ &lt;p&gt;My favorite part of this video is shortly after &lt;a href="http://www.youtube.com/watch?v=D0NbGbZBPL0#t=4m40s"&gt;4:40&lt;/a&gt;, where Steve reveals how to extract the innards of the Cube.&lt;/p&gt;</video-caption><video-source>http://www.youtube.com/watch?v=D0NbGbZBPL0</video-source><video-player>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/D0NbGbZBPL0&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/D0NbGbZBPL0&amp;amp;rel=0&amp;amp;egm=0&amp;amp;showinfo=0&amp;amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;</video-player><tag>Apple</tag></post></posts></tumblr>
15
+
16
+ body_exist: true
17
+ code: "200"
18
+ header:
19
+ last-modified:
20
+ - Fri, 05 Mar 2010 17:12:57 GMT
21
+ p3p:
22
+ - CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
23
+ content-type:
24
+ - text/xml; charset=UTF-8
25
+ connection:
26
+ - close
27
+ x-tumblr-usec:
28
+ - D=59314
29
+ x-robots-tag:
30
+ - noindex
31
+ server:
32
+ - Apache/2.2.3 (CentOS)
33
+ date:
34
+ - Sat, 06 Mar 2010 12:06:18 GMT
35
+ content-length:
36
+ - "7776"
37
+ x-cache-auto:
38
+ - miss
39
+ x-tumblr-perf:
40
+ - "\"ch:6/rate-limiter,tumblelog_id_for_host,tumblelogs,country_code_for_ip,rate-limiter,rate-limiter cm:1/auto ce:0/ c:0/0 d:0/0 e:0/0\""
41
+ vary:
42
+ - Accept-Encoding
43
+ cache-control:
44
+ - max-age=900
45
+ http_version: "1.1"
46
+ message: OK
47
+ read: true
48
+ socket:
@@ -0,0 +1,31 @@
1
+ ---
2
+ - !ruby/struct:VCR::RecordedResponse
3
+ method: :post
4
+ uri: http://www.tumblr.com:80/api/delete
5
+ response: !ruby/object:Net::HTTPOK
6
+ body: Deleted
7
+ body_exist: true
8
+ code: "200"
9
+ header:
10
+ p3p:
11
+ - CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
12
+ content-type:
13
+ - text/plain; charset=utf-8
14
+ connection:
15
+ - close
16
+ x-tumblr-usec:
17
+ - D=109741
18
+ server:
19
+ - Apache/2.2.3 (Red Hat)
20
+ date:
21
+ - Sun, 07 Mar 2010 07:09:10 GMT
22
+ content-length:
23
+ - "7"
24
+ x-tumblr-perf:
25
+ - "\"ch:7/country_code_for_ip,rate-limiter,posts,tumblelogs,version,users,users cm:2/rate-limiter,rate-limiter ce:0/ c:0/0 d:0/0 e:0/0\""
26
+ vary:
27
+ - Accept-Encoding
28
+ http_version: "1.1"
29
+ message: OK
30
+ read: true
31
+ socket:
@@ -0,0 +1,31 @@
1
+ ---
2
+ - !ruby/struct:VCR::RecordedResponse
3
+ method: :post
4
+ uri: http://www.tumblr.com:80/api/write
5
+ response: !ruby/object:Net::HTTPCreated
6
+ body: "431830023"
7
+ body_exist: true
8
+ code: "201"
9
+ header:
10
+ p3p:
11
+ - CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
12
+ content-type:
13
+ - text/plain; charset=utf-8
14
+ connection:
15
+ - close
16
+ x-tumblr-usec:
17
+ - D=128043
18
+ server:
19
+ - Apache/2.2.3 (CentOS)
20
+ date:
21
+ - Sun, 07 Mar 2010 07:01:22 GMT
22
+ content-length:
23
+ - "9"
24
+ x-tumblr-perf:
25
+ - "\"ch:7/country_code_for_ip,rate-limiter,posts,tumblelogs,version,users,users cm:2/rate-limiter,rate-limiter ce:0/ c:0/0 d:0/0 e:0/0\""
26
+ vary:
27
+ - Accept-Encoding
28
+ http_version: "1.1"
29
+ message: Created
30
+ read: true
31
+ socket:
@@ -0,0 +1,31 @@
1
+ ---
2
+ - !ruby/struct:VCR::RecordedResponse
3
+ method: :post
4
+ uri: http://www.tumblr.com:80/api/write
5
+ response: !ruby/object:Net::HTTPCreated
6
+ body: "431830023"
7
+ body_exist: true
8
+ code: "201"
9
+ header:
10
+ p3p:
11
+ - CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
12
+ content-type:
13
+ - text/plain; charset=utf-8
14
+ connection:
15
+ - close
16
+ x-tumblr-usec:
17
+ - D=137805
18
+ server:
19
+ - Apache/2.2.3 (Red Hat)
20
+ date:
21
+ - Sun, 07 Mar 2010 06:20:29 GMT
22
+ content-length:
23
+ - "9"
24
+ x-tumblr-perf:
25
+ - "\"ch:8/country_code_for_ip,rate-limiter,tumblelog_id_for_host,tumblelogs,users,version,users,users cm:2/rate-limiter,rate-limiter ce:0/ c:0/0 d:0/0 e:0/0\""
26
+ vary:
27
+ - Accept-Encoding
28
+ http_version: "1.1"
29
+ message: Created
30
+ read: true
31
+ socket:
data/test/helper.rb CHANGED
@@ -8,8 +8,11 @@ rescue LoadError
8
8
  Bundler.setup
9
9
  end
10
10
 
11
+ require 'pp'
11
12
  require 'test/unit'
13
+ require 'contest'
12
14
  require 'redgreen'
15
+ require 'vcr'
13
16
 
14
17
  begin
15
18
  require 'tumblr'
@@ -18,3 +21,20 @@ rescue LoadError
18
21
  $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
19
22
  require 'tumblr'
20
23
  end
24
+
25
+ FakeWeb.allow_net_connect = false
26
+
27
+ VCR.config do |c|
28
+ # the cache_dir is where the cassette yml files will be saved.
29
+ c.cache_dir = File.join(File.dirname(__FILE__),'fixtures', 'vcr_cassettes')
30
+
31
+ # this record mode will be used for any cassette you create without specifying a record mode.
32
+ c.default_cassette_record_mode = :none
33
+ end
34
+
35
+ def hijack!(request, fixture)
36
+ record_mode = File.exist?(VCR::Cassette.new(fixture).cache_file) ? :none : :unregistered
37
+ VCR.with_cassette(fixture, :record => record_mode) do
38
+ request.perform
39
+ end
40
+ end
data/test/test_tumblr.rb CHANGED
@@ -1,7 +1,596 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestTumblr < Test::Unit::TestCase
4
- def test_something_for_real
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
4
+ describe 'Tumblr' do
5
+ test 'maps post types to the right class' do
6
+ assert_respond_to Tumblr, :map
7
+ assert_equal Tumblr::Post::Photo, Tumblr.map(:photo)
8
+ assert_raise RuntimeError do
9
+ Tumblr.map(:foobar)
10
+ end
11
+ end
12
+
13
+ test 'parses a post out of a document' do
14
+ klass = Class.new Tumblr::Post
15
+ klass.parameters :title, :body
16
+ type = :regular
17
+ post = klass.new
18
+ post.instance_variable_set(:@type,type)
19
+ post.body = "Hello world."
20
+ post.title = "Regular post"
21
+ document = post.to_s
22
+ assert Tumblr.parse(document).is_a? Tumblr.map(type)
23
+ assert_equal post.body, Tumblr.parse(document).body
24
+ assert_equal post.to_h, Tumblr.parse(document).to_h
25
+ assert_equal post.title, Tumblr.parse(document).title
26
+ end
27
+
28
+ test 'parses a document and sets up basic post' do
29
+ klass = Class.new Tumblr::Post
30
+ klass.parameters :title, :body
31
+ type = :regular
32
+ post = klass.new('123')
33
+ post.instance_variable_set(:@type,type)
34
+ post.tags 'hello', 'stuff'
35
+ post.state = :queue
36
+ post.format = :markdown
37
+ post.send_to_twitter 'Hi from tumblr'
38
+ post.publish_on 'tuesday'
39
+ post.date = Time.now.iso8601
40
+ post.body = "Hello world."
41
+ post.generator = Tumblr::GENERATOR
42
+ post.private = true
43
+ post.group = 'tumblrgemtest.tumblr.com'
44
+ document = post.to_s
45
+
46
+ %w(post_id format state tags send_to_twitter publish_on date generator private? group).each do |attribute|
47
+ assert_equal post.send(attribute), Tumblr.parse(document).send(attribute)
48
+ end
49
+ end
50
+
51
+ test 'infers a post type from a document if none is explicitly set' do
52
+ link = 'http://something.com'
53
+ video = 'http://www.youtube.com/watch?v=CW0DUg63lqU'
54
+ text = "Hello world"
55
+ assert_equal :video, Tumblr.infer_post_type(video)
56
+ assert_equal :link, Tumblr.infer_post_type(link)
57
+ assert_equal :regular, Tumblr.infer_post_type(text)
58
+ end
59
+
60
+ test 'parses a document' do
61
+ link = <<-link
62
+ ---
63
+ name: The Something Website
64
+ ---
65
+ http://something.com
66
+ link
67
+
68
+ post = Tumblr.parse(link)
69
+ assert_equal :link, post.type
70
+ assert_equal 'The Something Website', post.name
71
+ assert_equal 'http://something.com', post.url
72
+ end
73
+
74
+ test 'sets up credentials for authentication' do
75
+ params = {:email => 'test@testermcgee.com', :password => 'dontrevealmysecrets'}
76
+ credentials = Tumblr.new(params[:email],params[:password]).instance_variable_get(:@credentials)
77
+ assert credentials.has_key? :email
78
+ assert credentials.has_key? :password
79
+ assert_equal params, credentials
80
+ end
81
+
82
+ test 'can read posts' do
83
+ assert Tumblr.new.read('mwunsch').is_a? Weary::Request
84
+ end
85
+
86
+ test 'can publish posts to tumblr' do
87
+ tumbl = Tumblr.new('test@testermcgee.com','dontrevealmysecrets')
88
+ write = tumbl.post('Hello world')
89
+ assert write.is_a? Weary::Request
90
+ end
91
+ end
92
+
93
+ describe 'Reader' do
94
+ test 'sets up credentials for authentication' do
95
+ reader = Tumblr::Reader
96
+ assert !reader.new.defaults
97
+ params = {:email => 'test@testermcgee.com', :password => 'dontrevealmysecrets'}
98
+ credentials = reader.new(params[:email],params[:password]).defaults
99
+ assert credentials.has_key? :email
100
+ assert credentials.has_key? :password
101
+ assert_equal params, credentials
102
+ end
103
+
104
+ test 'handles parameters for reads' do
105
+ reader = Tumblr::Reader
106
+ options = {:start => 5, :num => 10, :foo => 'Bar', :type => 'video'}
107
+ cred = {:email => 'test@testermcgee.com', :password => 'dontrevealmysecrets'}
108
+ assert !reader.new.parameters(options).has_key?(:foo)
109
+ assert_equal cred[:email], reader.new(cred[:email],cred[:password]).parameters(options)[:email]
110
+ assert reader.new(*cred.values).parameters(options).has_key?(:password)
111
+ end
112
+
113
+ test 'convenience method for reads' do
114
+ t = Tumblr::Reader
115
+ assert_respond_to t, :read
116
+ assert_equal :get, t.read('mwunsch').via
117
+ assert_equal 'http://mwunsch.tumblr.com/api/read/', t.read('mwunsch').uri.normalize.to_s
118
+ assert_equal :post, t.read('mwunsch',:post).via
119
+ params = {:email => 'test@testermcgee.com', :password => 'dontrevealmysecrets'}
120
+ assert_equal params.to_params, t.read('mwunsch',:post,params).with
121
+ end
122
+
123
+ test 'reads posts' do
124
+ reader = Tumblr::Reader
125
+ mwunsch = reader.new.read('mwunsch')
126
+ assert_respond_to mwunsch, :perform
127
+ assert_equal 'http://mwunsch.tumblr.com/api/read/', mwunsch.uri.normalize.to_s
128
+ response = hijack! mwunsch, 'read/mwunsch'
129
+ assert response.success?
130
+ assert_equal :xml, response.format
131
+ end
132
+
133
+ test 'reads posts with some optional parameters' do
134
+ reader = Tumblr::Reader
135
+ options = {:num => 5, :type => :video}
136
+ posts = reader.new.read 'mwunsch', options
137
+ response = hijack! posts, 'read/optional'
138
+ parsed = response.parse["tumblr"]["posts"]
139
+ assert_equal "video", parsed["type"]
140
+ assert_equal 5, parsed['post'].count
141
+ end
142
+
143
+ test 'attempts to perform an authenticated read' do
144
+ reader = Tumblr::Reader
145
+ auth = reader.new('test@testermcgee.com','dontrevealmysecrets').authenticated_read('mwunsch',{:state => :draft})
146
+ response = hijack! auth, 'read/authenticated'
147
+ assert_equal '420292045', response['tumblr']['posts']['post']['id']
148
+ end
149
+
150
+ test 'sometimes authentication fails' do
151
+ reader = Tumblr::Reader
152
+ auth = reader.new('test@testermcgee.com','dontrevealmysecrets').authenticated_read('mwunsch')
153
+ response = hijack! auth, 'read/authentication failure'
154
+ assert !response.success?
155
+ assert_equal 403, response.code
156
+ end
157
+
158
+ test 'can not do an authenticated read without credentials' do
159
+ reader = Tumblr::Reader
160
+ assert_raise RuntimeError do
161
+ reader.new.authenticated_read('mwunsch')
162
+ end
163
+ end
164
+
165
+ # need to test these more thoroughly
166
+ test 'has several other authenticated read methods' do
167
+ reader = Tumblr::Reader.new
168
+ assert_respond_to reader, :dashboard
169
+ assert_respond_to reader, :likes
170
+ assert_respond_to reader, :like
171
+ assert_respond_to reader, :unlike
172
+ end
173
+
174
+ test 'parses posts out of a read' do
175
+ reader = Tumblr::Reader
176
+ assert_respond_to reader, :get_posts
177
+ mwunsch = reader.new.read('mwunsch')
178
+ response = hijack! mwunsch, 'read/mwunsch'
179
+ assert_equal response['tumblr']['posts']['post'].count, reader.get_posts(response).count
180
+ assert reader.get_posts(response).first.is_a? Tumblr::Post::Quote
181
+ end
182
+
183
+ test 'selects posts by type' do
184
+ reader = Tumblr::Reader
185
+ assert_respond_to reader, :get_posts
186
+ mwunsch = reader.new.read('mwunsch')
187
+ response = hijack! mwunsch, 'read/mwunsch'
188
+ assert reader.get_posts(response, :link).first.is_a? Tumblr::Post::Link
189
+ end
190
+
191
+ test 'generates a Post object from a parsed post' do
192
+ reader = Tumblr::Reader
193
+ assert_respond_to reader, :build_post
194
+ mwunsch = reader.new.read('mwunsch')
195
+ response = hijack! mwunsch, 'read/mwunsch'
196
+ posts = response['tumblr']['posts']['post']
197
+ link = posts.select {|post| post['type'].eql?('link') }
198
+ link_post = reader.build_post(link.first)
199
+ assert link_post.is_a? Tumblr::Post::Link
200
+ assert_equal :link, link_post.type
201
+ assert_equal :markdown, link_post.format
202
+ assert_equal link.first['link_url'], link_post.url
203
+ end
204
+ end
205
+
206
+ describe 'Writer' do
207
+ test 'sets up credentials for authentication' do
208
+ writer = Tumblr::Writer
209
+ params = {:email => 'test@testermcgee.com', :password => 'dontrevealmysecrets'}
210
+ credentials = writer.new(params[:email],params[:password]).defaults
211
+ assert credentials.has_key? :email
212
+ assert credentials.has_key? :password
213
+ end
214
+
215
+ test 'writes a post' do
216
+ writer = Tumblr::Writer
217
+ assert_respond_to writer.new, :write
218
+ post = {:type => :regular, :body => 'Hello world.', :group => 'tumblrgemtest.tumblr.com'}
219
+ publisher = Tumblr::Writer.new('test@testermcgee.com','dontrevealmysecrets')
220
+ response = hijack! publisher.write(post), 'write/write'
221
+ assert_equal 201, response.code
222
+ end
223
+
224
+ test 'edits an existing post' do
225
+ assert_respond_to Tumblr::Writer.new, :edit
226
+ publisher = Tumblr::Writer.new('test@testermcgee.com','dontrevealmysecrets')
227
+ post = {:'post-id' => "431830023", :body => 'Hello world?'}
228
+ response = hijack! publisher.edit(post), 'write/edit'
229
+ assert_equal 201, response.code
230
+ end
231
+
232
+ test 'deletes a post' do
233
+ assert_respond_to Tumblr::Writer.new, :edit
234
+ publisher = Tumblr::Writer.new('test@testermcgee.com','dontrevealmysecrets')
235
+ post = {:'post-id' => "431830023"}
236
+ response = hijack! publisher.delete(post), 'write/delete'
237
+ assert response.success?
238
+ assert_equal 'Deleted', response.body
239
+ end
240
+ end
241
+
242
+ describe 'Authenticator' do
243
+ test 'sets up credentials for authentication' do
244
+ user = Tumblr::Authenticator
245
+ params = {:email => 'test@testermcgee.com', :password => 'dontrevealmysecrets'}
246
+ credentials = user.new(params[:email],params[:password]).defaults
247
+ assert credentials.has_key? :email
248
+ assert credentials.has_key? :password
249
+ end
250
+
251
+ test 'authenticates a user to get information' do
252
+ user = Tumblr::Authenticator.new('test@testermcgee.com','dontrevealmysecrets')
253
+ assert_respond_to user, :authenticate
254
+ response = hijack! user.authenticate, 'authenticate/authenticate'
255
+ assert response.success?
256
+ assert_equal 'mwunsch', response["tumblr"]["tumblelog"].first["name"]
257
+ end
258
+ end
259
+
260
+ describe 'Post' do
261
+ describe 'Basic' do
262
+ test 'has a set of post-specific parameters' do
263
+ klass = Class.new(Tumblr::Post)
264
+ assert_respond_to klass, :parameters
265
+ klass.parameters :title, :body
266
+ assert klass.parameters.include? :title
267
+ post = klass.new
268
+ assert_respond_to post, :title
269
+ post.title = 'Hello world'
270
+ assert_equal 'Hello world', post.title
271
+ end
272
+
273
+ test 'can have a post_id already set' do
274
+ post = Tumblr::Post.new
275
+ assert !post.post_id
276
+ post = Tumblr::Post.new 123
277
+ assert_equal 123, post.post_id
278
+ end
279
+
280
+ test 'sets a date for publishing in the past' do
281
+ post = Tumblr::Post.new
282
+ assert_respond_to post, :date
283
+ right_now = Time.now.iso8601
284
+ post.date = right_now
285
+ assert_equal right_now, post.date
286
+ end
287
+
288
+ test 'can be a private post' do
289
+ post = Tumblr::Post.new
290
+ assert_respond_to post, :private?
291
+ assert !post.private?
292
+ post.private = true
293
+ assert post.private?
294
+ end
295
+
296
+ test 'has a comma separated list of tags' do
297
+ post = Tumblr::Post.new
298
+ assert_respond_to post, :tags
299
+ post.tags :tumblr, :whatever, :ruby
300
+ assert_equal 'tumblr,whatever,ruby', post.tags
301
+ end
302
+
303
+ test 'can set its format to be html or markdown' do
304
+ post = Tumblr::Post.new
305
+ assert_respond_to post, :format
306
+ post.format = :foobar
307
+ assert !post.format
308
+ post.format = :markdown
309
+ assert_equal :markdown, post.format
310
+ end
311
+
312
+ test 'can set this post to be published to a secondary blog' do
313
+ post = Tumblr::Post.new
314
+ assert_respond_to post, :group
315
+ post.group = 'mygroup.tumblr.com'
316
+ assert_equal 'mygroup.tumblr.com', post.group
317
+ end
318
+
319
+ test 'sets a slug for its url' do
320
+ post = Tumblr::Post.new
321
+ assert_respond_to post, :slug
322
+ post.slug = "this-string-right-here"
323
+ assert_equal "this-string-right-here", post.slug
324
+ end
325
+
326
+ test 'can change published state' do
327
+ post = Tumblr::Post.new
328
+ assert_respond_to post, :state
329
+ post.state = 'queue'
330
+ assert_equal :queue, post.state
331
+ assert_raise RuntimeError do
332
+ post.state = 'foobar'
333
+ end
334
+ end
335
+
336
+ test 'sends to twitter' do
337
+ post = Tumblr::Post.new
338
+ assert_respond_to post, :send_to_twitter
339
+ assert !post.send_to_twitter
340
+ post.send_to_twitter :no
341
+ assert !post.send_to_twitter
342
+ post.send_to_twitter 'Updating twitter through tumblr'
343
+ assert_equal 'Updating twitter through tumblr', post.send_to_twitter
344
+ end
345
+
346
+ test 'if the published state is in the queue, specify a publish date' do
347
+ post = Tumblr::Post.new
348
+ assert_respond_to post, :publish_on
349
+ right_now = Time.now.iso8601
350
+ post.publish_on right_now
351
+ assert !post.publish_on
352
+ post.state = :queue
353
+ post.publish_on right_now
354
+ assert_equal right_now, post.publish_on
355
+ end
356
+
357
+ test 'converts to a hash' do
358
+ post = Tumblr::Post.new(123)
359
+ post.private = 1
360
+ assert_respond_to post, :to_h
361
+ assert_equal 1, post.to_h[:private]
362
+ assert_equal 123, post.to_h[:'post-id']
363
+ klass = Class.new(post.class)
364
+ klass.parameters :title, :body
365
+ new_post = klass.new(456)
366
+ new_post.title = "Hello world"
367
+ assert_equal 'Hello world', new_post.to_h[:title]
368
+ assert !new_post.to_h.has_key?(:body)
369
+ end
370
+
371
+ test 'writes itself to tumblr' do
372
+ klass = Class.new Tumblr::Post
373
+ post = klass.new
374
+ post.instance_variable_set(:@type,:regular)
375
+ assert post.write('test@testermcgee.com','dontrevealmysecrets').is_a? Weary::Request
376
+ end
377
+
378
+ test 'edits itself on tumblr' do
379
+ post = Tumblr::Post.new(123)
380
+ assert post.edit('test@testermcgee.com','dontrevealmysecrets').is_a? Weary::Request
381
+ end
382
+
383
+ test 'deletes itself' do
384
+ post = Tumblr::Post.new(123)
385
+ assert post.delete('test@testermcgee.com','dontrevealmysecrets').is_a? Weary::Request
386
+ end
387
+
388
+ test 'publishes to tumblr' do
389
+ klass = Class.new Tumblr::Post
390
+ post = klass.new
391
+ post.instance_variable_set(:@type,:regular)
392
+ post.state = :queue
393
+ assert post.publish_now('test@testermcgee.com','dontrevealmysecrets').is_a? Weary::Request
394
+ assert_equal :published, post.state
395
+ end
396
+
397
+ test 'saves as a draft to tumblr' do
398
+ klass = Class.new Tumblr::Post
399
+ post = klass.new
400
+ post.instance_variable_set(:@type,:regular)
401
+ post.state = :published
402
+ assert post.save_as_draft('test@testermcgee.com','dontrevealmysecrets').is_a? Weary::Request
403
+ assert_equal :draft, post.state
404
+ end
405
+
406
+ test 'adds to tumblr queue' do
407
+ klass = Class.new Tumblr::Post
408
+ post = klass.new
409
+ post.instance_variable_set(:@type,:regular)
410
+ post.state = :draft
411
+ assert post.add_to_queue('test@testermcgee.com','dontrevealmysecrets').is_a? Weary::Request
412
+ assert_equal :queue, post.state
413
+ end
414
+
415
+ test 'publish on a specific date' do
416
+ klass = Class.new Tumblr::Post
417
+ post = klass.new
418
+ post.instance_variable_set(:@type,:regular)
419
+ post.state = :draft
420
+ assert post.add_to_queue('test@testermcgee.com','dontrevealmysecrets','tuesday').is_a? Weary::Request
421
+ assert_equal :queue, post.state
422
+ assert_equal 'tuesday',post.publish_on
423
+ end
424
+
425
+ test 'converts itself to YAML' do
426
+ klass = Class.new Tumblr::Post
427
+ klass.parameters :title, :body
428
+ post = klass.new
429
+ post.instance_variable_set(:@type,:regular)
430
+ post.tags 'hello', 'stuff'
431
+ post.state = :queue
432
+ post.body = "Hello world."
433
+ assert_respond_to post, :to_yaml
434
+ post_yaml = post.to_yaml
435
+ assert_equal 'Hello world.', YAML.load(post_yaml)['body']
436
+ assert_equal 'regular', YAML.load(post_yaml)['data']['type']
437
+ end
438
+
439
+ test 'converts itself to a string for writing to a file' do
440
+ klass = Class.new Tumblr::Post
441
+ klass.parameters :title, :body
442
+ post = klass.new
443
+ post.instance_variable_set(:@type,:regular)
444
+ post.tags 'hello', 'stuff'
445
+ post.state = :queue
446
+ post.body = "Hello world."
447
+ assert_equal 'regular', YAML.load(post.to_s)['type']
448
+ end
449
+ end
450
+
451
+ describe 'Regular' do
452
+ test 'is of regular type' do
453
+ reg = Tumblr::Post::Regular.new
454
+ assert_equal :regular, reg.type
455
+ end
456
+
457
+ test 'has a body' do
458
+ reg = Tumblr::Post::Regular.new
459
+ assert_respond_to reg, :body
460
+ reg.body = "Hi"
461
+ assert_equal "Hi", reg.body
462
+ end
463
+
464
+ test 'has a title' do
465
+ reg = Tumblr::Post::Regular.new
466
+ assert_respond_to reg, :title
467
+ reg.title = "Hi"
468
+ assert_equal "Hi", reg.title
469
+ end
470
+ end
471
+
472
+ describe 'Photo' do
473
+ test 'is a photo' do
474
+ photo = Tumblr::Post::Photo.new
475
+ assert_equal :photo, photo.type
476
+ end
477
+
478
+ test 'has a source' do
479
+ photo = Tumblr::Post::Photo.new
480
+ photo.source = "http://foo.bar/picture.png"
481
+ assert_equal "http://foo.bar/picture.png", photo.source
482
+ end
483
+
484
+ test 'has a caption' do
485
+ photo = Tumblr::Post::Photo.new
486
+ photo.caption = "Me in my youth"
487
+ assert_equal "Me in my youth", photo.caption
488
+ end
489
+
490
+ test 'has a click-through-url' do
491
+ photo = Tumblr::Post::Photo.new
492
+ photo.click_through_url = "http://tumblr.com"
493
+ assert_equal "http://tumblr.com", photo.click_through_url
494
+ end
495
+ end
496
+
497
+ describe 'Quote' do
498
+ test 'is a quote' do
499
+ quote = Tumblr::Post::Quote.new("To be or not to be")
500
+ assert_equal :quote, quote.type
501
+ end
502
+
503
+ test 'the quote is text' do
504
+ quote = Tumblr::Post::Quote.new("To be or not to be")
505
+ assert_equal "To be or not to be", quote.quote
506
+ quote.quote = "that is the question."
507
+ assert_equal "that is the question.", quote.quote
508
+ end
509
+
510
+ test 'the quote has a source' do
511
+ quote = Tumblr::Post::Quote.new("To be or not to be")
512
+ quote.source = "Hamlet"
513
+ assert_equal 'Hamlet', quote.source
514
+ end
515
+ end
516
+
517
+ describe 'Link' do
518
+ test 'is a link' do
519
+ link = Tumblr::Post::Link.new('http://tumblr.com')
520
+ assert_equal :link, link.type
521
+ end
522
+
523
+ test 'is a link to something' do
524
+ link = Tumblr::Post::Link.new('http://tumblr.com')
525
+ assert_equal 'http://tumblr.com', link.url
526
+ end
527
+
528
+ test 'has an optional name' do
529
+ link = Tumblr::Post::Link.new('http://tumblr.com')
530
+ link.name = 'Tumblr'
531
+ assert_equal 'Tumblr', link.name
532
+ end
533
+
534
+ test 'has an optional description' do
535
+ link = Tumblr::Post::Link.new('http://tumblr.com')
536
+ link.description = "Simple blogging"
537
+ assert_equal 'Simple blogging', link.description
538
+ end
539
+ end
540
+
541
+ describe 'Conversation' do
542
+ test 'is a conversation' do
543
+ conversation = Tumblr::Post::Conversation.new('Me: hey whatsup')
544
+ assert_equal :conversation, conversation.type
545
+ end
546
+
547
+ test 'requires a chat' do
548
+ conversation = Tumblr::Post::Conversation.new('Me: hey whatsup')
549
+ assert_equal 'Me: hey whatsup', conversation.conversation
550
+ end
551
+
552
+ test 'has an optional title' do
553
+ conversation = Tumblr::Post::Conversation.new('Me: hey whatsup')
554
+ conversation.title = 'Inner dialogue'
555
+ assert_equal 'Inner dialogue', conversation.title
556
+ end
557
+ end
558
+
559
+ describe 'Video' do
560
+ test 'is a video' do
561
+ video = Tumblr::Post::Video.new('http://www.youtube.com/watch?v=CW0DUg63lqU')
562
+ assert_equal :video, video.type
563
+ end
564
+
565
+ test 'has a url or embed code' do
566
+ video = Tumblr::Post::Video.new('http://www.youtube.com/watch?v=CW0DUg63lqU')
567
+ assert_equal 'http://www.youtube.com/watch?v=CW0DUg63lqU', video.embed
568
+ end
569
+
570
+ test 'has a caption' do
571
+ video = Tumblr::Post::Video.new('http://www.youtube.com/watch?v=CW0DUg63lqU')
572
+ video.caption = 'Good artists copy...'
573
+ assert_equal 'Good artists copy...', video.caption
574
+ end
575
+ end
576
+
577
+ describe 'Audio' do
578
+ test 'is audio' do
579
+ audio = Tumblr::Post::Audio.new
580
+ assert_equal :audio, audio.type
581
+ end
582
+
583
+ test 'can be a hosted url to an mp3 file' do
584
+ audio = Tumblr::Post::Audio.new
585
+ audio.externally_hosted_url = 'http://foobar.com/some.mp3'
586
+ assert_equal 'http://foobar.com/some.mp3', audio.externally_hosted_url
587
+ end
588
+
589
+ test 'has an optional caption' do
590
+ audio = Tumblr::Post::Audio.new
591
+ audio.caption = 'not pirated'
592
+ assert_equal 'not pirated', audio.caption
593
+ end
594
+ end
6
595
  end
7
596
  end