rest-core 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/.travis.yml +6 -7
  2. data/CHANGES.md +137 -0
  3. data/Gemfile +1 -1
  4. data/README.md +183 -191
  5. data/TODO.md +5 -8
  6. data/example/multi.rb +31 -24
  7. data/example/simple.rb +28 -0
  8. data/example/use-cases.rb +194 -0
  9. data/lib/rest-core.rb +26 -19
  10. data/lib/rest-core/builder.rb +2 -2
  11. data/lib/rest-core/client.rb +40 -27
  12. data/lib/rest-core/client/universal.rb +16 -13
  13. data/lib/rest-core/client_oauth1.rb +5 -5
  14. data/lib/rest-core/engine/auto.rb +25 -0
  15. data/lib/rest-core/{app → engine}/dry.rb +1 -2
  16. data/lib/rest-core/engine/em-http-request.rb +39 -0
  17. data/lib/rest-core/engine/future/future.rb +106 -0
  18. data/lib/rest-core/engine/future/future_fiber.rb +39 -0
  19. data/lib/rest-core/engine/future/future_thread.rb +29 -0
  20. data/lib/rest-core/engine/rest-client.rb +56 -0
  21. data/lib/rest-core/middleware.rb +27 -5
  22. data/lib/rest-core/middleware/auth_basic.rb +5 -5
  23. data/lib/rest-core/middleware/bypass.rb +2 -2
  24. data/lib/rest-core/middleware/cache.rb +67 -54
  25. data/lib/rest-core/middleware/common_logger.rb +5 -8
  26. data/lib/rest-core/middleware/default_headers.rb +2 -2
  27. data/lib/rest-core/middleware/default_payload.rb +26 -2
  28. data/lib/rest-core/middleware/default_query.rb +4 -2
  29. data/lib/rest-core/middleware/default_site.rb +8 -6
  30. data/lib/rest-core/middleware/error_detector.rb +9 -16
  31. data/lib/rest-core/middleware/error_handler.rb +25 -11
  32. data/lib/rest-core/middleware/follow_redirect.rb +11 -14
  33. data/lib/rest-core/middleware/json_request.rb +19 -0
  34. data/lib/rest-core/middleware/json_response.rb +28 -0
  35. data/lib/rest-core/middleware/oauth1_header.rb +2 -7
  36. data/lib/rest-core/middleware/oauth2_header.rb +4 -7
  37. data/lib/rest-core/middleware/oauth2_query.rb +2 -2
  38. data/lib/rest-core/middleware/timeout.rb +21 -65
  39. data/lib/rest-core/middleware/timeout/{eventmachine_timer.rb → timer_em.rb} +3 -1
  40. data/lib/rest-core/middleware/timeout/timer_thread.rb +36 -0
  41. data/lib/rest-core/patch/multi_json.rb +8 -0
  42. data/lib/rest-core/test.rb +3 -12
  43. data/lib/rest-core/util/json.rb +65 -0
  44. data/lib/rest-core/util/parse_query.rb +2 -2
  45. data/lib/rest-core/version.rb +1 -1
  46. data/lib/rest-core/wrapper.rb +16 -16
  47. data/rest-core.gemspec +28 -27
  48. data/test/test_auth_basic.rb +14 -10
  49. data/test/test_builder.rb +7 -7
  50. data/test/test_cache.rb +126 -37
  51. data/test/test_client.rb +3 -1
  52. data/test/test_client_oauth1.rb +2 -3
  53. data/test/test_default_query.rb +17 -23
  54. data/test/test_em_http_request.rb +146 -0
  55. data/test/test_error_detector.rb +0 -1
  56. data/test/test_error_handler.rb +44 -0
  57. data/test/test_follow_redirect.rb +17 -19
  58. data/test/test_json_request.rb +28 -0
  59. data/test/test_json_response.rb +51 -0
  60. data/test/test_oauth1_header.rb +4 -4
  61. data/test/test_payload.rb +20 -12
  62. data/test/test_simple.rb +14 -0
  63. data/test/test_timeout.rb +11 -19
  64. data/test/test_universal.rb +5 -5
  65. data/test/test_wrapper.rb +19 -13
  66. metadata +28 -29
  67. data/doc/ToC.md +0 -7
  68. data/doc/dependency.md +0 -4
  69. data/doc/design.md +0 -4
  70. data/example/auto.rb +0 -51
  71. data/example/coolio.rb +0 -21
  72. data/example/eventmachine.rb +0 -30
  73. data/example/rest-client.rb +0 -16
  74. data/lib/rest-core/app/abstract/async_fiber.rb +0 -13
  75. data/lib/rest-core/app/auto.rb +0 -23
  76. data/lib/rest-core/app/coolio-async.rb +0 -32
  77. data/lib/rest-core/app/coolio-fiber.rb +0 -30
  78. data/lib/rest-core/app/coolio.rb +0 -9
  79. data/lib/rest-core/app/em-http-request-async.rb +0 -37
  80. data/lib/rest-core/app/em-http-request-fiber.rb +0 -45
  81. data/lib/rest-core/app/em-http-request.rb +0 -9
  82. data/lib/rest-core/app/rest-client.rb +0 -41
  83. data/lib/rest-core/middleware/json_decode.rb +0 -93
  84. data/lib/rest-core/middleware/timeout/coolio_timer.rb +0 -10
  85. data/pending/test_multi.rb +0 -123
  86. data/pending/test_test_util.rb +0 -86
  87. data/test/test_json_decode.rb +0 -24
@@ -1,10 +0,0 @@
1
-
2
- class RestCore::Timeout::CoolioTimer < ::Coolio::TimerWatcher
3
- attr_accessor :error
4
-
5
- alias_method :cancel, :detach
6
-
7
- def canceled?
8
- !attached?
9
- end
10
- end
@@ -1,123 +0,0 @@
1
-
2
- if respond_to?(:require_relative, true)
3
- require_relative 'common'
4
- else
5
- require File.dirname(__FILE__) + '/common'
6
- end
7
-
8
- describe 'RestGraph#multi' do
9
- after do
10
- WebMock.reset!
11
- RR.verify
12
- end
13
-
14
- should 'do multi query with em-http-request' do
15
- url = 'https://graph.facebook.com/me'
16
- stub_request(:get, url).to_return(:body => '{"data":"get"}')
17
- stub_request(:put, url).to_return(:body => '{"data":"put"}')
18
- rg = RestGraph.new
19
- mock.proxy(rg).request_em(anything, anything)
20
- EM.run{
21
- rg.multi([[:get, 'me'], [:put, 'me']]){ |results|
22
- results.should == [{'data' => 'get'}, {'data' => 'put'}]
23
- EM.stop
24
- }
25
- }
26
- end
27
-
28
- should 'call aget, aput family with multi' do
29
- url = 'https://graph.facebook.com/me'
30
- %w[aget adelete apost aput].each{ |meth|
31
- stub_request("#{meth[1..-1]}".to_sym, url).
32
- to_return(:body => "{\"data\":\"#{meth}\"}")
33
- rg = RestGraph.new
34
- mock.proxy(rg).request_em(anything, anything)
35
- EM.run{
36
- rg.send(meth, 'me', {}){ |result|
37
- result.should == {'data' => meth.to_s}
38
- EM.stop
39
- }
40
- }
41
- }
42
- end
43
-
44
- should 'for_pages' do
45
- rg = RestGraph.new
46
-
47
- args = [is_a(Hash), is_a(Array)]
48
- mock.proxy(rg).request_em(*args) # at least one time
49
- stub.proxy(rg).request_em(*args)
50
-
51
- %w[next previous].each{ |type|
52
- kind = "#{type}_page"
53
- data = {'paging' => {type => 'http://z'}, 'data' => ['z']}
54
-
55
- # invalid pages or just the page itself
56
- # not really need network
57
- nils = 0
58
- ranges = -1..1
59
- ranges.each{ |page|
60
- rg.for_pages(data, page, {:async => true}, kind){ |r|
61
- if r
62
- r.should == data
63
- else
64
- nils += 1
65
- end
66
- }.should == data
67
- }
68
- nils.should == ranges.to_a.size
69
-
70
- (2..4).each{ |pages|
71
- # merge data
72
- stub_request(:get, 'z').to_return(:body => '{"data":["y"]}')
73
- expects = [{'data' => %w[y]}, nil]
74
-
75
- EM.run{
76
- rg.for_pages(data, pages, {:async => true}, kind){ |r|
77
- r.should == expects.shift
78
- EM.stop if expects.empty?
79
- }
80
- }
81
-
82
- # this data cannot be merged
83
- stub_request(:get, 'z').to_return(:body => '{"data":"y"}')
84
- expects = [{'data' => 'y'}, nil]
85
-
86
- EM.run{
87
- rg.for_pages(data, pages, {:async => true}, kind){ |r|
88
- r.should == expects.shift
89
- EM.stop if expects.empty?
90
- }
91
- }
92
- }
93
-
94
- stub_request(:get, 'z').to_return(:body =>
95
- '{"paging":{"'+type+'":"http://yyy"},"data":["y"]}')
96
- stub_request(:get, 'yyy').to_return(:body => '{"data":["x"]}')
97
- expects = [{'data' => %w[y]}, {'data' => %w[x]}, nil]
98
-
99
- EM.run{
100
- rg.for_pages(data, 3, {:async => true}, kind){ |rr|
101
- rr.frozen?.should == true unless rr.nil? && RUBY_VERSION < '1.9.2'
102
- if rr
103
- r = rr.dup
104
- r.delete('paging')
105
- else
106
- r = rr
107
- end
108
- r.should == expects.shift
109
- EM.stop if expects.empty?
110
- }
111
- }
112
- }
113
- end
114
-
115
- # should 'cache in multi' do
116
- # end
117
- #
118
- # should 'logging' do
119
- # end
120
- #
121
- # should 'error handler?' do
122
- # end
123
- end
@@ -1,86 +0,0 @@
1
-
2
- if respond_to?(:require_relative, true)
3
- require_relative 'common'
4
- else
5
- require File.dirname(__FILE__) + '/common'
6
- end
7
-
8
- require 'rest-graph/test_util'
9
-
10
- describe RestGraph::TestUtil do
11
- before do
12
- RestGraph::TestUtil.setup
13
- end
14
-
15
- after do
16
- RestGraph::TestUtil.teardown
17
- end
18
-
19
- should 'stub requests and store result and teardown do cleanup' do
20
- RestGraph.new.get('me') .should == {'data' => []}
21
- RestGraph::TestUtil.history .should ==
22
- [[:get, "https://graph.facebook.com/me", nil]]
23
-
24
- RestGraph::TestUtil.teardown
25
-
26
- RestGraph::TestUtil.history.should == []
27
- begin
28
- RestGraph.new.get('me')
29
- rescue => e
30
- e.should.kind_of?(WebMock::NetConnectNotAllowedError)
31
- end
32
- end
33
-
34
- should 'have default response' do
35
- default = {'meta' => []}
36
- RestGraph::TestUtil.default_response = default
37
- RestGraph.new.get('me') .should == default
38
- end
39
-
40
- should 'have default data' do
41
- rg = RestGraph.new
42
- rg.data['uid'] .should == '1234'
43
- RestGraph::TestUtil.default_data = {'uid' => '4321'}
44
- rg.data['uid'] .should == '4321'
45
- RestGraph.new.data['uid'].should == '4321'
46
- end
47
-
48
- should 'be easy to stub data' do
49
- response = {'data' => 'me'}
50
- RestGraph::TestUtil.get('me'){ response }
51
- RestGraph.new.get('me').should == response
52
- RestGraph.new.get('he').should == RestGraph::TestUtil.default_response
53
- end
54
-
55
- should 'emulate login' do
56
- RestGraph::TestUtil.login(1829)
57
- rg = RestGraph.new
58
- rg.data['uid'].should == '1829'
59
- rg.authorized?.should == true
60
- rg.get('me').should == RestGraph::TestUtil.user('1829')
61
- end
62
-
63
- should 'reset before login' do
64
- RestGraph::TestUtil.login(1234).login(1829)
65
- rg = RestGraph.new
66
- rg.data['uid'].should == '1829'
67
- rg.authorized?.should == true
68
- rg.get('me').should == RestGraph::TestUtil.user('1829')
69
- RestGraph::TestUtil.login(1234)
70
- rg.data['uid'].should == '1234'
71
- rg.authorized?.should == true
72
- rg.get('me').should == RestGraph::TestUtil.user('1234')
73
- end
74
-
75
- should 'return correct response in fake get' do
76
- RestGraph.new.fql('', {}, :post => true).
77
- should == [RestGraph::TestUtil.default_response]
78
-
79
- RestGraph.new.fql_multi({:a => '', :b => ''}, {}, :post => true).
80
- sort_by{ |h| h['name'] }.
81
- should == [{'name' => 'a',
82
- 'fql_result_set' => [RestGraph::TestUtil.default_response]},
83
- {'name' => 'b',
84
- 'fql_result_set' => [RestGraph::TestUtil.default_response]}]
85
- end
86
- end
@@ -1,24 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RC::JsonDecode do
5
- before do
6
- @app = RC::JsonDecode.new(RC::Dry.new, true)
7
- end
8
-
9
- should 'do nothing' do
10
- @app.call({}).should.eq(RC::RESPONSE_BODY => nil)
11
- end
12
-
13
- should 'decode sync' do
14
- @app.call(RC::RESPONSE_BODY => '{}').should.eq(
15
- RC::RESPONSE_BODY => {} )
16
- end
17
-
18
- should 'decode async' do
19
- @app.call(RC::RESPONSE_BODY => '{}',
20
- RC::ASYNC => lambda{ |response|
21
- response[RC::RESPONSE_BODY].should.eq({})}
22
- )[RC::RESPONSE_BODY].should.eq '{}'
23
- end
24
- end