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,106 +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 'get the next/prev page' do
11
- rg = RestCore::Facebook.new(:site => '', :cache => false)
12
- %w[next previous].each{ |type|
13
- kind = "#{type}_page"
14
- rg.send(kind, {}) .should.eq nil
15
- rg.send(kind, {'paging' => []}).should.eq nil
16
- rg.send(kind, {'paging' => {}}).should.eq nil
17
-
18
- stub_request(:get, 'zzz').to_return(:body => '["ok"]')
19
- rg.send(kind, {'paging' => {type => 'zzz'}}).should.eq ['ok']
20
- }
21
- end
22
-
23
- should 'merge all pages into one' do
24
- rg = RestCore::Facebook.new(:site => '', :cache => false)
25
- %w[next previous].each{ |type|
26
- kind = "#{type}_page"
27
- data = {'paging' => {type => 'zzz'}, 'data' => ['z']}
28
-
29
- # invalid pages or just the page itself
30
- (-1..1).each{ |page|
31
- rg.for_pages(data, page, {}, kind).should == data
32
- }
33
-
34
- (2..4).each{ |pages|
35
- # merge data
36
- stub_request(:get, 'zzz').to_return(:body => '{"data":["y"]}')
37
- rg.for_pages(data, pages, {}, kind).should.eq({'data' => %w[z y]})
38
-
39
- # this data cannot be merged
40
- stub_request(:get, 'zzz').to_return(:body => '{"data":"y"}')
41
- rg.for_pages(data, pages, {}, kind).should.eq({'data' => %w[z]})
42
- }
43
-
44
- stub_request(:get, 'zzz').to_return(:body =>
45
- '{"paging":{"'+type+'":"yyy"},"data":["y"]}')
46
- stub_request(:get, 'yyy').to_return(:body => '{"data":["x"]}')
47
-
48
- rg.for_pages(data, 3, {}, kind).should.eq({'data' => %w[z y x]})
49
- }
50
- end
51
-
52
- should 'for_pages with callback' do
53
- rg = RestCore::Facebook.new(:site => '', :cache => false)
54
- %w[next previous].each{ |type|
55
- kind = "#{type}_page"
56
- data = {'paging' => {type => 'zzz'}, 'data' => ['z']}
57
-
58
- # invalid pages or just the page itself
59
- nils = 0
60
- ranges = -1..1
61
- ranges.each{ |page|
62
- rg.for_pages(data, page, {}, kind){ |r|
63
- if r
64
- r.should.eq data
65
- else
66
- nils += 1
67
- end
68
- }.should.eq data
69
- }
70
- nils.should.eq ranges.to_a.size
71
-
72
- (2..4).each{ |pages|
73
- # merge data
74
- stub_request(:get, 'zzz').to_return(:body => '{"data":["y"]}')
75
- expects = [{'data' => %w[y]}, nil]
76
- rg.for_pages(data, pages, {}, kind){ |r|
77
- r.should.eq expects.shift
78
- }.should.eq({'data' => %w[z y]})
79
- expects.empty?.should.eq true
80
-
81
- # this data cannot be merged
82
- stub_request(:get, 'zzz').to_return(:body => '{"data":"y"}')
83
- expects = [{'data' => 'y'}, nil]
84
- rg.for_pages(data, pages, {}, kind){ |r|
85
- r.should.eq expects.shift
86
- }.should.eq({'data' => %w[z]})
87
- expects.empty?.should.eq true
88
- }
89
-
90
- stub_request(:get, 'zzz').to_return(:body =>
91
- '{"paging":{"'+type+'":"yyy"},"data":["y"]}')
92
- stub_request(:get, 'yyy').to_return(:body => '{"data":["x"]}')
93
-
94
- expects = [{'data' => %w[y]}, {'data' => %w[x]}, nil]
95
- rg.for_pages(data, 3, {}, kind){ |rr|
96
- if rr
97
- r = rr.dup
98
- r.delete('paging')
99
- else
100
- r = rr
101
- end
102
- r.should.eq expects.shift
103
- }.should.eq({'data' => %w[z y x]})
104
- }
105
- end
106
- end
@@ -1,128 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Facebook do
5
-
6
- should 'return nil if parse error, but not when call data directly' do
7
- rg = RestCore::Facebook.new
8
- rg.parse_cookies!({}).should.eq nil
9
- rg.data .should.eq({})
10
- end
11
-
12
- should 'parse if fbs contains json as well' do
13
- algorithm = 'HMAC-SHA256'
14
- user = '{"country"=>"us", "age"=>{"min"=>21}}'
15
- data = {'algorithm' => algorithm, 'user' => user}
16
- rg = RestCore::Facebook.new(:data => data, :secret => 'secret')
17
- sig = rg.send(:calculate_sig, data)
18
- rg.parse_fbs!("\"#{rg.fbs}\"").should.eq data.merge('sig' => sig)
19
- end
20
-
21
- should 'extract correct access_token or fail checking sig' do
22
- access_token = '1|2-5|f.'
23
- app_id = '1829'
24
- secret = app_id.reverse
25
- sig = '398262caea8442bd8801e8fba7c55c8a'
26
- fbs = "access_token=#{CGI.escape(access_token)}&expires=0&" \
27
- "secret=abc&session_key=def-456&sig=#{sig}&uid=3"
28
-
29
- check = lambda{ |token, fbs1|
30
- http_cookie =
31
- "__utma=123; __utmz=456.utmcsr=(d)|utmccn=(d)|utmcmd=(n); " \
32
- "fbs_#{app_id}=#{fbs1}"
33
-
34
- rg = RestCore::Facebook.new(:app_id => app_id, :secret => secret)
35
- rg.parse_rack_env!('HTTP_COOKIE' => http_cookie).
36
- should.kind_of?(token ? Hash : NilClass)
37
- rg.access_token.should.eq token
38
-
39
- rg.parse_rack_env!('HTTP_COOKIE' => nil).should.eq nil
40
- rg.data.should.eq({})
41
-
42
- rg.parse_cookies!({"fbs_#{app_id}" => fbs1}).
43
- should.kind_of?(token ? Hash : NilClass)
44
- rg.access_token.should.eq token
45
-
46
- rg.parse_fbs!(fbs1).
47
- should.kind_of?(token ? Hash : NilClass)
48
- rg.access_token.should.eq token
49
- }
50
- check.call(access_token, fbs)
51
- check.call(access_token, "\"#{fbs}\"")
52
- fbs << '&inject=evil"'
53
- check.call(nil, fbs)
54
- check.call(nil, "\"#{fbs}\"")
55
- end
56
-
57
- should 'not pass if there is no secret, prevent from forgery' do
58
- rg = RestCore::Facebook.new
59
- rg.parse_fbs!('"feed=me&sig=bddd192cf27f22c05f61c8bea24fa4b7"').
60
- should.eq nil
61
- end
62
-
63
- should 'parse json correctly' do
64
- rg = RestCore::Facebook.new
65
-
66
- rg.parse_json!('bad json') .should.eq nil
67
- rg.parse_json!('{"no":"sig"}').should.eq nil
68
- rg.parse_json!('{"feed":"me","sig":"bddd192cf27f22c05f61c8bea24fa4b7"}').
69
- should.eq nil
70
-
71
- rg = RestCore::Facebook.new(:secret => 'bread')
72
- rg.parse_json!('{"feed":"me","sig":"20393e7823730308938a86ecf1c88b14"}').
73
- should.eq({'feed' => 'me', 'sig' => "20393e7823730308938a86ecf1c88b14"})
74
- rg.data.empty?.should.eq false
75
- rg.parse_json!('bad json')
76
- rg.data.empty?.should.eq true
77
- end
78
-
79
- should 'parse signed_request' do
80
- secret = 'aloha'
81
- json = RestCore::JsonDecode.json_encode('ooh' => 'dir', 'moo' => 'bar')
82
- encode = lambda{ |str|
83
- [str].pack('m').tr("\n=", '').tr('+/', '-_')
84
- }
85
- json_encoded = encode[json]
86
- sig = OpenSSL::HMAC.digest('sha256', secret, json_encoded)
87
- signed_request = "#{encode[sig]}.#{json_encoded}"
88
-
89
- rg = RestCore::Facebook.new(:secret => secret)
90
- rg.parse_signed_request!(signed_request)
91
- rg.data['ooh'].should.eq 'dir'
92
- rg.data['moo'].should.eq 'bar'
93
-
94
- signed_request = "#{encode[sig[0..-4]+'bad']}.#{json_encoded}"
95
- rg.parse_signed_request!(signed_request).should.eq nil
96
- rg.data .should.eq({})
97
- end
98
-
99
- should 'fallback to ruby-hmac if Digest.new raise an runtime error' do
100
- key, data = 'top', 'secret'
101
- digest = OpenSSL::HMAC.digest('sha256', key, data)
102
- mock(OpenSSL::HMAC).digest('sha256', key, data){ raise 'boom' }
103
- RestCore::Hmac.sha256(key, data).should.eq digest
104
- end
105
-
106
- should 'generate correct fbs with correct sig' do
107
- RestCore::Facebook.new(:access_token => 'fake', :secret => 's').fbs.
108
- should.eq \
109
- "access_token=fake&sig=#{Digest::MD5.hexdigest('access_token=fakes')}"
110
- end
111
-
112
- should 'parse fbs from facebook response which lacks sig...' do
113
- rg = RestCore::Facebook.new(:access_token => 'a', :secret => 'z')
114
- rg.parse_fbs!(rg.fbs) .should.kind_of?(Hash)
115
- rg.data.empty?.should.eq false
116
- rg.parse_fbs!(rg.fbs.sub(/sig\=\w+/, 'sig=abc')).should.eq nil
117
- rg.data.empty?.should.eq true
118
- end
119
-
120
- should 'generate correct fbs with additional parameters' do
121
- rg = RestCore::Facebook.new(:access_token => 'a', :secret => 'z')
122
- rg.data['expires'] = '1234'
123
- rg.parse_fbs!(rg.fbs).should.kind_of?(Hash)
124
- rg.access_token .should.eq 'a'
125
- rg.data['expires'] .should.eq '1234'
126
- end
127
-
128
- end
@@ -1,43 +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 'be serialized with lighten' do
11
- engines = begin
12
- require 'psych'
13
- YAML::ENGINE.yamler = 'psych' # TODO: probably a bug?
14
- [Psych, YAML, Marshal]
15
- rescue LoadError
16
- [YAML, Marshal]
17
- end
18
-
19
- # sorry, it is marshal in 1.8 is broken
20
- if defined?(RUBY_ENGINE)
21
- if RUBY_ENGINE == 'ruby' && RUBY_VERSION == '1.8.7'
22
- engines.pop # REE 1.8.7
23
- end
24
- else
25
- engines.pop # MRI 1.8.7
26
- end
27
-
28
- engines.each{ |engine|
29
- test = lambda{ |obj| engine.load(engine.dump(obj)) }
30
- rg = RestCore::Facebook.new(:log_handler => lambda{})
31
- lambda{ test[rg] }.should.raise(TypeError)
32
- test[rg.lighten].should.eq rg.lighten
33
- lambda{ test[rg] }.should.raise(TypeError)
34
- rg.lighten!
35
- test[rg.lighten].should.eq rg
36
- }
37
- end
38
-
39
- should 'lighten takes options to change attributes' do
40
- RestCore::Facebook.new.lighten(:timeout => 100 ).timeout.should.eq 100
41
- RestCore::Facebook.new.lighten(:lang => 'zh-TW').lang.should.eq 'zh-TW'
42
- end
43
- end
@@ -1,22 +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 'respect timeout' do
11
- stub_request(:get, 'https://graph.facebook.com/me').
12
- to_return(:body => '{}')
13
- mock.proxy(Timeout).timeout(numeric)
14
- RestCore::Facebook.new.get('me').should.eq({})
15
- end
16
-
17
- should 'override timeout' do
18
- mock(Timeout).timeout(99){ {RestCore::Facebook::RESPONSE_BODY => true} }
19
- RestCore::Facebook.new(:timeout => 1).get('me', {}, :timeout => 99).
20
- should.eq true
21
- end
22
- end
@@ -1,83 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- require 'time'
5
-
6
- describe RestCore::Flurry do
7
- after do
8
- WebMock.reset!
9
- RR.verify
10
- end
11
-
12
- before do
13
- startDate = '2011-08-23'
14
- endDate = '2011-09-19'
15
- @flurry = RestCore::Flurry.new
16
- stub(Time).now{ Time.parse(endDate, nil) }
17
- stub_request(:get,
18
- "http://api.flurry.com/appMetrics/ActiveUsers?" \
19
- "startDate=#{startDate}&endDate=#{endDate}").
20
- to_return(:body =>
21
- '{"@startDate":"2011-08-23",
22
- "@metric":"ActiveUsersByDay",
23
- "@endDate":"2011-09-19",
24
- "@version":"1.0",
25
- "@generatedDate":"9/19/11 5:54 AM",
26
- "day":
27
- [{"@value":"34722","@date":"2011-08-23"},
28
- {"@value":"33560","@date":"2011-08-24"},
29
- {"@value":"34392","@date":"2011-08-25"},
30
- {"@value":"37737","@date":"2011-08-26"},
31
- {"@value":"41332","@date":"2011-08-27"},
32
- {"@value":"40456","@date":"2011-08-28"},
33
- {"@value":"34950","@date":"2011-08-29"},
34
- {"@value":"34076","@date":"2011-08-30"},
35
- {"@value":"32096","@date":"2011-08-31"},
36
- {"@value":"31558","@date":"2011-09-01"},
37
- {"@value":"33467","@date":"2011-09-02"},
38
- {"@value":"39306","@date":"2011-09-03"},
39
- {"@value":"40233","@date":"2011-09-04"},
40
- {"@value":"37777","@date":"2011-09-05"},
41
- {"@value":"35829","@date":"2011-09-06"},
42
- {"@value":"35960","@date":"2011-09-07"},
43
- {"@value":"34871","@date":"2011-09-08"},
44
- {"@value":"36683","@date":"2011-09-09"},
45
- {"@value":"44077","@date":"2011-09-10"},
46
- {"@value":"45057","@date":"2011-09-11"},
47
- {"@value":"37750","@date":"2011-09-12"},
48
- {"@value":"35223","@date":"2011-09-13"},
49
- {"@value":"34918","@date":"2011-09-14"},
50
- {"@value":"37587","@date":"2011-09-15"},
51
- {"@value":"40972","@date":"2011-09-16"},
52
- {"@value":"45412","@date":"2011-09-17"},
53
- {"@value":"43920","@date":"2011-09-18"},
54
- {"@value":"6516","@date":"2011-09-19"}]}')
55
-
56
- @active_users =
57
- [["2011-09-19", 6516], ["2011-09-18", 43920], ["2011-09-17", 45412],
58
- ["2011-09-16", 40972], ["2011-09-15", 37587], ["2011-09-14", 34918],
59
- ["2011-09-13", 35223], ["2011-09-12", 37750], ["2011-09-11", 45057],
60
- ["2011-09-10", 44077], ["2011-09-09", 36683], ["2011-09-08", 34871],
61
- ["2011-09-07", 35960], ["2011-09-06", 35829], ["2011-09-05", 37777],
62
- ["2011-09-04", 40233], ["2011-09-03", 39306], ["2011-09-02", 33467],
63
- ["2011-09-01", 31558], ["2011-08-31", 32096], ["2011-08-30", 34076],
64
- ["2011-08-29", 34950], ["2011-08-28", 40456], ["2011-08-27", 41332],
65
- ["2011-08-26", 37737], ["2011-08-25", 34392], ["2011-08-24", 33560],
66
- ["2011-08-23", 34722]]
67
-
68
- @weekly = [244548, 270227, 248513, 257149]
69
- end
70
-
71
- should 'metrics("ActiveUsers")' do
72
- @flurry.metrics('ActiveUsers', {}, :weeks => 4).should.eq @active_users
73
-
74
- end
75
-
76
- should 'weekly(metrics("ActiveUsers"))' do
77
- @flurry.weekly(@active_users).should.eq @weekly
78
- end
79
-
80
- should 'sum(weekly(metrics("ActiveUsers")))' do
81
- @flurry.sum(@weekly).should.eq [1020437, 775889, 505662, 257149]
82
- end
83
- end
@@ -1,37 +0,0 @@
1
-
2
- require 'rest-core/test'
3
-
4
- describe RestCore::Twitter do
5
- after do
6
- WebMock.reset!
7
- RR.verify
8
- end
9
-
10
- should 'get right' do
11
- stub_request(:get, 'https://api.twitter.com/me').
12
- to_return(:body => '{"status": "OK"}')
13
-
14
- RestCore::Twitter.new.get('me').should.eq({'status' => 'OK'})
15
- end
16
-
17
- def check status, klass
18
- stub_request(:delete, 'https://api.twitter.com/123').to_return(
19
- :body => '{}', :status => status)
20
-
21
- lambda{
22
- RestCore::Twitter.new.delete('123')
23
- }.should.raise(klass)
24
-
25
- WebMock.reset!
26
- end
27
-
28
- should 'raise exception when encountering error' do
29
- [401, 402, 403].each{ |status|
30
- check(status, RestCore::Twitter::Error)
31
- }
32
-
33
- [500, 502, 503].each{ |status|
34
- check(status, RestCore::Twitter::Error::ServerError)
35
- }
36
- end
37
- end