rest-core 0.4.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.travis.yml +2 -7
  2. data/CHANGES.md +13 -2
  3. data/Gemfile +0 -3
  4. data/README.md +30 -34
  5. data/Rakefile +5 -46
  6. data/lib/rest-core.rb +0 -6
  7. data/lib/rest-core/version.rb +1 -1
  8. data/rest-core.gemspec +5 -83
  9. metadata +11 -87
  10. data/example/rails2/Gemfile +0 -21
  11. data/example/rails2/README +0 -4
  12. data/example/rails2/Rakefile +0 -11
  13. data/example/rails2/app/controllers/application_controller.rb +0 -128
  14. data/example/rails2/app/views/application/helper.html.erb +0 -2
  15. data/example/rails2/config/boot.rb +0 -130
  16. data/example/rails2/config/environment.rb +0 -15
  17. data/example/rails2/config/environments/development.rb +0 -17
  18. data/example/rails2/config/environments/production.rb +0 -28
  19. data/example/rails2/config/environments/test.rb +0 -30
  20. data/example/rails2/config/initializers/cookie_verification_secret.rb +0 -7
  21. data/example/rails2/config/initializers/new_rails_defaults.rb +0 -21
  22. data/example/rails2/config/initializers/session_store.rb +0 -15
  23. data/example/rails2/config/preinitializer.rb +0 -23
  24. data/example/rails2/config/rest-core.yaml +0 -16
  25. data/example/rails2/config/routes.rb +0 -43
  26. data/example/rails2/log +0 -0
  27. data/example/rails2/test/functional/application_controller_test.rb +0 -197
  28. data/example/rails2/test/test_helper.rb +0 -18
  29. data/example/rails2/test/unit/rails_util_test.rb +0 -44
  30. data/example/rails3/Gemfile +0 -20
  31. data/example/rails3/README +0 -4
  32. data/example/rails3/Rakefile +0 -7
  33. data/example/rails3/app/controllers/application_controller.rb +0 -128
  34. data/example/rails3/app/views/application/helper.html.erb +0 -2
  35. data/example/rails3/config.ru +0 -4
  36. data/example/rails3/config/application.rb +0 -23
  37. data/example/rails3/config/environment.rb +0 -5
  38. data/example/rails3/config/environments/development.rb +0 -26
  39. data/example/rails3/config/environments/production.rb +0 -49
  40. data/example/rails3/config/environments/test.rb +0 -30
  41. data/example/rails3/config/initializers/secret_token.rb +0 -7
  42. data/example/rails3/config/initializers/session_store.rb +0 -8
  43. data/example/rails3/config/rest-core.yaml +0 -16
  44. data/example/rails3/config/routes.rb +0 -5
  45. data/example/rails3/test/functional/application_controller_test.rb +0 -197
  46. data/example/rails3/test/test_helper.rb +0 -18
  47. data/example/rails3/test/unit/rails_util_test.rb +0 -44
  48. data/example/sinatra/config.ru +0 -16
  49. data/lib/rest-core/client/facebook.rb +0 -251
  50. data/lib/rest-core/client/facebook/rails_util.rb +0 -333
  51. data/lib/rest-core/client/flurry.rb +0 -96
  52. data/lib/rest-core/client/flurry/rails_util.rb +0 -74
  53. data/lib/rest-core/client/github.rb +0 -18
  54. data/lib/rest-core/client/linkedin.rb +0 -59
  55. data/lib/rest-core/client/mixi.rb +0 -47
  56. data/lib/rest-core/client/twitter.rb +0 -101
  57. data/test/client/facebook/config/rest-core.yaml +0 -8
  58. data/test/client/facebook/test_api.rb +0 -97
  59. data/test/client/facebook/test_cache.rb +0 -58
  60. data/test/client/facebook/test_default.rb +0 -23
  61. data/test/client/facebook/test_error.rb +0 -65
  62. data/test/client/facebook/test_handler.rb +0 -84
  63. data/test/client/facebook/test_load_config.rb +0 -39
  64. data/test/client/facebook/test_misc.rb +0 -72
  65. data/test/client/facebook/test_oauth.rb +0 -38
  66. data/test/client/facebook/test_old.rb +0 -114
  67. data/test/client/facebook/test_page.rb +0 -106
  68. data/test/client/facebook/test_parse.rb +0 -128
  69. data/test/client/facebook/test_serialize.rb +0 -43
  70. data/test/client/facebook/test_timeout.rb +0 -22
  71. data/test/client/flurry/test_metrics.rb +0 -83
  72. data/test/client/twitter/test_api.rb +0 -37
@@ -1,58 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::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 = RestCore::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 = RestCore::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
@@ -1,23 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook do
5
- should 'honor default attributes' do
6
- RestCore::Facebook.members.reject{ |name|
7
- name.to_s =~ /method$|handler$|detector$/ }.each{ |name|
8
- RestCore::Facebook.new.send(name).should ==
9
- RestCore::Facebook.new.send("default_#{name}")
10
- }
11
- end
12
-
13
- should 'use module to override default attributes' do
14
- klass = RestCore::Facebook.dup
15
- klass.send(:include, Module.new do
16
- def default_app_id
17
- '1829'
18
- end
19
- end)
20
-
21
- klass.new.app_id.should.eq('1829')
22
- end
23
- end
@@ -1,65 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook::Error do
5
- after do
6
- WebMock.reset!
7
- RR.verify
8
- end
9
-
10
- should 'have the right ancestors' do
11
- RestCore::Facebook::Error::AccessToken.should.lt RestCore::Facebook::Error
12
-
13
- RestCore::Facebook::Error::InvalidAccessToken.should.lt \
14
- RestCore::Facebook::Error::AccessToken
15
-
16
- RestCore::Facebook::Error::MissingAccessToken.should.lt \
17
- RestCore::Facebook::Error::AccessToken
18
- end
19
-
20
- def error2env hash
21
- {RestCore::RESPONSE_BODY => hash,
22
- RestCore::REQUEST_PATH => '/' ,
23
- RestCore::REQUEST_QUERY => {}}
24
- end
25
-
26
- should 'parse right' do
27
- %w[OAuthInvalidTokenException OAuthException].each{ |type|
28
- RestCore::Facebook::Error.call(error2env('error' => {'type' => type})).
29
- should.kind_of?(RestCore::Facebook::Error::InvalidAccessToken)
30
- }
31
-
32
- RestCore::Facebook::Error.call(
33
- error2env('error'=>{'type' =>'QueryParseException',
34
- 'message'=>'An active access token..'})).
35
- should.kind_of?(RestCore::Facebook::Error::MissingAccessToken)
36
-
37
- RestCore::Facebook::Error.call(
38
- error2env('error'=>{'type' =>'QueryParseException',
39
- 'message'=>'Oh active access token..'})).
40
- should.not.kind_of?(RestCore::Facebook::Error::MissingAccessToken)
41
-
42
- RestCore::Facebook::Error.call(error2env('error_code' => 190)).
43
- should.kind_of?(RestCore::Facebook::Error::InvalidAccessToken)
44
-
45
- RestCore::Facebook::Error.call(error2env('error_code' => 104)).
46
- should.kind_of?(RestCore::Facebook::Error::MissingAccessToken)
47
-
48
- RestCore::Facebook::Error.call(error2env('error_code' => 999)).
49
- should.not.kind_of?(RestCore::Facebook::Error::AccessToken)
50
-
51
- error = RestCore::Facebook::Error.call(error2env(['not a hash']))
52
- error.should.not.kind_of?(RestCore::Facebook::Error::AccessToken)
53
- error.should .kind_of?(RestCore::Facebook::Error)
54
- end
55
-
56
- should 'nuke cache upon errors' do
57
- stub_request(:get, 'https://graph.facebook.com/me').
58
- to_return(:body => '{"error":"wrong"}').times(2)
59
-
60
- rg = RestCore::Facebook.new(:cache => {},
61
- :error_handler => lambda{|env|env})
62
- rg.get('me'); rg.get('me')
63
- rg.cache.values.should.eq []
64
- end
65
- end
@@ -1,84 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook do
5
- after do
6
- WebMock.reset!
7
- RR.verify
8
- end
9
-
10
- describe 'log method' do
11
- should 'log whenever doing network request' do
12
- stub_request(:get, 'https://graph.facebook.com/me').
13
- to_return(:body => '{}')
14
-
15
- logger = []
16
- rg = RestCore::Facebook.new(:log_method => lambda{ |s| logger << [s] })
17
- rg.get('me')
18
-
19
- logger.size.should.eq 1
20
- end
21
- end
22
-
23
- describe 'with Graph API' do
24
- before do
25
- @id = lambda{ |env| env }
26
- @error = '{"error":{"type":"Exception","message":"(#2500)"}}'
27
- @error_hash = RestCore::JsonDecode.json_decode(@error)
28
-
29
- stub_request(:get, 'https://graph.facebook.com/me').
30
- to_return(:body => @error)
31
- end
32
-
33
- should 'call error_handler if error occurred' do
34
- RestCore::Facebook.new(:error_handler => @id).get('me').
35
- should.eq @error_hash
36
- end
37
-
38
- should 'raise ::RestCore::Facebook::Error in default error_handler' do
39
- begin
40
- RestCore::Facebook.new.get('me')
41
- rescue ::RestCore::Facebook::Error => e
42
- e.error .should.eq @error_hash
43
- e.message.should.eq \
44
- "#{@error_hash.inspect} from https://graph.facebook.com/me"
45
- end
46
- end
47
- end
48
-
49
- describe 'with FQL API' do
50
- # Example of an actual response (without newline)
51
- # {"error_code":603,"error_msg":"Unknown table: bad_table",
52
- # "request_args":[{"key":"method","value":"fql.query"},
53
- # {"key":"format","value":"json"},
54
- # {"key":"query","value":
55
- # "SELECT name FROM bad_table WHERE uid=12345"}]}
56
- before do
57
- @id = lambda{ |env| env }
58
- @fql_error = '{"error_code":603,"error_msg":"Unknown table: bad"}'
59
- @fql_error_hash = RestCore::JsonDecode.json_decode(@fql_error)
60
-
61
- @bad_fql_query = 'SELECT name FROM bad_table WHERE uid="12345"'
62
- bad_fql_request = "https://api.facebook.com/method/fql.query?" \
63
- "format=json&query=#{CGI.escape(@bad_fql_query)}"
64
-
65
- stub_request(:get, bad_fql_request).to_return(:body => @fql_error)
66
- end
67
-
68
- should 'call error_handler if error occurred' do
69
- RestCore::Facebook.new(:error_handler => @id).fql(@bad_fql_query).
70
- should.eq @fql_error_hash
71
- end
72
-
73
- should 'raise ::RestCore::Facebook::Error in default error_handler' do
74
- begin
75
- RestCore::Facebook.new.fql(@bad_fql_query)
76
- rescue ::RestCore::Facebook::Error => e
77
- e.error .should.eq @fql_error_hash
78
- e.message.should.start_with?(
79
- "#{@fql_error_hash.inspect} from " \
80
- "https://api.facebook.com/method/fql.query?")
81
- end
82
- end
83
- end
84
- end
@@ -1,39 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- require 'rest-core/util/config'
5
-
6
- describe RestCore::Config do
7
-
8
- before do
9
- @klass = RestCore::Facebook.dup
10
- end
11
-
12
- after do
13
- RR.verify
14
- end
15
-
16
- def check
17
- @klass.default_app_id .should.eq 41829
18
- @klass.default_secret .should.eq 'r41829'.reverse
19
- @klass.default_json_decode.should.eq false
20
- @klass.default_lang .should.eq 'zh-tw'
21
- end
22
-
23
- should 'honor rails config' do
24
- app = Object.new
25
- mock(app).env { 'test' }
26
- mock(app).root{ File.dirname(__FILE__) }
27
- RestCore::Config.load_for_rails(@klass, 'facebook', app)
28
- check
29
- end
30
-
31
- should 'honor config' do
32
- RestCore::Config.load(
33
- @klass,
34
- "#{File.dirname(__FILE__)}/config/rest-core.yaml",
35
- 'test',
36
- 'facebook')
37
- check
38
- end
39
- end
@@ -1,72 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook do
5
- after do
6
- WebMock.reset!
7
- RR.verify
8
- end
9
-
10
- should 'return true in authorized? if there is an access_token' do
11
- RestCore::Facebook.new(:access_token => '1').authorized?.should.eq true
12
- RestCore::Facebook.new(:access_token => nil).authorized?.should.eq false
13
- end
14
-
15
- should 'treat oauth_token as access_token as well' do
16
- rg = RestCore::Facebook.new
17
- hate_facebook = 'why the hell two different name?'
18
- rg.data['oauth_token'] = hate_facebook
19
- rg.authorized?.should.eq true
20
- rg.access_token.should.eq hate_facebook
21
- end
22
-
23
- should 'build correct headers' do
24
- rg = RestCore::Facebook.new(:accept => 'text/html',
25
- :lang => 'zh-tw')
26
-
27
- headers = rg.dry.call(rg.send(:build_env))[RestCore::REQUEST_HEADERS]
28
- headers['Accept' ].should.eq 'text/html'
29
- headers['Accept-Language'].should.eq 'zh-tw'
30
- end
31
-
32
- should 'build empty query string' do
33
- rg = RestCore::Facebook.new
34
- (rg.dry.call(rg.send(:build_env))[RestCore::REQUEST_QUERY] || {}).
35
- should.eq({})
36
- end
37
-
38
- should 'create access_token in query string' do
39
- rg = RestCore::Facebook.new(:access_token => 'token')
40
- (rg.dry.call(rg.send(:build_env))[RestCore::REQUEST_QUERY] || {}).
41
- should.eq({'access_token' => 'token'})
42
- end
43
-
44
- should 'build correct query string' do
45
- rg = RestCore::Facebook.new(:access_token => 'token')
46
- TestHelper.normalize_url(rg.url('', :message => 'hi!!')).
47
- should.eq "#{rg.site}?access_token=token&message=hi%21%21"
48
-
49
- rg.access_token = nil
50
- TestHelper.normalize_url(rg.url('', :message => 'hi!!',
51
- :subject => '(&oh&)')).
52
- should.eq "#{rg.site}?message=hi%21%21&subject=%28%26oh%26%29"
53
- end
54
-
55
- should 'auto decode json' do
56
- rg = RestCore::Facebook.new(:json_decode => true)
57
- stub_request(:get, rg.site).to_return(:body => '[]')
58
- rg.get('').should.eq []
59
- end
60
-
61
- should 'not auto decode json' do
62
- rg = RestCore::Facebook.new(:json_decode => false)
63
- stub_request(:get, rg.site).to_return(:body => '[]')
64
- rg.get('').should.eq '[]'
65
- end
66
-
67
- should 'give attributes' do
68
- RestCore::Facebook.new(:json_decode => false).attributes.
69
- keys.map(&:to_s).sort.should.eq \
70
- RestCore::Facebook.members.map(&:to_s).sort
71
- end
72
- end
@@ -1,38 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook do
5
- before do
6
- @rg = RestCore::Facebook.new(:app_id => '29', :secret => '18')
7
- @uri = 'http://zzz.tw'
8
- end
9
-
10
- after do
11
- WebMock.reset!
12
- end
13
-
14
- should 'return correct oauth url' do
15
- TestHelper.normalize_url(@rg.authorize_url(:redirect_uri => @uri)).
16
- should.eq 'https://graph.facebook.com/oauth/authorize?' \
17
- 'client_id=29&redirect_uri=http%3A%2F%2Fzzz.tw'
18
- end
19
-
20
- should 'do authorizing and parse result and save it in data' do
21
- stub_request(:get, 'https://graph.facebook.com/oauth/access_token?' \
22
- 'client_id=29&client_secret=18&code=zzz&' \
23
- 'redirect_uri=http%3A%2F%2Fzzz.tw').
24
- to_return(:body => 'access_token=baken&expires=2918')
25
-
26
- result = {'access_token' => 'baken', 'expires' => '2918'}
27
-
28
- @rg.authorize!(:redirect_uri => @uri, :code => 'zzz').
29
- should.eq result
30
- @rg.data.should.eq result
31
- end
32
-
33
- should 'not append access_token in authorize_url even presented' do
34
- RestCore::Facebook.new(:access_token => 'do not use me').authorize_url.
35
- should.eq 'https://graph.facebook.com/oauth/authorize'
36
- end
37
-
38
- end
@@ -1,114 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook do
5
- after do
6
- WebMock.reset!
7
- RR.verify
8
- end
9
-
10
- should 'do fql query with/without access_token' do
11
- fql = 'SELECT name FROM likes where id="123"'
12
- query = "format=json&query=#{CGI.escape(fql)}"
13
- stub_request(:get, "https://api.facebook.com/method/fql.query?#{query}").
14
- to_return(:body => '[]')
15
-
16
- RestCore::Facebook.new.fql(fql).should.eq []
17
-
18
- token = 'token'.reverse
19
- stub_request(:get, "https://api.facebook.com/method/fql.query?#{query}" \
20
- "&access_token=#{token}").
21
- to_return(:body => '[]')
22
-
23
- RestCore::Facebook.new(:access_token => token).fql(fql).should.eq []
24
- end
25
-
26
- should 'do fql.mutilquery correctly' do
27
- f0 = 'SELECT display_name FROM application WHERE app_id="233082465238"'
28
- f1 = 'SELECT display_name FROM application WHERE app_id="110225210740"'
29
- f0q, f1q = "\"#{f0.gsub('"', '\\"')}\"", "\"#{f1.gsub('"', '\\"')}\""
30
- q = "format=json&queries=#{CGI.escape("{\"f0\":#{f0q},\"f1\":#{f1q}}")}"
31
- p = "format=json&queries=#{CGI.escape("{\"f1\":#{f1q},\"f0\":#{f0q}}")}"
32
-
33
- stub_multi = lambda{
34
- stub_request(:get,
35
- "https://api.facebook.com/method/fql.multiquery?#{q}").
36
- to_return(:body => '[]')
37
-
38
- stub_request(:get,
39
- "https://api.facebook.com/method/fql.multiquery?#{p}").
40
- to_return(:body => '[]')
41
- }
42
-
43
- stub_multi.call
44
- RestCore::Facebook.new.fql_multi(:f0 => f0, :f1 => f1).should.eq []
45
- end
46
-
47
- should 'cache fake post in fql' do
48
- query = 'select name from user where uid = 4'
49
- body = '[{"name":"Mark Zuckerberg"}]'
50
- stub_request(:post,
51
- 'https://api.facebook.com/method/fql.query?format=json').
52
- with(:body => {:query => query}).
53
- to_return(:body => body)
54
-
55
- RestCore::Facebook.new(:cache => (cache = {})).
56
- fql(query, {}, :post => true).
57
- first['name'] .should.eq 'Mark Zuckerberg'
58
- cache.size .should.eq 1
59
- cache.values.first.should.eq body
60
-
61
- WebMock.reset! # should hit the cache
62
-
63
- RestCore::Facebook.new(:cache => cache).fql(query, {}, :post => true).
64
- first['name'] .should.eq 'Mark Zuckerberg'
65
- cache.size .should.eq 1
66
- cache.values.first.should.eq body
67
-
68
- # query changed
69
- should.raise(WebMock::NetConnectNotAllowedError) do
70
- RestCore::Facebook.new(:cache => cache).
71
- fql(query.upcase, {}, :post => true)
72
- end
73
-
74
- # cache should work for normal get
75
- RestCore::Facebook.new(:cache => cache).fql(query).
76
- first['name'] .should.eq 'Mark Zuckerberg'
77
- cache.size .should.eq 1
78
- cache.values.first.should.eq body
79
- end
80
-
81
- should 'do facebook old rest api' do
82
- body = 'hate facebook inconsistent'
83
- stub_request(:get,
84
- 'https://api.facebook.com/method/notes.create?format=json').
85
- to_return(:body => body)
86
-
87
- RestCore::Facebook.new.
88
- old_rest('notes.create', {}, :json_decode => false).should.eq body
89
- end
90
-
91
- should 'exchange sessions for access token' do
92
- stub_request(:post,
93
- 'https://graph.facebook.com/oauth/exchange_sessions?' \
94
- 'type=client_cred&client_id=id&client_secret=di&' \
95
- 'sessions=bad%20bed').
96
- to_return(:body => '[{"access_token":"bogus"}]')
97
-
98
- RestCore::Facebook.new(:app_id => 'id',
99
- :secret => 'di').
100
- exchange_sessions(:sessions => 'bad bed').
101
- first['access_token'].should.eq 'bogus'
102
- end
103
-
104
- should 'use an secret access_token' do
105
- stub_request(:get,
106
- 'https://api.facebook.com/method/admin.getAppProperties?' \
107
- 'access_token=123%7Cs&format=json&properties=app_id'
108
- ).to_return(:body => '{"app_id":"123"}')
109
-
110
- RestCore::Facebook.new(:app_id => '123', :secret => 's').
111
- secret_old_rest('admin.getAppProperties', :properties => 'app_id').
112
- should.eq({'app_id' => '123'})
113
- end
114
- end