tumblr-oauth 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +51 -0
- data/.rspec +1 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +52 -0
- data/Guardfile +14 -0
- data/LICENSE.txt +20 -0
- data/README.md +36 -0
- data/Rakefile +51 -0
- data/lib/faraday/response/raise_http_4xx.rb +43 -0
- data/lib/faraday/response/raise_http_5xx.rb +24 -0
- data/lib/tumblr-oauth.rb +25 -0
- data/lib/tumblr-oauth/authentication.rb +25 -0
- data/lib/tumblr-oauth/client.rb +27 -0
- data/lib/tumblr-oauth/client/blog.rb +36 -0
- data/lib/tumblr-oauth/client/user.rb +15 -0
- data/lib/tumblr-oauth/configuration.rb +65 -0
- data/lib/tumblr-oauth/connection.rb +28 -0
- data/lib/tumblr-oauth/error.rb +35 -0
- data/lib/tumblr-oauth/request.rb +39 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/tumblr-oauth/client/blog_spec.rb +38 -0
- data/spec/tumblr-oauth/client_spec.rb +4 -0
- data/spec/tumblr-oauth/configuration_spec.rb +44 -0
- data/spec/tumblr-oauth_spec.rb +30 -0
- metadata +169 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
18
|
+
#
|
19
|
+
# * Create a file at ~/.gitignore
|
20
|
+
# * Include files you want ignored
|
21
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
22
|
+
#
|
23
|
+
# After doing this, these files will be ignored in all your git projects,
|
24
|
+
# saving you from having to 'pollute' every project you touch with them
|
25
|
+
#
|
26
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
27
|
+
#
|
28
|
+
# For MacOS:
|
29
|
+
#
|
30
|
+
#.DS_Store
|
31
|
+
|
32
|
+
# For TextMate
|
33
|
+
#*.tmproj
|
34
|
+
#tmtags
|
35
|
+
|
36
|
+
# For emacs:
|
37
|
+
#*~
|
38
|
+
#\#*
|
39
|
+
#.\#*
|
40
|
+
|
41
|
+
# For vim:
|
42
|
+
#*.swp
|
43
|
+
|
44
|
+
# For redcar:
|
45
|
+
#.redcar
|
46
|
+
|
47
|
+
# For rubinius:
|
48
|
+
#*.rbc
|
49
|
+
|
50
|
+
# rvm files
|
51
|
+
.rvmrc
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem 'faraday', '~> 0.7.4'
|
4
|
+
gem 'faraday_middleware', '~> 0.7.0'
|
5
|
+
gem 'multi_json', '~> 1.0.0'
|
6
|
+
gem 'hashie', '~> 1.0.0'
|
7
|
+
gem 'simple_oauth', '~> 0.1.5'
|
8
|
+
|
9
|
+
# Add dependencies to develop your gem here.
|
10
|
+
# Include everything needed to run rake, tests, features, etc.
|
11
|
+
group :development, :test do
|
12
|
+
gem "rspec", "~> 2.3.0"
|
13
|
+
gem "bundler", "~> 1.0.0"
|
14
|
+
gem "jeweler", "~> 1.6.4"
|
15
|
+
|
16
|
+
gem 'guard'
|
17
|
+
gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ /darwin/i
|
18
|
+
gem 'guard-rspec'
|
19
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.2.6)
|
5
|
+
diff-lcs (1.1.2)
|
6
|
+
faraday (0.6.1)
|
7
|
+
addressable (~> 2.2.4)
|
8
|
+
multipart-post (~> 1.1.0)
|
9
|
+
rack (>= 1.1.0, < 2)
|
10
|
+
faraday_middleware (0.6.5)
|
11
|
+
faraday (~> 0.6.0)
|
12
|
+
git (1.2.5)
|
13
|
+
guard (0.5.1)
|
14
|
+
thor (~> 0.14.6)
|
15
|
+
guard-rspec (0.4.0)
|
16
|
+
guard (>= 0.4.0)
|
17
|
+
hashie (1.0.0)
|
18
|
+
jeweler (1.6.4)
|
19
|
+
bundler (~> 1.0)
|
20
|
+
git (>= 1.2.5)
|
21
|
+
rake
|
22
|
+
multi_json (1.0.3)
|
23
|
+
multipart-post (1.1.2)
|
24
|
+
rack (1.3.1)
|
25
|
+
rake (0.9.2)
|
26
|
+
rb-fsevent (0.4.1)
|
27
|
+
rspec (2.3.0)
|
28
|
+
rspec-core (~> 2.3.0)
|
29
|
+
rspec-expectations (~> 2.3.0)
|
30
|
+
rspec-mocks (~> 2.3.0)
|
31
|
+
rspec-core (2.3.1)
|
32
|
+
rspec-expectations (2.3.0)
|
33
|
+
diff-lcs (~> 1.1.2)
|
34
|
+
rspec-mocks (2.3.0)
|
35
|
+
simple_oauth (0.1.5)
|
36
|
+
thor (0.14.6)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
bundler (~> 1.0.0)
|
43
|
+
faraday (~> 0.6.0)
|
44
|
+
faraday_middleware (~> 0.6.0)
|
45
|
+
guard
|
46
|
+
guard-rspec
|
47
|
+
hashie (~> 1.0.0)
|
48
|
+
jeweler (~> 1.6.4)
|
49
|
+
multi_json (~> 1.0.0)
|
50
|
+
rb-fsevent
|
51
|
+
rspec (~> 2.3.0)
|
52
|
+
simple_oauth (~> 0.1.5)
|
data/Guardfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2, :notification => false do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^spec/.+_spec\.rb$})
|
11
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
12
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
13
|
+
watch('spec/spec_helper.rb') { "spec" }
|
14
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Ildar Shaynurov
|
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,36 @@
|
|
1
|
+
tumblr-oauth
|
2
|
+
============
|
3
|
+
|
4
|
+
Tumblr ruby gem with OAuth support.
|
5
|
+
|
6
|
+
It's very raw and unstable version.
|
7
|
+
|
8
|
+
Install
|
9
|
+
------
|
10
|
+
|
11
|
+
```
|
12
|
+
gem 'tumblr-oauth'
|
13
|
+
```
|
14
|
+
|
15
|
+
Usage
|
16
|
+
-----
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
TumblrOAuth.configure do |config|
|
20
|
+
config.consumer_key = 'consumer_key'
|
21
|
+
config.consumer_secret = 'consumer_secret'
|
22
|
+
end
|
23
|
+
|
24
|
+
tumblr_client = TumblrOAuth::Client(
|
25
|
+
:oauth_token => 'oauth_token',
|
26
|
+
:oauth_token_secret => 'oauth_secret',
|
27
|
+
:blog_host => 'blog_host_name' # For example "test.tumblr.com"
|
28
|
+
)
|
29
|
+
```
|
30
|
+
|
31
|
+
Copyright
|
32
|
+
=========
|
33
|
+
|
34
|
+
Copyright (c) 2011 Ildar Shaynurov. See LICENSE.txt for
|
35
|
+
further details.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "tumblr-oauth"
|
18
|
+
gem.homepage = "http://github.com/shir/tumblr-oauth"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Tumblr library with OAuth support}
|
21
|
+
gem.description = %q{A Ruby wrapper for Tumblr AOuth API}
|
22
|
+
gem.email = "shaynurov@gmail.com"
|
23
|
+
gem.authors = ["Ildar Shaynurov"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
require File.expand_path('../lib/tumblr-oauth/version', __FILE__)
|
43
|
+
|
44
|
+
RDoc::Task.new do |rdoc|
|
45
|
+
version = TumblrOAuth::VERSION
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "tumblr-oauth #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Response::RaiseHttp4xx < Response::Middleware
|
7
|
+
def on_complete(env)
|
8
|
+
case env[:status].to_i
|
9
|
+
when 400
|
10
|
+
raise TumblrOAuth::BadRequest.new(error_message(env), env[:response_headers])
|
11
|
+
when 401
|
12
|
+
raise TumblrOAuth::Unauthorized.new(error_message(env), env[:response_headers])
|
13
|
+
when 403
|
14
|
+
raise TumblrOAuth::Forbidden.new(error_message(env), env[:response_headers])
|
15
|
+
when 404
|
16
|
+
raise TumblrOAuth::NotFound.new(error_message(env), env[:response_headers])
|
17
|
+
when 406
|
18
|
+
raise TumblrOAuth::NotAcceptable.new(error_message(env), env[:response_headers])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def error_message(env)
|
25
|
+
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def error_body(body)
|
29
|
+
if body.nil?
|
30
|
+
nil
|
31
|
+
elsif body['error']
|
32
|
+
": #{body['error']}"
|
33
|
+
elsif body['errors']
|
34
|
+
first = body['errors'].to_a.first
|
35
|
+
if first.kind_of? Hash
|
36
|
+
": #{first['message'].chomp}"
|
37
|
+
else
|
38
|
+
": #{first.chomp}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
module Faraday
|
5
|
+
# @private
|
6
|
+
class Response::RaiseHttp5xx < Response::Middleware
|
7
|
+
def on_complete(env)
|
8
|
+
case env[:status].to_i
|
9
|
+
when 500
|
10
|
+
raise TumblrOAuth::InternalServerError.new(error_message(env, "Something is technically wrong."), env[:response_headers])
|
11
|
+
when 502
|
12
|
+
raise TumblrOAuth::BadGateway.new(error_message(env, "Tumblr is down or being upgraded."), env[:response_headers])
|
13
|
+
when 503
|
14
|
+
raise TumblrOAuth::ServiceUnavailable.new(error_message(env, "(__-){ Tumblr is over capacity."), env[:response_headers])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def error_message(env, body=nil)
|
21
|
+
"#{env[:method].to_s.upcase} #{env[:url].to_s}: #{[env[:status].to_s + ':', body].compact.join(' ')}."
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/tumblr-oauth.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'tumblr-oauth/configuration'
|
2
|
+
require 'tumblr-oauth/client'
|
3
|
+
require 'tumblr-oauth/error'
|
4
|
+
|
5
|
+
module TumblrOAuth
|
6
|
+
extend Configuration
|
7
|
+
|
8
|
+
# Alias for TumblrOAuth::Client.new
|
9
|
+
#
|
10
|
+
# @return [TumblrOAuth::Client]
|
11
|
+
#
|
12
|
+
def self.client(options={})
|
13
|
+
TumblrOAuth::Client.new(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Delegate to TumblrOAuth::Client
|
17
|
+
def self.method_missing(method, *args, &block)
|
18
|
+
return super unless client.respond_to?(method)
|
19
|
+
client.send(method, *args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.respond_to?(method, include_private = false)
|
23
|
+
client.respond_to?(method, include_private) || super(method, include_private)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module TumblrOAuth
|
2
|
+
# @private
|
3
|
+
module Authentication
|
4
|
+
private
|
5
|
+
|
6
|
+
# Authentication hash
|
7
|
+
#
|
8
|
+
# @return [Hash]
|
9
|
+
def authentication
|
10
|
+
{
|
11
|
+
:consumer_key => consumer_key,
|
12
|
+
:consumer_secret => consumer_secret,
|
13
|
+
:token => oauth_token,
|
14
|
+
:token_secret => oauth_token_secret
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
# Check whether user is authenticated
|
19
|
+
#
|
20
|
+
# @return [Boolean]
|
21
|
+
def authenticated?
|
22
|
+
authentication.values.all?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'tumblr-oauth/authentication'
|
2
|
+
require 'tumblr-oauth/connection'
|
3
|
+
require 'tumblr-oauth/request'
|
4
|
+
|
5
|
+
module TumblrOAuth
|
6
|
+
class Client
|
7
|
+
require 'tumblr-oauth/client/blog'
|
8
|
+
require 'tumblr-oauth/client/user'
|
9
|
+
|
10
|
+
attr_accessor *Configuration::VALID_OPTIONS_KEYS
|
11
|
+
|
12
|
+
# Creates a new API
|
13
|
+
def initialize(options={})
|
14
|
+
options = TumblrOAuth.options.merge(options)
|
15
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
16
|
+
send("#{key}=", options[key])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
include Authentication
|
21
|
+
include Connection
|
22
|
+
include Request
|
23
|
+
|
24
|
+
include TumblrOAuth::Client::Blog
|
25
|
+
include TumblrOAuth::Client::User
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module TumblrOAuth
|
2
|
+
class Client
|
3
|
+
module Blog
|
4
|
+
def blog_info
|
5
|
+
blog_request(:get, 'info', :api_key)
|
6
|
+
end
|
7
|
+
|
8
|
+
def blog_avatar
|
9
|
+
blog_request(:get, 'avatar')
|
10
|
+
end
|
11
|
+
|
12
|
+
def blog_followers
|
13
|
+
blog_request(:get, 'followers', :oauth)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def blog_request(method, path, auth_type = :none)
|
19
|
+
auth_string =
|
20
|
+
case auth_type.to_sym
|
21
|
+
when :api_key
|
22
|
+
"?api_key=#{consumer_key}"
|
23
|
+
when :oauth
|
24
|
+
''
|
25
|
+
else
|
26
|
+
''
|
27
|
+
end
|
28
|
+
request(method, path_with_blog_host(path) + auth_string)
|
29
|
+
end
|
30
|
+
|
31
|
+
def path_with_blog_host(path)
|
32
|
+
['blog', blog_host, path].join('/')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module TumblrOAuth
|
2
|
+
class Client
|
3
|
+
module User
|
4
|
+
def user_info
|
5
|
+
@user_info ||= request(:post, 'user/info')
|
6
|
+
end
|
7
|
+
|
8
|
+
def primary_blog
|
9
|
+
blog = user_info.response.user.blogs.find {|blog| blog.primary }
|
10
|
+
URI.parse(blog.url).host
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module TumblrOAuth
|
2
|
+
module Configuration
|
3
|
+
VALID_OPTIONS_KEYS = [
|
4
|
+
:consumer_key,
|
5
|
+
:consumer_secret,
|
6
|
+
:endpoint,
|
7
|
+
:oauth_token,
|
8
|
+
:oauth_token_secret,
|
9
|
+
:blog_host,
|
10
|
+
:debug
|
11
|
+
].freeze
|
12
|
+
|
13
|
+
# By default, don't set an application key
|
14
|
+
DEFAULT_CONSUMER_KEY = nil
|
15
|
+
|
16
|
+
# By default, don't set an application secret
|
17
|
+
DEFAULT_CONSUMER_SECRET = nil
|
18
|
+
|
19
|
+
# The endpoint that will be used to connect if none is set
|
20
|
+
#
|
21
|
+
DEFAULT_ENDPOINT = 'http://api.tumblr.com/v2'.freeze
|
22
|
+
|
23
|
+
# By default, don't set a user oauth token
|
24
|
+
DEFAULT_OAUTH_TOKEN = nil
|
25
|
+
|
26
|
+
# By default, don't set a user oauth secret
|
27
|
+
DEFAULT_OAUTH_TOKEN_SECRET = nil
|
28
|
+
|
29
|
+
# By default don't set blog_host
|
30
|
+
DEFAULT_BLOG_HOST = nil
|
31
|
+
|
32
|
+
# Disable debug output by default
|
33
|
+
DEFAULT_DEBUG = false
|
34
|
+
|
35
|
+
# @private
|
36
|
+
attr_accessor *VALID_OPTIONS_KEYS
|
37
|
+
|
38
|
+
# When this module is extended, set all configuration options to their default values
|
39
|
+
def self.extended(base)
|
40
|
+
base.reset
|
41
|
+
end
|
42
|
+
|
43
|
+
# Convenience method to allow configuration options to be set in a block
|
44
|
+
def configure
|
45
|
+
yield self
|
46
|
+
end
|
47
|
+
|
48
|
+
# Create a hash of options and their values
|
49
|
+
def options
|
50
|
+
{}.tap{ |options| VALID_OPTIONS_KEYS.each{|k| options[k] = send(k) } }
|
51
|
+
end
|
52
|
+
|
53
|
+
# Reset all configuration options to defaults
|
54
|
+
def reset
|
55
|
+
self.consumer_key = DEFAULT_CONSUMER_KEY
|
56
|
+
self.consumer_secret = DEFAULT_CONSUMER_SECRET
|
57
|
+
self.endpoint = DEFAULT_ENDPOINT
|
58
|
+
self.oauth_token = DEFAULT_OAUTH_TOKEN
|
59
|
+
self.oauth_token_secret = DEFAULT_OAUTH_TOKEN_SECRET
|
60
|
+
self.blog_host = DEFAULT_BLOG_HOST
|
61
|
+
self.debug = DEFAULT_DEBUG
|
62
|
+
self
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday/response/raise_http_4xx'
|
3
|
+
require 'faraday/response/raise_http_5xx'
|
4
|
+
|
5
|
+
module TumblrOAuth
|
6
|
+
# @private
|
7
|
+
module Connection
|
8
|
+
private
|
9
|
+
|
10
|
+
def connection
|
11
|
+
options = {
|
12
|
+
:headers => {'Accept' => "application/json"},
|
13
|
+
:url => endpoint,
|
14
|
+
}
|
15
|
+
|
16
|
+
Faraday.new(options) do |builder|
|
17
|
+
builder.use Faraday::Request::OAuth, authentication if authenticated?
|
18
|
+
builder.use Faraday::Request::UrlEncoded
|
19
|
+
builder.use Faraday::Response::Mashify
|
20
|
+
builder.use Faraday::Response::ParseJson
|
21
|
+
builder.use Faraday::Response::Logger if debug
|
22
|
+
builder.use Faraday::Response::RaiseHttp4xx
|
23
|
+
builder.use Faraday::Response::RaiseHttp4xx
|
24
|
+
builder.adapter Faraday.default_adapter
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module TumblrOAuth
|
2
|
+
# Custom error class for rescuing from all Twitter errors
|
3
|
+
class Error < StandardError
|
4
|
+
attr_reader :http_headers
|
5
|
+
|
6
|
+
def initialize(message, http_headers)
|
7
|
+
@http_headers = Hash[http_headers]
|
8
|
+
super message
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Raised when Tumblr returns the HTTP status code 400
|
13
|
+
class BadRequest < Error; end
|
14
|
+
|
15
|
+
# Raised when Tumblr returns the HTTP status code 401
|
16
|
+
class Unauthorized < Error; end
|
17
|
+
|
18
|
+
# Raised when Tumblr returns the HTTP status code 403
|
19
|
+
class Forbidden < Error; end
|
20
|
+
|
21
|
+
# Raised when Tumblr returns the HTTP status code 404
|
22
|
+
class NotFound < Error; end
|
23
|
+
|
24
|
+
# Raised when Tumblr returns the HTTP status code 406
|
25
|
+
class NotAcceptable < Error; end
|
26
|
+
|
27
|
+
# Raised when Tumblr returns the HTTP status code 500
|
28
|
+
class InternalServerError < Error; end
|
29
|
+
|
30
|
+
# Raised when Tumblr returns the HTTP status code 502
|
31
|
+
class BadGateway < Error; end
|
32
|
+
|
33
|
+
# Raised when Tumblr returns the HTTP status code 503
|
34
|
+
class ServiceUnavailable < Error; end
|
35
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module TumblrOAuth
|
2
|
+
module Request
|
3
|
+
# Perform an HTTP GET request
|
4
|
+
def get(path, options={})
|
5
|
+
request(:get, path, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Perform an HTTP POST request
|
9
|
+
def post(path, options={})
|
10
|
+
request(:post, path, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Perform an HTTP PUT request
|
14
|
+
def put(path, options={})
|
15
|
+
request(:put, path, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Perform an HTTP DELETE request
|
19
|
+
def delete(path, options={})
|
20
|
+
request(:delete, path, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Perform an HTTP request
|
26
|
+
def request(method, path, options={})
|
27
|
+
response = connection.send(method) do |request|
|
28
|
+
case method
|
29
|
+
when :get, :delete
|
30
|
+
request.url(path, options)
|
31
|
+
when :post, :put
|
32
|
+
request.path = path
|
33
|
+
request.body = options unless options.empty?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
response.body
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'tumblr-oauth'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
TumblrOAuth.configure do |config|
|
12
|
+
config.consumer_key = ''
|
13
|
+
config.consumer_secret = ''
|
14
|
+
config.oauth_token = ''
|
15
|
+
config.oauth_token_secret = ''
|
16
|
+
config.blog_host = ''
|
17
|
+
config.debug = false
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TumblrOAuth::Client::Blog do
|
4
|
+
let(:client) { TumblrOAuth.client }
|
5
|
+
let(:non_authorized_client) { TumblrOAuth.client(:consumer_key => nil) }
|
6
|
+
let(:bad_oauth_client) { TumblrOAuth.client(:consumer_key => 'sdfsfdsf') }
|
7
|
+
|
8
|
+
describe '#info' do
|
9
|
+
it 'should not raise error' do
|
10
|
+
# TODO: stub request
|
11
|
+
expect { client.info }.to_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should raise error if consumer key is bad' do
|
15
|
+
# TODO: stub request
|
16
|
+
expect { bad_oauth_client.info }.to raise_error(TumblrOAuth::Unauthorized)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#avatar' do
|
21
|
+
it 'should not raise error' do
|
22
|
+
# TODO: stub rquest
|
23
|
+
expect { client.avatar }.to_not raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#followers' do
|
28
|
+
it 'should not raise error' do
|
29
|
+
# TODO: stub request
|
30
|
+
expect { client.followers }.to_not raise_error
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should raise error if oauth data are bad' do
|
34
|
+
# TODO: stub request
|
35
|
+
expect { non_authorized_client.followers }.to raise_error(TumblrOAuth::Unauthorized)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TumblrOAuth::Configuration do
|
4
|
+
it 'should has setup default end point' do
|
5
|
+
TumblrOAuth.endpoint.should == TumblrOAuth::Configuration::DEFAULT_ENDPOINT
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should set default end point to http://api.tumblr.com/v2' do
|
9
|
+
TumblrOAuth.endpoint.should == 'http://api.tumblr.com/v2'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should set consumer key to nil by default' do
|
13
|
+
TumblrOAuth.consumer_key.should be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should set consumer secret to nil by default' do
|
17
|
+
TumblrOAuth.consumer_secret.should be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should set oauth token to nil by default' do
|
21
|
+
TumblrOAuth.oauth_token.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should set oauth token secret to nil by default' do
|
25
|
+
TumblrOAuth.oauth_token_secret.should be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should set blog host to nil by default' do
|
29
|
+
TumblrOAuth.blog_host.should be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should allow to set own values for consumer key and secret' do
|
33
|
+
consumer_key = 'consumer_key'
|
34
|
+
consumer_secret = 'consumer_secret'
|
35
|
+
|
36
|
+
TumblrOAuth.configure do |config|
|
37
|
+
config.consumer_key = consumer_key
|
38
|
+
config.consumer_secret = consumer_secret
|
39
|
+
end
|
40
|
+
|
41
|
+
TumblrOAuth.consumer_key.should == consumer_key
|
42
|
+
TumblrOAuth.consumer_secret.should == consumer_secret
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "TumblrOauth" do
|
4
|
+
it 'should loads success' do
|
5
|
+
expect { TumblrOAuth }.to_not raise_error
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'client' do
|
9
|
+
it 'should create Client instance' do
|
10
|
+
TumblrOAuth.client.should be_a(TumblrOAuth::Client)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should delegate uknown methods to Client' do
|
14
|
+
test_string = 'test string'
|
15
|
+
TumblrOAuth::Client.send :define_method, :test_method do
|
16
|
+
return test_string
|
17
|
+
end
|
18
|
+
|
19
|
+
TumblrOAuth.test_method.should == test_string
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should corretly answer for respond_to for Client methods' do
|
23
|
+
TumblrOAuth::Client.send :define_method, :test_method do
|
24
|
+
return 'test string'
|
25
|
+
end
|
26
|
+
|
27
|
+
TumblrOAuth.should respond_to(:test_method)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tumblr-oauth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ildar Shaynurov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-21 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hashie
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: faraday
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.7.4
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: faraday_middleware
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.7.0
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: multi_json
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.0.0
|
57
|
+
type: :runtime
|
58
|
+
version_requirements: *id004
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: simple_oauth
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 0.1.5
|
68
|
+
type: :runtime
|
69
|
+
version_requirements: *id005
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: guard
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id006
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rake
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0.9"
|
90
|
+
type: :development
|
91
|
+
version_requirements: *id007
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
name: rspec
|
94
|
+
prerelease: false
|
95
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ~>
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.3.0
|
101
|
+
type: :development
|
102
|
+
version_requirements: *id008
|
103
|
+
description: A Ruby wrapper for Tumblr AOuth API
|
104
|
+
email: shaynurov@gmail.com
|
105
|
+
executables: []
|
106
|
+
|
107
|
+
extensions: []
|
108
|
+
|
109
|
+
extra_rdoc_files: []
|
110
|
+
|
111
|
+
files:
|
112
|
+
- .document
|
113
|
+
- .gitignore
|
114
|
+
- .rspec
|
115
|
+
- Gemfile
|
116
|
+
- Gemfile.lock
|
117
|
+
- Guardfile
|
118
|
+
- LICENSE.txt
|
119
|
+
- README.md
|
120
|
+
- Rakefile
|
121
|
+
- lib/faraday/response/raise_http_4xx.rb
|
122
|
+
- lib/faraday/response/raise_http_5xx.rb
|
123
|
+
- lib/tumblr-oauth.rb
|
124
|
+
- lib/tumblr-oauth/authentication.rb
|
125
|
+
- lib/tumblr-oauth/client.rb
|
126
|
+
- lib/tumblr-oauth/client/blog.rb
|
127
|
+
- lib/tumblr-oauth/client/user.rb
|
128
|
+
- lib/tumblr-oauth/configuration.rb
|
129
|
+
- lib/tumblr-oauth/connection.rb
|
130
|
+
- lib/tumblr-oauth/error.rb
|
131
|
+
- lib/tumblr-oauth/request.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
- spec/tumblr-oauth/client/blog_spec.rb
|
134
|
+
- spec/tumblr-oauth/client_spec.rb
|
135
|
+
- spec/tumblr-oauth/configuration_spec.rb
|
136
|
+
- spec/tumblr-oauth_spec.rb
|
137
|
+
homepage: https://github.com/shir/tumblr-oauth
|
138
|
+
licenses: []
|
139
|
+
|
140
|
+
post_install_message:
|
141
|
+
rdoc_options: []
|
142
|
+
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: "0"
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: 1.3.6
|
157
|
+
requirements: []
|
158
|
+
|
159
|
+
rubyforge_project:
|
160
|
+
rubygems_version: 1.8.5
|
161
|
+
signing_key:
|
162
|
+
specification_version: 3
|
163
|
+
summary: Tumblr library with OAuth support
|
164
|
+
test_files:
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
- spec/tumblr-oauth/client/blog_spec.rb
|
167
|
+
- spec/tumblr-oauth/client_spec.rb
|
168
|
+
- spec/tumblr-oauth/configuration_spec.rb
|
169
|
+
- spec/tumblr-oauth_spec.rb
|