tumbl_rb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/Guardfile +8 -0
- data/LICENSE +20 -0
- data/README.md +62 -0
- data/Rakefile +19 -0
- data/lib/faraday/response/raise_tumbl_rb_error.rb +28 -0
- data/lib/tumbl_rb.rb +25 -0
- data/lib/tumbl_rb/blog.rb +36 -0
- data/lib/tumbl_rb/client.rb +40 -0
- data/lib/tumbl_rb/client/blogs.rb +48 -0
- data/lib/tumbl_rb/configuration.rb +39 -0
- data/lib/tumbl_rb/connection.rb +34 -0
- data/lib/tumbl_rb/error.rb +12 -0
- data/lib/tumbl_rb/request.rb +29 -0
- data/lib/tumbl_rb/version.rb +3 -0
- data/spec/faraday/response_spec.rb +27 -0
- data/spec/fixtures/avatar.json +1 -0
- data/spec/fixtures/info.json +18 -0
- data/spec/fixtures/posts.json +484 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/tumbl_rb/blog_spec.rb +75 -0
- data/spec/tumbl_rb/client/blogs_spec.rb +34 -0
- data/spec/tumbl_rb/client_spec.rb +38 -0
- data/spec/tumbl_rb_spec.rb +27 -0
- data/tumbl_rb.gemspec +37 -0
- metadata +308 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
guard 'rspec', :version => 2 do
|
2
|
+
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
4
|
+
watch(%r{^lib/tumbl_rb/(.+)\.rb$}) { |m| "spec/tumbl_rb/#{m[1]}_spec.rb" }
|
5
|
+
watch(%r{^lib/tumbl_rb/client/(.+)\.rb$}) { |m| "spec/tumbl_rb/client/#{m[1]}_spec.rb" }
|
6
|
+
watch('spec/spec_helper.rb') { "spec" }
|
7
|
+
end
|
8
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Andrew Thorp
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Tumbl_rb
|
2
|
+
Simple Ruby wrapper for the Tumblr v2 API
|
3
|
+
|
4
|
+
[![Build Status](https://secure.travis-ci.org/andrewpthorp/tumbl_rb.png)](http://travis-ci.org/andrewpthorp/tumbl_rb) [![Dependency Status](https://gemnasium.com/andrewpthorp/tumbl_rb.png)](https://gemnasium.com/andrewpthorp/tumbl_rb)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
gem install 'tumbl_rb'
|
8
|
+
|
9
|
+
## Documentation
|
10
|
+
none yet
|
11
|
+
|
12
|
+
## Examples
|
13
|
+
|
14
|
+
### Add your api_key (consumer_oauth_key)
|
15
|
+
```ruby
|
16
|
+
TumblRb.configure do |config|
|
17
|
+
config.consumer_oauth_key = "1234567890"
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
### Get a Blog's info
|
22
|
+
```ruby
|
23
|
+
TumblRb.info("andrewpthorp")
|
24
|
+
```
|
25
|
+
|
26
|
+
### Get a Blog's avatar
|
27
|
+
```ruby
|
28
|
+
TumblRb.avatar("andrewpthorp")
|
29
|
+
```
|
30
|
+
|
31
|
+
### Get Blog Posts
|
32
|
+
```ruby
|
33
|
+
TumblRb.posts("andrewpthorp")
|
34
|
+
```
|
35
|
+
|
36
|
+
### Get all posts of a specific type (text, quote, link, answer, video, audio, photo, chat)
|
37
|
+
```ruby
|
38
|
+
TumblRb.posts("andrewpthorp", :type => "link")
|
39
|
+
```
|
40
|
+
|
41
|
+
### Get a specific post
|
42
|
+
```ruby
|
43
|
+
TumblRb.posts("andrewpthorp", :id => 123456789)
|
44
|
+
```
|
45
|
+
|
46
|
+
**Different types bring back different responses. To check them out, [visit the documentation](http://www.tumblr.com/docs/en/api/v2)**
|
47
|
+
|
48
|
+
## Version
|
49
|
+
This gem supports the methods of the Tumblr API that do not require OAuth. This may change before v3 of the Tumblr API, and will definitely change after v3 of the Tumblr API.
|
50
|
+
|
51
|
+
## Inspiration
|
52
|
+
Tumbl_rb was heavily inspired by [octokit][octokit] and [sqoot][sqoot]. Reading through
|
53
|
+
and understanding those projects was crucial to understanding the anatomy of a gem (specifically
|
54
|
+
an API wrapper gem). Thanks!
|
55
|
+
|
56
|
+
[octokit]: https://github.com/pengwynn/octokit
|
57
|
+
[sqoot]: https://github.com/causemetric/sqoot
|
58
|
+
|
59
|
+
## Copyright
|
60
|
+
Copyright (c) 2012 Andrew Thorp. See [LICENSE][license] for details.
|
61
|
+
|
62
|
+
[license]: https://github.com/andrewpthorp/tumbl_rb/blob/master/LICENSE
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :test => :spec
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
namespace :doc do
|
11
|
+
require 'yard'
|
12
|
+
YARD::Rake::YardocTask.new do |task|
|
13
|
+
task.files = ['README.md', 'LICENSE.md', 'lib/**/*.rb']
|
14
|
+
task.options = [
|
15
|
+
'--output-dir', 'doc/yard',
|
16
|
+
'--markup', 'markdown',
|
17
|
+
]
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
# @api private
|
5
|
+
module Faraday
|
6
|
+
class Response::RaiseTumblRbError < Response::Middleware
|
7
|
+
def on_complete(response)
|
8
|
+
case response[:status].to_i
|
9
|
+
when 401
|
10
|
+
raise TumblRb::Unauthorized, error_message(response)
|
11
|
+
when 404
|
12
|
+
raise TumblRb::NotFound, error_message(response)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def error_message(response)
|
17
|
+
message = if (body = response[:body]) && !body.empty?
|
18
|
+
if body.is_a?(String)
|
19
|
+
body = MultiJson.load(body, :symbolize_keys => true)
|
20
|
+
end
|
21
|
+
": #{body[:error] || body[:message] || ''}"
|
22
|
+
else
|
23
|
+
''
|
24
|
+
end
|
25
|
+
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/tumbl_rb.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'tumbl_rb/configuration'
|
2
|
+
require 'tumbl_rb/client'
|
3
|
+
require 'tumbl_rb/error'
|
4
|
+
|
5
|
+
module TumblRb
|
6
|
+
extend Configuration
|
7
|
+
class << self
|
8
|
+
# Alias for TumblRb::Client.new
|
9
|
+
#
|
10
|
+
# @return [TumblRb::Client]
|
11
|
+
def new(options={})
|
12
|
+
TumblRb::Client.new(options)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Delegate to TumblRb::Client.new
|
16
|
+
def method_missing(method, *args, &block)
|
17
|
+
return super unless new.respond_to?(method)
|
18
|
+
new.send(method, *args, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def respond_to?(method, include_private=false)
|
22
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module TumblRb
|
2
|
+
class Blog
|
3
|
+
attr_accessor :hostname, :username
|
4
|
+
|
5
|
+
def initialize(blog)
|
6
|
+
case blog
|
7
|
+
when String
|
8
|
+
if blog.include?(".")
|
9
|
+
@hostname = blog
|
10
|
+
@username = blog.include?(".tumblr.com") ? blog.chomp(".tumblr.com") : nil
|
11
|
+
else
|
12
|
+
@hostname = "#{blog}.tumblr.com"
|
13
|
+
@username = blog
|
14
|
+
end
|
15
|
+
when Hash
|
16
|
+
@hostname = blog[:hostname] ||= blog[:blog]
|
17
|
+
@username = blog[:username] ||= blog[:user]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the hostname
|
22
|
+
#
|
23
|
+
# @return [String]
|
24
|
+
def to_s
|
25
|
+
@hostname
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns the url
|
29
|
+
#
|
30
|
+
# @return [String]
|
31
|
+
def url
|
32
|
+
"http://#{@hostname}"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'tumbl_rb/connection'
|
2
|
+
require 'tumbl_rb/request'
|
3
|
+
require 'tumbl_rb/blog'
|
4
|
+
|
5
|
+
require 'tumbl_rb/client/blogs'
|
6
|
+
|
7
|
+
module TumblRb
|
8
|
+
|
9
|
+
class Client
|
10
|
+
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
|
11
|
+
|
12
|
+
def initialize(options={})
|
13
|
+
options = TumblRb.options.merge(options)
|
14
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
15
|
+
send("#{key}=", options[key])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Provides the URL for accessing the API
|
20
|
+
#
|
21
|
+
# @return [String]
|
22
|
+
def api_url
|
23
|
+
"http://api.tumblr.com"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Determine if an consumer_oauth_key has been set
|
27
|
+
#
|
28
|
+
# @return [Boolean]
|
29
|
+
def oauthed?
|
30
|
+
!consumer_oauth_key.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
include TumblRb::Connection
|
34
|
+
include TumblRb::Request
|
35
|
+
|
36
|
+
include TumblRb::Client::Blogs
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module TumblRb
|
2
|
+
class Client
|
3
|
+
module Blogs
|
4
|
+
|
5
|
+
# Get the info of a blog
|
6
|
+
#
|
7
|
+
# @param [String, Hash] blog to get information for
|
8
|
+
#
|
9
|
+
# @return [Hashie::Mash]
|
10
|
+
# @example
|
11
|
+
# TumblRb.info("andrewpthorp")
|
12
|
+
def info(blog)
|
13
|
+
get("/v2/blog/#{Blog.new blog}/info", {}, false)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
# Get the avatar of a blog
|
18
|
+
#
|
19
|
+
# @param [String, Hash] blog
|
20
|
+
#
|
21
|
+
# @return [Hashie::Mash]
|
22
|
+
# @example
|
23
|
+
# TumblRb.avatar("andrewpthorp")
|
24
|
+
def avatar(blog)
|
25
|
+
get("/v2/blog/#{Blog.new blog}/avatar", {}, false)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Get the posts of a blog
|
29
|
+
#
|
30
|
+
# @param [String, Hash] blog
|
31
|
+
# @param [String] post type
|
32
|
+
#
|
33
|
+
# @return [Hashie::Mash]
|
34
|
+
# @example
|
35
|
+
# TumblRb.posts("andrewpthorp", "text")
|
36
|
+
def posts(blog, options={})
|
37
|
+
url = "/v2/blog/#{Blog.new blog}/posts"
|
38
|
+
|
39
|
+
if !options[:type].nil?
|
40
|
+
url += "/#{options[:type]}"
|
41
|
+
end
|
42
|
+
|
43
|
+
get(url, options, false)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'tumbl_rb/version'
|
3
|
+
|
4
|
+
module TumblRb
|
5
|
+
module Configuration
|
6
|
+
VALID_OPTIONS_KEYS = [
|
7
|
+
:adapter,
|
8
|
+
:api_version,
|
9
|
+
:proxy,
|
10
|
+
:consumer_oauth_key,
|
11
|
+
:user_agent].freeze
|
12
|
+
|
13
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
14
|
+
DEFAULT_API_VERSION = 2
|
15
|
+
DEFAULT_USER_AGENT = "TumblRb Ruby Gem #{TumblRb::VERSION}".freeze
|
16
|
+
|
17
|
+
attr_accessor(*VALID_OPTIONS_KEYS)
|
18
|
+
|
19
|
+
def self.extended(base)
|
20
|
+
base.reset
|
21
|
+
end
|
22
|
+
|
23
|
+
def configure
|
24
|
+
yield self
|
25
|
+
end
|
26
|
+
|
27
|
+
def options
|
28
|
+
VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def reset
|
32
|
+
self.adapter = DEFAULT_ADAPTER
|
33
|
+
self.api_version = DEFAULT_API_VERSION
|
34
|
+
self.consumer_oauth_key = nil
|
35
|
+
self.proxy = nil
|
36
|
+
self.user_agent = DEFAULT_USER_AGENT
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday/response/raise_tumbl_rb_error'
|
3
|
+
|
4
|
+
module TumblRb
|
5
|
+
# @private
|
6
|
+
module Connection
|
7
|
+
private
|
8
|
+
|
9
|
+
def connection(raw=false)
|
10
|
+
|
11
|
+
options = {
|
12
|
+
:proxy => proxy,
|
13
|
+
:ssl => { :verify => false },
|
14
|
+
:url => api_url
|
15
|
+
}
|
16
|
+
|
17
|
+
options.merge!(:params => { :api_key => consumer_oauth_key}) if oauthed?
|
18
|
+
|
19
|
+
connection = Faraday.new(options) do |builder|
|
20
|
+
builder.request :url_encoded
|
21
|
+
|
22
|
+
builder.use Faraday::Response::RaiseTumblRbError
|
23
|
+
unless raw
|
24
|
+
builder.use FaradayMiddleware::Mashify
|
25
|
+
builder.use FaradayMiddleware::ParseJson
|
26
|
+
end
|
27
|
+
|
28
|
+
builder.adapter adapter
|
29
|
+
end
|
30
|
+
|
31
|
+
connection
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
module TumblRb
|
4
|
+
module Request
|
5
|
+
def get(path, options={}, raw=false, include_meta=false)
|
6
|
+
request(:get, path, options, raw, include_meta)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def request(method, path, options, raw, include_meta)
|
12
|
+
response = connection(raw).send(method) do |request|
|
13
|
+
request.url(path, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
if raw
|
17
|
+
response
|
18
|
+
else
|
19
|
+
|
20
|
+
if include_meta
|
21
|
+
response.body
|
22
|
+
else
|
23
|
+
response.body.response
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|