rest-more 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -5
- data/.travis.yml +4 -6
- data/CHANGES.md +16 -0
- data/Gemfile +6 -3
- data/README.md +43 -129
- data/Rakefile +2 -3
- data/example/multi.rb +12 -23
- data/example/rails3/Gemfile +2 -1
- data/example/rails3/app/controllers/application_controller.rb +9 -4
- data/example/rails3/test/functional/application_controller_test.rb +1 -1
- data/example/rainbows.rb +48 -7
- data/example/simple.rb +3 -3
- data/example/sinatra/config.ru +1 -1
- data/lib/rest-core/client/dropbox.rb +19 -27
- data/lib/rest-core/client/facebook.rb +28 -51
- data/lib/rest-core/client/facebook/rails_util.rb +13 -11
- data/lib/rest-core/client/github.rb +13 -21
- data/lib/rest-core/client/linkedin.rb +19 -27
- data/lib/rest-core/client/twitter.rb +13 -21
- data/lib/rest-more.rb +0 -2
- data/lib/rest-more/test.rb +11 -0
- data/lib/rest-more/version.rb +1 -1
- data/rest-more.gemspec +7 -41
- data/task/.gitignore +1 -0
- data/task/gemgem.rb +267 -0
- data/test/dropbox/test_api.rb +1 -1
- data/test/facebook/config/rest-core.yaml +1 -1
- data/test/facebook/test_api.rb +10 -14
- data/test/facebook/test_error.rb +2 -2
- data/test/facebook/test_handler.rb +6 -6
- data/test/facebook/test_load_config.rb +4 -4
- data/test/facebook/test_misc.rb +3 -3
- data/test/facebook/test_old.rb +2 -37
- data/test/facebook/test_page.rb +26 -24
- data/test/facebook/test_parse.rb +1 -1
- data/test/facebook/test_serialize.rb +3 -19
- data/test/facebook/test_timeout.rb +6 -2
- data/test/twitter/test_api.rb +1 -1
- metadata +6 -40
- data/doc/ToC.md +0 -9
- data/doc/dependency.md +0 -4
- data/doc/design.md +0 -4
- data/doc/rest-graph.md +0 -4
- data/example/async.rb +0 -89
- data/example/facebook.rb +0 -13
- data/example/rails2/Gemfile +0 -22
- data/example/rails2/README +0 -4
- data/example/rails2/Rakefile +0 -11
- data/example/rails2/app/controllers/application_controller.rb +0 -138
- data/example/rails2/app/views/application/helper.html.erb +0 -1
- data/example/rails2/config/boot.rb +0 -130
- data/example/rails2/config/environment.rb +0 -25
- data/example/rails2/config/environments/development.rb +0 -17
- data/example/rails2/config/environments/production.rb +0 -28
- data/example/rails2/config/environments/test.rb +0 -30
- data/example/rails2/config/initializers/cookie_verification_secret.rb +0 -7
- data/example/rails2/config/initializers/new_rails_defaults.rb +0 -21
- data/example/rails2/config/initializers/session_store.rb +0 -15
- data/example/rails2/config/preinitializer.rb +0 -23
- data/example/rails2/config/rest-core.yaml +0 -12
- data/example/rails2/config/routes.rb +0 -43
- data/example/rails2/log +0 -0
- data/example/rails2/test/functional/application_controller_test.rb +0 -259
- data/example/rails2/test/test_helper.rb +0 -18
- data/example/rails2/test/unit/rails_util_test.rb +0 -49
- data/lib/rest-core/client/bing.rb +0 -102
- data/lib/rest-core/client/bing/rails_util.rb +0 -13
- data/lib/rest-core/client/mixi.rb +0 -60
- data/lib/rest-core/client/mixi/rails_util.rb +0 -13
- data/test/bing/test_api.rb +0 -34
- data/test/facebook/test_cache.rb +0 -58
- data/test/mixi/test_api.rb +0 -20
@@ -1,18 +0,0 @@
|
|
1
|
-
|
2
|
-
ENV["RAILS_ENV"] = "test"
|
3
|
-
require File.expand_path('../../config/environment', __FILE__)
|
4
|
-
begin
|
5
|
-
require 'rails/test_help'
|
6
|
-
rescue LoadError # for rails2
|
7
|
-
require 'test_help'
|
8
|
-
end
|
9
|
-
|
10
|
-
class ActiveSupport::TestCase
|
11
|
-
def normalize_query query, amp='&'
|
12
|
-
'?' + query[1..-1].split(amp).sort.join(amp)
|
13
|
-
end
|
14
|
-
|
15
|
-
def normalize_url url, amp='&'
|
16
|
-
url.sub(/\?.+/){ |query| normalize_query(query, amp) }
|
17
|
-
end
|
18
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'test_helper'
|
3
|
-
require 'rr'
|
4
|
-
|
5
|
-
class RailsUtilTest < ActiveSupport::TestCase
|
6
|
-
include RR::Adapters::TestUnit
|
7
|
-
|
8
|
-
def setup_mock url
|
9
|
-
@controller = Class.new do
|
10
|
-
def self.helper dummy ; end
|
11
|
-
def self.rescue_from dummy, _; end
|
12
|
-
include RestCore::Facebook::RailsUtil
|
13
|
-
end.new
|
14
|
-
mock(@controller).rc_facebook_in_canvas?{ false }
|
15
|
-
mock(@controller).request{
|
16
|
-
mock(Object.new).url{ url }
|
17
|
-
}
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_rest_graph_normalized_request_uri_0
|
21
|
-
setup_mock( 'http://test.com/?code=123&lang=en')
|
22
|
-
assert_equal('http://test.com/?lang=en',
|
23
|
-
@controller.send(:rc_facebook_normalized_request_uri))
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_rest_graph_normalized_request_uri_1
|
27
|
-
setup_mock( 'http://test.com/?lang=en&code=123')
|
28
|
-
assert_equal('http://test.com/?lang=en',
|
29
|
-
@controller.send(:rc_facebook_normalized_request_uri))
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_rest_graph_normalized_request_uri_2
|
33
|
-
setup_mock( 'http://test.com/?session=abc&lang=en&code=123')
|
34
|
-
assert_equal('http://test.com/?lang=en',
|
35
|
-
@controller.send(:rc_facebook_normalized_request_uri))
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_rest_graph_normalized_request_uri_3
|
39
|
-
setup_mock( 'http://test.com/?code=123')
|
40
|
-
assert_equal('http://test.com/',
|
41
|
-
@controller.send(:rc_facebook_normalized_request_uri))
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_rest_graph_normalized_request_uri_4
|
45
|
-
setup_mock( 'http://test.com/?signed_request=abc&code=123')
|
46
|
-
assert_equal('http://test.com/',
|
47
|
-
@controller.send(:rc_facebook_normalized_request_uri))
|
48
|
-
end
|
49
|
-
end
|
@@ -1,102 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-core'
|
3
|
-
|
4
|
-
# http://msdn.microsoft.com/en-us/library/dd250846.aspx
|
5
|
-
RestCore::Bing = RestCore::Builder.client(:AppId) do
|
6
|
-
s = RestCore
|
7
|
-
use s::Timeout , 10
|
8
|
-
|
9
|
-
use s::DefaultSite , 'http://api.bing.net/json.aspx'
|
10
|
-
use s::DefaultHeaders, {'Accept' => 'application/json'}
|
11
|
-
use s::DefaultQuery , {}
|
12
|
-
|
13
|
-
use s::CommonLogger , nil
|
14
|
-
use s::Cache , nil, 600 do
|
15
|
-
use s::ErrorHandler, lambda{ |env|
|
16
|
-
if env[s::ASYNC]
|
17
|
-
if env[s::RESPONSE_BODY].kind_of?(::Exception)
|
18
|
-
env
|
19
|
-
else
|
20
|
-
env.merge(s::RESPONSE_BODY => ::RestCore::Bing::Error.call(env))
|
21
|
-
end
|
22
|
-
else
|
23
|
-
raise ::RestCore::Bing::Error.call(env)
|
24
|
-
end}
|
25
|
-
use s::ErrorDetector, lambda{ |env|
|
26
|
-
if env[s::RESPONSE_BODY].kind_of?(Hash) &&
|
27
|
-
(res = env[s::RESPONSE_BODY]['SearchResponse']).kind_of?(Hash)
|
28
|
-
res['Errors']
|
29
|
-
end}
|
30
|
-
use s::JsonDecode , true
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# http://msdn.microsoft.com/en-us/library/dd251042.aspx
|
35
|
-
class RestCore::Bing::Error < RestCore::Error
|
36
|
-
include RestCore
|
37
|
-
class MissingParameter < Bing::Error; end
|
38
|
-
class InvalidParameter < Bing::Error; end
|
39
|
-
class QueryTooLong < Bing::Error; end
|
40
|
-
class AppIdNotFunctioning < Bing::Error; end
|
41
|
-
class ExceededLimit < Bing::Error; end
|
42
|
-
class NoAccess < Bing::Error; end
|
43
|
-
class ResultsTemporarilyUnavailable < Bing::Error; end
|
44
|
-
class ServiceTemporarilyUnavailable < Bing::Error; end
|
45
|
-
class SourceTypeError < Bing::Error; end
|
46
|
-
|
47
|
-
attr_reader :error, :code, :url
|
48
|
-
def initialize error, code, url=''
|
49
|
-
@error, @code, @url = error, code, url
|
50
|
-
super("[#{code}] #{error.inspect} from #{url}")
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.call env
|
54
|
-
error, url = env[RESPONSE_BODY], Middleware.request_uri(env)
|
55
|
-
code = extract_error_code(error)
|
56
|
-
|
57
|
-
return new(error, code, url) unless code
|
58
|
-
|
59
|
-
case code
|
60
|
-
when 1001; MissingParameter
|
61
|
-
when 1002; InvalidParameter
|
62
|
-
when 1005; QueryTooLong
|
63
|
-
when 2001; AppIdNotFunctioning
|
64
|
-
when 2002; ExceededLimit
|
65
|
-
when 2003; NoAccess
|
66
|
-
when 3001; ResultsTemporarilyUnavailable
|
67
|
-
when 3002; ServiceTemporarilyUnavailable
|
68
|
-
when 4001; SourceTypeError
|
69
|
-
end.new(error, code, url)
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.extract_error_code error
|
73
|
-
code = error.kind_of?(Hash) &&
|
74
|
-
(error = error['SearchResponse']).kind_of?(Hash) &&
|
75
|
-
(error = error['Errors'] ).kind_of?(Array) &&
|
76
|
-
(error = error[0] ).kind_of?(Hash) &&
|
77
|
-
(error = error['Code'] )
|
78
|
-
|
79
|
-
code && code.to_i
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
module RestCore::Bing::Client
|
84
|
-
def query
|
85
|
-
{'AppId' => self.AppId,
|
86
|
-
'JsonType' => 'raw' ,
|
87
|
-
'Version' => '2.2' }
|
88
|
-
end
|
89
|
-
|
90
|
-
def search_image term, query={}, opts={}
|
91
|
-
get('', {:Query => term, :Sources => 'Image'}.merge(query), opts)[
|
92
|
-
'SearchResponse']['Image']['Results'] || []
|
93
|
-
end
|
94
|
-
|
95
|
-
def search_image_urls term, query={}, opts={}
|
96
|
-
search_image(term, query, opts).map{ |i| i['MediaUrl'] }
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
RestCore::Bing.send(:include, RestCore::Bing::Client)
|
101
|
-
require 'rest-core/client/bing/rails_util' if
|
102
|
-
Object.const_defined?(:Rails)
|
@@ -1,13 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-core/util/rails_util_util'
|
3
|
-
|
4
|
-
module RestCore::Bing::DefaultAttributes
|
5
|
-
def default_log_method; Rails.logger.method(:debug); end
|
6
|
-
def default_cache ; Rails.cache ; end
|
7
|
-
end
|
8
|
-
|
9
|
-
module RestCore::Bing::RailsUtil
|
10
|
-
include RestCore::RailsUtilUtil
|
11
|
-
end
|
12
|
-
|
13
|
-
RestCore::Bing::RailsUtil.init(Rails)
|
@@ -1,60 +0,0 @@
|
|
1
|
-
|
2
|
-
# http://developer.mixi.co.jp/connect/mixi_graph_api/
|
3
|
-
RestCore::Mixi = RestCore::Builder.client(
|
4
|
-
:data, :consumer_key, :consumer_secret, :redirect_uri) do
|
5
|
-
s = RestCore
|
6
|
-
use s::Timeout , 10
|
7
|
-
|
8
|
-
use s::DefaultSite , 'http://api.mixi-platform.com/'
|
9
|
-
use s::DefaultHeaders, {'Accept' => 'application/json'}
|
10
|
-
|
11
|
-
use s::Oauth2Header , 'OAuth', nil
|
12
|
-
|
13
|
-
use s::CommonLogger , nil
|
14
|
-
use s::Cache , nil, 600 do
|
15
|
-
use s::ErrorHandler , lambda{ |env| p env }
|
16
|
-
use s::ErrorDetectorHttp
|
17
|
-
use s::JsonDecode , true
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module RestCore::Mixi::Client
|
22
|
-
include RestCore
|
23
|
-
|
24
|
-
def me query={}, opts={}
|
25
|
-
get('2/people/@me/@self', query, opts)
|
26
|
-
end
|
27
|
-
|
28
|
-
def access_token
|
29
|
-
data['access_token'] if data.kind_of?(Hash)
|
30
|
-
end
|
31
|
-
|
32
|
-
def access_token= token
|
33
|
-
data['access_token'] = token if data.kind_of?(Hash)
|
34
|
-
end
|
35
|
-
|
36
|
-
def authorize_url queries={}
|
37
|
-
url('https://mixi.jp/connect_authorize.pl',
|
38
|
-
{:client_id => consumer_key,
|
39
|
-
:response_type => 'code',
|
40
|
-
:scope => 'r_profile'}.merge(queries))
|
41
|
-
end
|
42
|
-
|
43
|
-
def authorize! payload={}, opts={}
|
44
|
-
pl = {:client_id => consumer_key ,
|
45
|
-
:client_secret => consumer_secret,
|
46
|
-
:redirect_uri => redirect_uri ,
|
47
|
-
:grant_type => 'authorization_code'}.merge(payload)
|
48
|
-
|
49
|
-
self.data = post('https://secure.mixi-platform.com/2/token', pl, {}, opts)
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
def default_data
|
54
|
-
{}
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
RestCore::Mixi.send(:include, RestCore::Mixi::Client)
|
59
|
-
require 'rest-core/client/mixi/rails_util' if
|
60
|
-
Object.const_defined?(:Rails)
|
@@ -1,13 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-core/util/rails_util_util'
|
3
|
-
|
4
|
-
module RestCore::Mixi::DefaultAttributes
|
5
|
-
def default_log_method; Rails.logger.method(:debug); end
|
6
|
-
def default_cache ; Rails.cache ; end
|
7
|
-
end
|
8
|
-
|
9
|
-
module RestCore::Mixi::RailsUtil
|
10
|
-
include RestCore::RailsUtilUtil
|
11
|
-
end
|
12
|
-
|
13
|
-
RestCore::Mixi::RailsUtil.init(Rails)
|
data/test/bing/test_api.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-more/test'
|
3
|
-
|
4
|
-
describe RC::Bing do
|
5
|
-
after do
|
6
|
-
WebMock.reset!
|
7
|
-
RR.verify
|
8
|
-
end
|
9
|
-
|
10
|
-
def stub_with body, query={}
|
11
|
-
stub_request(:get, 'http://api.bing.net/json.aspx').
|
12
|
-
with(:query => {'JsonType' => 'raw', 'Version' => '2.2'}.merge(query)).
|
13
|
-
to_return(:body => body)
|
14
|
-
end
|
15
|
-
|
16
|
-
should 'get right' do
|
17
|
-
stub_with('{"status":"OK"}')
|
18
|
-
RC::Bing.new.get('').should.eq({'status' => 'OK'})
|
19
|
-
end
|
20
|
-
|
21
|
-
should 'be able to set AppId' do
|
22
|
-
RC::Bing.new(:AppId => 'QQ').AppId.should.eq 'QQ'
|
23
|
-
end
|
24
|
-
|
25
|
-
should 'use AppId for requests' do
|
26
|
-
stub_with('{}', 'AppId' => 'zz')
|
27
|
-
RC::Bing.new(:AppId => 'zz').get('').should.eq({})
|
28
|
-
end
|
29
|
-
|
30
|
-
should 'raise correct error' do
|
31
|
-
stub_with('{"SearchResponse":{"Errors":[{"Code":2003}]}}')
|
32
|
-
lambda{RC::Bing.new.get('')}.should.raise(RC::Bing::Error::NoAccess)
|
33
|
-
end
|
34
|
-
end
|
data/test/facebook/test_cache.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-more/test'
|
3
|
-
|
4
|
-
describe RC::Facebook do
|
5
|
-
after do
|
6
|
-
WebMock.reset!
|
7
|
-
RR.verify
|
8
|
-
end
|
9
|
-
|
10
|
-
describe 'cache' do
|
11
|
-
before do
|
12
|
-
@url, @body = "https://graph.facebook.com/cache", '{"message":"ok"}'
|
13
|
-
@cache_key = Digest::MD5.hexdigest(@url)
|
14
|
-
@cache = {}
|
15
|
-
@rg = RC::Facebook.new(:cache => @cache, :json_decode => false)
|
16
|
-
stub_request(:get, @url).to_return(:body => @body).times(1)
|
17
|
-
end
|
18
|
-
|
19
|
-
should 'enable cache if passing cache' do
|
20
|
-
3.times{ @rg.get('cache').should.eq @body }
|
21
|
-
@cache.should.eq({@cache_key => @body})
|
22
|
-
end
|
23
|
-
|
24
|
-
should 'respect expires_in' do
|
25
|
-
mock(@cache).method(:store){ mock!.arity{ -3 } }
|
26
|
-
mock(@cache).store(@cache_key, @body, :expires_in => 3)
|
27
|
-
@rg.get('cache', {}, :expires_in => 3).should.eq @body
|
28
|
-
end
|
29
|
-
|
30
|
-
should 'update cache if there is cache option set to false' do
|
31
|
-
@rg.get('cache') .should.eq @body
|
32
|
-
stub_request(:get, @url).to_return(:body => @body.reverse).times(2)
|
33
|
-
@rg.get('cache') .should.eq @body
|
34
|
-
@rg.get('cache', {}, :cache => false).should.eq @body.reverse
|
35
|
-
@rg.get('cache') .should.eq @body.reverse
|
36
|
-
@rg.cache = nil
|
37
|
-
@rg.get('cache', {}, :cache => false).should.eq @body.reverse
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
should 'not cache post/put/delete' do
|
42
|
-
[:put, :post, :delete].each{ |meth|
|
43
|
-
url, body = "https://graph.facebook.com/cache", '{"message":"ok"}'
|
44
|
-
stub_request(meth, url).to_return(:body => body).times(3)
|
45
|
-
|
46
|
-
cache = {}
|
47
|
-
rg = RC::Facebook.new(:cache => cache)
|
48
|
-
3.times{
|
49
|
-
if meth == :delete
|
50
|
-
rg.send(meth, 'cache').should.eq({'message' => 'ok'})
|
51
|
-
else
|
52
|
-
rg.send(meth, 'cache', 'payload').should.eq({'message' => 'ok'})
|
53
|
-
end
|
54
|
-
}
|
55
|
-
cache.should.eq({})
|
56
|
-
}
|
57
|
-
end
|
58
|
-
end
|
data/test/mixi/test_api.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'rest-more/test'
|
3
|
-
|
4
|
-
describe RC::Mixi do
|
5
|
-
after do
|
6
|
-
WebMock.reset!
|
7
|
-
RR.verify
|
8
|
-
end
|
9
|
-
|
10
|
-
should 'get right' do
|
11
|
-
stub_request(:get, 'http://api.mixi-platform.com/me').
|
12
|
-
to_return(:body => '{"status": "OK"}')
|
13
|
-
|
14
|
-
RC::Mixi.new.get('me').should.eq({'status' => 'OK'})
|
15
|
-
end
|
16
|
-
|
17
|
-
should 'be able to set access_token' do
|
18
|
-
RC::Mixi.new(:access_token => 'QQ').access_token.should.eq 'QQ'
|
19
|
-
end
|
20
|
-
end
|