tumble 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tumble/blog.rb CHANGED
@@ -1,14 +1,195 @@
1
1
  module Tumble
2
2
  class Blog
3
- def initialize(client, name)
4
- @client = client
3
+ def initialize(connection, name)
4
+ @connection = connection
5
5
  @name = name
6
6
  end
7
7
 
8
8
  attr_reader :name
9
9
 
10
+ # This method returns general information about the blog, such as the title,
11
+ # number of posts, and other high-level data.
12
+ #
13
+ # @see http://www.tumblr.com/docs/en/api/v2#blog-info
14
+ # @requires_authentication Yes
10
15
  def info
11
- @client.connection.get("blog/#{name}/info")
16
+ @connection.get("/blog/#{name}/info")
17
+ end
18
+
19
+ # You can get a blog's avatar in 9 different sizes. The default size is 64x64.
20
+ #
21
+ # @see http://www.tumblr.com/docs/en/api/v2#blog-avatar
22
+ # @requires_authentication No
23
+ #
24
+ # @param options [Hash] A customizable set of options.
25
+ # @option options [Integer] :size The size of the avatar. Must be in 16, 24, 30, 40, 48, 64, 96, 128, or 512
26
+ def avatar(options={})
27
+ @connection.get("/blog/#{name}/avatar", options)
28
+ end
29
+
30
+ # Retrieve a Blog's Followers
31
+ #
32
+ # @see http://www.tumblr.com/docs/en/api/v2#blog-followers
33
+ # @requires_authentication Yes
34
+ #
35
+ # @param options [Hash] A customizable set of options.
36
+ # @option options [Integer] :limit The number of results to return: 1–20, inclusive
37
+ # @option options [Integer] :offset Result to start at
38
+ def followers(options={})
39
+ @connection.get("/blog/#{name}/followers", options)
40
+ end
41
+
42
+ # Retrieve Published Posts
43
+ #
44
+ # @see http://www.tumblr.com/docs/en/api/v2#posts
45
+ # @requires_authentication Yes
46
+ #
47
+ # @param options [Hash] A customizable set of options.
48
+ # @option options [String] :type The type of post to return. Specify one of the following: text, quote, link, answer, video, audio, photo
49
+ # @option options [Integer] :id A specific post ID. Returns the single post specified or (if not found) a 404 error.
50
+ # @option options [String] :tag Limits the response to posts with the specified tag
51
+ # @option options [Integer] :limit The number of posts to return: 1–20, inclusive
52
+ # @option options [Integer] :offset Post number to start at
53
+ # @option options [Boolean] :reblog_info Indicates whether to return reblog information (specify true or false). Returns the various reblogged_ fields.
54
+ # @option options [Boolean] :notes_info Indicates whether to return notes information (specify true or false). Returns note count and note metadata.
55
+ # @option options [String] :format Specifies the post format to return, other than HTML. Must be either text or raw.
56
+ def posts(options={})
57
+ @connection.get("/blog/#{name}/posts", options)
58
+ end
59
+
60
+ # Retrieve Queued Posts
61
+ #
62
+ # @see http://www.tumblr.com/docs/en/api/v2#blog-queue
63
+ # @requires_authentication Yes
64
+ def queue
65
+ @connection.get("/blog/#{name}/posts/queue")
66
+ end
67
+
68
+ # Retreive Draft Posts
69
+ #
70
+ # @see http://www.tumblr.com/docs/en/api/v2#blog-drafts
71
+ # @requires_authentication Yes
72
+ def drafts
73
+ @connection.get("/blog/#{name}/posts/draft")
74
+ end
75
+
76
+ # Retreive Submission Posts
77
+ #
78
+ # @see http://www.tumblr.com/docs/en/api/v2#blog-submissions
79
+ # @requires_authentication Yes
80
+ def submissions
81
+ @connection.get("/blog/#{name}/posts/submission")
82
+ end
83
+
84
+ # Create a New Blog Post
85
+ #
86
+ # @see http://www.tumblr.com/docs/en/api/v2#posting
87
+ # @requires_authentication Yes
88
+ #
89
+ # @param options [Hash] A customizable set of options
90
+ # @option options [String] :type The type of post to create. Specify one of the following: text, photo, quote, link, chat, audio, video
91
+ # @option options [String] :state The state of the post. Specify one of the following: published, draft, queue
92
+ # @option options [String] :tags Comma-separated tags for this post
93
+ # @option options [String] :tweet Manages the autotweet (if enabled) for this post: set to off for no tweet, or enter text to override the default tweet
94
+ # @option options [String] :date The GMT date and time of the post, as a string
95
+ # @option options [Boolean] :markdown Indicates whether the post uses markdown syntax
96
+ # @option options [String] :slug Add a short text summary to the end of the post URL
97
+ # For text posts:
98
+ # @option options [String] :title The optional title of the post, HTML entities must be escaped
99
+ # @option options [String] :body The full post body, HTML allowed
100
+ # For photo posts:
101
+ # @option options [String] :caption The user-supplied caption, HTML allowed
102
+ # @option options [String] :link The "click-through URL" for the photo
103
+ # @option options [String] :source The photo source URL
104
+ # @option options [Array] :data One or more image files (submit multiple times to create a slide show)
105
+ # For quote posts:
106
+ # @option options [String] :quote The full text of the quote, HTML entities must be escaped
107
+ # @option options [String] :source Cited source, HTML allowed
108
+ # For link posts:
109
+ # @option options [String] :title The title of the page the link points to, HTML entities should be escaped
110
+ # @option options [String] :url The link
111
+ # @option options [String] :description A user-supplied description, HTML allowed
112
+ # For chat posts:
113
+ # @option options [String] :title The title of the chat
114
+ # @option options [String] :conversation The text of the conversation/chat, with dialogue labels (no HTML)
115
+ # For audio posts:
116
+ # @option options [String] :caption The user-supplied caption
117
+ # @option options [String] :external_url The URL of the site that hosts the audio file (not tumblr)
118
+ # @option options [String] :data An audio file
119
+ # For video posts:
120
+ # @option options [String] :caption The user-supplied caption
121
+ # @option options [String] :embed HTML embed code for the video
122
+ # @option options [String] :data A video file
123
+ def create_post(options={})
124
+ @connection.post("/blog/#{name}/post", options)
125
+ end
126
+
127
+ # Edit a Blog Post
128
+ #
129
+ # @see http://www.tumblr.com/docs/en/api/v2#editing
130
+ # @requires_authentication Yes
131
+ #
132
+ # @param id [Integer] The ID of the post to edit
133
+ # @param options [Hash] A customizable set of options
134
+ # @option options [String] :type The type of post to create. Specify one of the following: text, photo, quote, link, chat, audio, video
135
+ # @option options [String] :state The state of the post. Specify one of the following: published, draft, queue
136
+ # @option options [String] :tags Comma-separated tags for this post
137
+ # @option options [String] :tweet Manages the autotweet (if enabled) for this post: set to off for no tweet, or enter text to override the default tweet
138
+ # @option options [String] :date The GMT date and time of the post, as a string
139
+ # @option options [Boolean] :markdown Indicates whether the post uses markdown syntax
140
+ # @option options [String] :slug Add a short text summary to the end of the post URL
141
+ # For text posts:
142
+ # @option options [String] :title The optional title of the post, HTML entities must be escaped
143
+ # @option options [String] :body The full post body, HTML allowed
144
+ # For photo posts:
145
+ # @option options [String] :caption The user-supplied caption, HTML allowed
146
+ # @option options [String] :link The "click-through URL" for the photo
147
+ # @option options [String] :source The photo source URL
148
+ # @option options [Array] :data One or more image files (submit multiple times to create a slide show)
149
+ # For quote posts:
150
+ # @option options [String] :quote The full text of the quote, HTML entities must be escaped
151
+ # @option options [String] :source Cited source, HTML allowed
152
+ # For link posts:
153
+ # @option options [String] :title The title of the page the link points to, HTML entities should be escaped
154
+ # @option options [String] :url The link
155
+ # @option options [String] :description A user-supplied description, HTML allowed
156
+ # For chat posts:
157
+ # @option options [String] :title The title of the chat
158
+ # @option options [String] :conversation The text of the conversation/chat, with dialogue labels (no HTML)
159
+ # For audio posts:
160
+ # @option options [String] :caption The user-supplied caption
161
+ # @option options [String] :external_url The URL of the site that hosts the audio file (not tumblr)
162
+ # @option options [String] :data An audio file
163
+ # For video posts:
164
+ # @option options [String] :caption The user-supplied caption
165
+ # @option options [String] :embed HTML embed code for the video
166
+ # @option options [String] :data A video file
167
+
168
+ def edit_post(id, options={})
169
+ @connection.post("/blog/#{name}/post/edit", options.merge('id' => id))
170
+ end
171
+
172
+ # Reblog a Post
173
+ #
174
+ # @see http://www.tumblr.com/docs/en/api/v2#reblogging
175
+ # @requires_authentication Yes
176
+ #
177
+ # @param id [Integer] The ID of the reblogged post on tumblelog
178
+ # @param reblog_key [Integer] The reblog key for the reblogged post – get the reblog key with a /posts request
179
+ # @param options [Hash] A customizable set of options
180
+ # @option options [String] :comment A comment added to the reblogged post
181
+ def reblog_post(id, reblog_key, options={})
182
+ @connection.post("/blog/#{name}/post/reblog", options.merge('id' => id, 'reblog_key' => reblog_key))
183
+ end
184
+
185
+ # Delete a Post
186
+ #
187
+ # @see http://www.tumblr.com/docs/en/api/v2#deleting-posts
188
+ # @requires_authentication Yes
189
+ #
190
+ # @param id [Integer] The ID of the post to delete
191
+ def delete_post(id)
192
+ @connection.post("/blog/#{name}/post/delete", :id => id)
12
193
  end
13
194
  end
14
195
  end
data/lib/tumble/client.rb CHANGED
@@ -9,48 +9,101 @@ module Tumble
9
9
  # @param [String] oauth_token
10
10
  # @param [String] oauth_secret
11
11
  def initialize(access_token, access_secret)
12
- @access_token = access_token
13
- @access_secret = access_secret
12
+ @connection = Connection.new(access_token, access_secret)
14
13
  end
15
14
 
16
- def connection
17
- options = {
18
- :url => api_url,
19
- :params => { :oauth_token => @oauth_token },
20
- :headers => {
21
- :accept => 'application/json',
22
- :user_agent => 'tumble'
23
- }
24
- }
25
- @connection ||= Faraday::Connection.new(options) do |builder|
26
- builder.use Faraday::Request::Multipart
27
- builder.use Faraday::Request::UrlEncoded
28
- builder.use FaradayMiddleware::Mashify
29
- builder.use FaradayMiddleware::ParseJson
30
- builder.use Tumble::Request::TumblrOAuth, credentials
31
- builder.adapter Faraday.default_adapter
32
- end
33
- end
34
-
35
- def api_url
36
- 'http://api.tumblr.com/v2'
37
- end
38
-
39
- def blog(blog_name)
40
- Blog.new(self, blog_name)
41
- end
42
-
43
- def credentials
44
- {
45
- :consumer_key => Config.consumer_key,
46
- :consumer_secret => Config.consumer_secret,
47
- :token => @access_token,
48
- :token_secret => @access_secret,
49
- }
15
+ def blog(name)
16
+ Blog.new(@connection, name)
50
17
  end
51
18
 
19
+ # Get Info for the Authenticated User
20
+ #
21
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
22
+ # @requires_authentication Yes
52
23
  def user_info
53
- @connection.post('http://api.tumblr.com/v2/user/info')
24
+ @connection.post('/user/info').body.response.user
25
+ end
26
+
27
+ # Retrieve a User's Dashboard
28
+ #
29
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
30
+ # @requires_authentication Yes
31
+ #
32
+ # @param options [Hash] A customizable set of options.
33
+ # @option options [Integer] :limit The number of results to return: 1–20, inclusive
34
+ # @option options [Integer] :offset Post number to start at
35
+ # @option options [String] :type The type of post to return. Specify one of the following: text, photo, quote, link, chat, audio, video, question
36
+ # @option options [Integer] :since_id Return posts that have appeared after this ID
37
+ # @option options [Boolean] :reblog_info Indicates whether to return reblog information (specify true or false). Returns the various reblogged_ fields.
38
+ # @option options [Boolean] :notes_info Indicates whether to return notes information (specify true or false). Returns note count and note metadata.
39
+ def dashboard(options={})
40
+ @connection.get('/user/dashboard')
41
+ end
42
+
43
+ # Retrieve a User's Likes
44
+ #
45
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
46
+ # @requires_authentication Yes
47
+ #
48
+ # @param options [Hash] A customizable set of options.
49
+ # @option options [Integer] :limit The number of results to return: 1–20, inclusive
50
+ # @option options [Integer] :offset Post number to start at
51
+ def likes(options={})
52
+ @connection.get('/user/likes')
53
+ end
54
+
55
+ # Retrieve the Blogs a User Is Following
56
+ #
57
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
58
+ # @requires_authentication Yes
59
+ #
60
+ # @param options [Hash] A customizable set of options.
61
+ # @option options [Integer] :limit The number of results to return: 1–20, inclusive
62
+ # @option options [Integer] :offset Post number to start at
63
+ def following(options={})
64
+ @connection.get('/user/following')
65
+ end
66
+
67
+ # Follow a Blog
68
+ #
69
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
70
+ # @requires_authentication Yes
71
+ #
72
+ # @param url [String] The URL of the blog to follow
73
+ def follow(url)
74
+ @connection.post('/user/follow', :url => url)
75
+ end
76
+
77
+ # Unfollow a Blog
78
+ #
79
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
80
+ # @requires_authentication Yes
81
+ #
82
+ # @param url [String] The URL of the blog to follow
83
+ def unfollow(url)
84
+ @connection.post('/user/unfollow', :url => url)
85
+ end
86
+
87
+ # Like a Post
88
+ #
89
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
90
+ # @requires_authentication Yes
91
+ #
92
+ # @param id [Integer] The ID of the post to like
93
+ # @param reblog_key [String] The reblog key for the post id
94
+ def like(id, reblog_key)
95
+ @connection.post('/user/like', :id => id, :reblog_key => reblog_key)
96
+ end
97
+
98
+ # Unlike a Post
99
+ #
100
+ # @see http://www.tumblr.com/docs/en/api/v2#user-methods
101
+ # @requires_authentication Yes
102
+ #
103
+ # @param id [Integer] The ID of the post to unlike
104
+ # @param reblog_key [String] The reblog key for the post id
105
+ def unlike(id, reblog_key)
106
+ @connection.post('/user/unlike', :id => id, :reblog_key => reblog_key)
54
107
  end
55
108
  end
56
109
  end
@@ -0,0 +1,72 @@
1
+ module Tumble
2
+ class Connection
3
+
4
+ def initialize(token, secret)
5
+ @token = token
6
+ @secret = secret
7
+ @faraday_connection = faraday_connection
8
+ end
9
+
10
+ def base_url
11
+ 'http://api.tumblr.com/'
12
+ end
13
+
14
+ def delete(path, params={})
15
+ request(:delete, path, params)
16
+ end
17
+
18
+ # Perform an HTTP GET request
19
+ def get(path, params={})
20
+ request(:get, path, params)
21
+ end
22
+
23
+ # Perform an HTTP POST request
24
+ def post(path, params={})
25
+ request(:post, path, params)
26
+ end
27
+
28
+ private
29
+
30
+ def request(method, path, params)
31
+ path = "/v2#{path}" unless path =~ /^\/v2/
32
+ response = @faraday_connection.send(method) do |request|
33
+ case method
34
+ when :delete, :get
35
+ request.url(path, params)
36
+ when :post
37
+ request.path = path
38
+ request.body = params unless params.empty?
39
+ end
40
+ end
41
+ response.body
42
+ end
43
+
44
+ def faraday_connection
45
+ options = {
46
+ :url => base_url,
47
+ :params => { :oauth_token => @token },
48
+ :headers => {
49
+ :accept => 'application/json',
50
+ :user_agent => 'tumble'
51
+ }
52
+ }
53
+ Faraday::Connection.new(options) do |builder|
54
+ builder.use Faraday::Request::Multipart
55
+ builder.use Faraday::Request::UrlEncoded
56
+ builder.use FaradayMiddleware::Mashify
57
+ builder.use FaradayMiddleware::ParseJson
58
+ builder.use Tumble::Request::TumblrOAuth, credentials
59
+ builder.adapter Faraday.default_adapter
60
+ end
61
+ end
62
+
63
+ def credentials
64
+ {
65
+ :consumer_key => Config.consumer_key,
66
+ :consumer_secret => Config.consumer_secret,
67
+ :token => @token,
68
+ :token_secret => @secret,
69
+ }
70
+ end
71
+ end
72
+ end
data/lib/tumble.rb CHANGED
@@ -1,11 +1,11 @@
1
- %w(blog client config request/oauth).each do |file|
1
+ %w(blog client config connection request/oauth).each do |file|
2
2
  require File.join(File.dirname(__FILE__), 'tumble', file)
3
3
  end
4
4
 
5
5
  module Tumble
6
- VERSION = '0.0.3'
6
+ VERSION = '0.0.4'
7
7
 
8
- def self.config
8
+ def self.configure
9
9
  yield Config
10
10
  Config
11
11
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tumble::Connection do
4
+ let(:connection) { Tumble::Connection.new('abc', '123') }
5
+
6
+ it 'should perform a get request' do
7
+ stub = WebMock.stub_request(:get, /http:\/\/api\.tumblr\.com\/v2\/user\/info?.*/)
8
+ connection.get('/user/info')
9
+ stub.should have_been_requested
10
+ end
11
+
12
+ it 'should perform a post request' do
13
+ stub = WebMock.stub_request(:post, /http:\/\/api\.tumblr\.com\/v2\/user\/info?.*/)
14
+ connection.post('/user/info')
15
+ stub.should have_been_requested
16
+ end
17
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,5 +2,5 @@ require 'tumble'
2
2
 
3
3
  require 'webmock/rspec'
4
4
 
5
- # WebMock.disable_net_connect!
6
- WebMock.allow_net_connect!
5
+ WebMock.disable_net_connect!
6
+ # WebMock.allow_net_connect!
data/tumble.gemspec CHANGED
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 1.3.5") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'tumble'
6
- s.version = '0.0.3'
6
+ s.version = '0.0.4'
7
7
 
8
8
  s.summary = 'Library for accessing the Tumblr api v2'
9
9
  # TODO: s.description
@@ -26,10 +26,12 @@ Gem::Specification.new do |s|
26
26
  lib/tumble/blog.rb
27
27
  lib/tumble/client.rb
28
28
  lib/tumble/config.rb
29
+ lib/tumble/connection.rb
29
30
  lib/tumble/request/oauth.rb
30
31
  spec/blog_spec.rb
31
32
  spec/client_spec.rb
32
33
  spec/config_spec.rb
34
+ spec/connection_spec.rb
33
35
  spec/oauth_spec.rb
34
36
  spec/spec_helper.rb
35
37
  tumble.gemspec
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tumble
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
17
- requirement: &70176759564620 !ruby/object:Gem::Requirement
17
+ requirement: &2162083700 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70176759564620
25
+ version_requirements: *2162083700
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &70176759563740 !ruby/object:Gem::Requirement
28
+ requirement: &2162083240 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70176759563740
36
+ version_requirements: *2162083240
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &70176759555780 !ruby/object:Gem::Requirement
39
+ requirement: &2162082820 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70176759555780
47
+ version_requirements: *2162082820
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: webmock
50
- requirement: &70176759555040 !ruby/object:Gem::Requirement
50
+ requirement: &2162082400 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70176759555040
58
+ version_requirements: *2162082400
59
59
  description:
60
60
  email: aubreyholland@gmail.com
61
61
  executables: []
@@ -69,10 +69,12 @@ files:
69
69
  - lib/tumble/blog.rb
70
70
  - lib/tumble/client.rb
71
71
  - lib/tumble/config.rb
72
+ - lib/tumble/connection.rb
72
73
  - lib/tumble/request/oauth.rb
73
74
  - spec/blog_spec.rb
74
75
  - spec/client_spec.rb
75
76
  - spec/config_spec.rb
77
+ - spec/connection_spec.rb
76
78
  - spec/oauth_spec.rb
77
79
  - spec/spec_helper.rb
78
80
  - tumble.gemspec