rest-core 0.3.0.pre.0 → 0.3.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -6,6 +6,7 @@
6
6
  * [facebook] Facebook::RailsUtil is imported from [rest-graph][]
7
7
  * [facebook] Use json_decode instead of auto_decode as rest-graph
8
8
  * [twitter] Fixed opts in Twitter#tweet
9
+ * [twitter] Introduced Twitter::Error instead of RuntimeError!
9
10
  * [rest-core] We no longer explicitly depends on Rack
10
11
  * [rest-core] Added a shorthand RC to access RestCore
11
12
  * [cache] All clients no longer have default hash cache
@@ -11,16 +11,11 @@ RestCore::Twitter = RestCore::Builder.client(:data) do
11
11
  use s::Oauth1Header ,
12
12
  'oauth/request_token', 'oauth/access_token', 'oauth/authorize'
13
13
 
14
- use s::CommonLogger , method(:puts)
14
+ use s::CommonLogger , lambda{|obj|obj}
15
15
 
16
16
  use s::Cache , nil, 3600 do
17
17
  use s::ErrorHandler , lambda{ |env|
18
- if (body = env[s::RESPONSE_BODY]).kind_of?(Hash)
19
- raise body['error']
20
- else
21
- raise body
22
- end
23
- }
18
+ raise ::RestCore::Twitter::Error.call(env) }
24
19
  use s::ErrorDetectorHttp
25
20
  use s::JsonDecode , true
26
21
  run s::Ask
@@ -31,6 +26,45 @@ RestCore::Twitter = RestCore::Builder.client(:data) do
31
26
  run s::RestClient
32
27
  end
33
28
 
29
+ class RestCore::Twitter::Error < RuntimeError
30
+ include RestCore
31
+ class ServerError < Twitter::Error; end
32
+
33
+ class BadRequest < Twitter::Error; end
34
+ class Unauthorized < Twitter::Error; end
35
+ class Forbidden < Twitter::Error; end
36
+ class NotFound < Twitter::Error; end
37
+ class NotAcceptable < Twitter::Error; end
38
+ class EnhanceYourCalm < Twitter::Error; end
39
+
40
+ class InternalServerError < Twitter::Error::ServerError; end
41
+ class BadGateway < Twitter::Error::ServerError; end
42
+ class ServiceUnavailable < Twitter::Error::ServerError; end
43
+
44
+ attr_reader :error, :url
45
+ def initialize error, url=''
46
+ @error, @url = error, url
47
+ super("#{error.inspect} from #{url}")
48
+ end
49
+
50
+ def self.call env
51
+ error, url = env[RESPONSE_BODY], Middleware.request_uri(env)
52
+ return new(env[FAIL], url) unless error.kind_of?(Hash)
53
+ case env[RESPONSE_STATUS]
54
+ when 400; BadRequest
55
+ when 401; Unauthorized
56
+ when 403; Forbidden
57
+ when 404; NotFound
58
+ when 406; NotAcceptable
59
+ when 420; EnhanceYourCalm
60
+ when 500; InternalServerError
61
+ when 502; BadGateway
62
+ when 503; ServiceUnavailable
63
+ else ; self
64
+ end.new(error, url)
65
+ end
66
+ end
67
+
34
68
  module RestCore::Twitter::Client
35
69
  include RestCore
36
70
 
@@ -6,6 +6,6 @@ class RestCore::ErrorDetectorHttp < RestCore::ErrorDetector
6
6
  include RestCore::Middleware
7
7
 
8
8
  def initialize app
9
- super(app, lambda{ |env| (env[RESPONSE_STATUS] || 200) / 100 != 2 })
9
+ super(app, lambda{ |env| (env[RESPONSE_STATUS] || 200) / 100 >= 4 })
10
10
  end
11
11
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module RestCore
3
- VERSION = '0.3.0.pre.0'
3
+ VERSION = '0.3.0.pre.1'
4
4
  end
data/rest-core.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "rest-core"
5
- s.version = "0.3.0.pre.0"
5
+ s.version = "0.3.0.pre.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [
@@ -117,6 +117,7 @@ Gem::Specification.new do |s|
117
117
  "test/client/facebook/test_parse.rb",
118
118
  "test/client/facebook/test_serialize.rb",
119
119
  "test/client/facebook/test_timeout.rb",
120
+ "test/client/twitter/test_api.rb",
120
121
  "test/test_oauth1_header.rb"]
121
122
  s.homepage = "https://github.com/cardinalblue/rest-core"
122
123
  s.require_paths = ["lib"]
@@ -136,6 +137,7 @@ Gem::Specification.new do |s|
136
137
  "test/client/facebook/test_parse.rb",
137
138
  "test/client/facebook/test_serialize.rb",
138
139
  "test/client/facebook/test_timeout.rb",
140
+ "test/client/twitter/test_api.rb",
139
141
  "test/test_oauth1_header.rb"]
140
142
 
141
143
  if s.respond_to? :specification_version then
@@ -0,0 +1,37 @@
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.pre.0
4
+ version: 0.3.0.pre.1
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2011-09-02 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
17
- requirement: &2154406600 !ruby/object:Gem::Requirement
17
+ requirement: &2166688360 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2154406600
25
+ version_requirements: *2166688360
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: yajl-ruby
28
- requirement: &2154405920 !ruby/object:Gem::Requirement
28
+ requirement: &2166687880 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2154405920
36
+ version_requirements: *2166687880
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rack
39
- requirement: &2154399520 !ruby/object:Gem::Requirement
39
+ requirement: &2166687280 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2154399520
47
+ version_requirements: *2166687280
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: json
50
- requirement: &2154398840 !ruby/object:Gem::Requirement
50
+ requirement: &2166686760 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2154398840
58
+ version_requirements: *2166686760
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: json_pure
61
- requirement: &2154398100 !ruby/object:Gem::Requirement
61
+ requirement: &2166686180 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2154398100
69
+ version_requirements: *2166686180
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: ruby-hmac
72
- requirement: &2154397380 !ruby/object:Gem::Requirement
72
+ requirement: &2166685580 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2154397380
80
+ version_requirements: *2166685580
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: webmock
83
- requirement: &2154396860 !ruby/object:Gem::Requirement
83
+ requirement: &2166678200 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2154396860
91
+ version_requirements: *2166678200
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: bacon
94
- requirement: &2154396080 !ruby/object:Gem::Requirement
94
+ requirement: &2166677280 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *2154396080
102
+ version_requirements: *2166677280
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: rr
105
- requirement: &2154395380 !ruby/object:Gem::Requirement
105
+ requirement: &2166676540 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: '0'
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *2154395380
113
+ version_requirements: *2166676540
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: rake
116
- requirement: &2154394440 !ruby/object:Gem::Requirement
116
+ requirement: &2166675740 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ! '>='
@@ -121,7 +121,7 @@ dependencies:
121
121
  version: '0'
122
122
  type: :development
123
123
  prerelease: false
124
- version_requirements: *2154394440
124
+ version_requirements: *2166675740
125
125
  description: ! 'A modular Ruby REST client collection/infrastructure.
126
126
 
127
127
 
@@ -259,6 +259,7 @@ files:
259
259
  - test/client/facebook/test_parse.rb
260
260
  - test/client/facebook/test_serialize.rb
261
261
  - test/client/facebook/test_timeout.rb
262
+ - test/client/twitter/test_api.rb
262
263
  - test/test_oauth1_header.rb
263
264
  homepage: https://github.com/cardinalblue/rest-core
264
265
  licenses: []
@@ -298,4 +299,5 @@ test_files:
298
299
  - test/client/facebook/test_parse.rb
299
300
  - test/client/facebook/test_serialize.rb
300
301
  - test/client/facebook/test_timeout.rb
302
+ - test/client/twitter/test_api.rb
301
303
  - test/test_oauth1_header.rb