cassette-rack 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cassette-rack/drawer.rb +1 -1
- data/lib/cassette-rack/request.rb +20 -4
- data/lib/cassette-rack/response.rb +42 -5
- data/lib/cassette-rack/response/raise_error.rb +77 -0
- data/lib/cassette-rack/version.rb +1 -1
- data/spec/{grant-front_spec.rb → cassette-rack_spec.rb} +0 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93203bf07ad2d2bf689158528378c15fcc5c4386
|
4
|
+
data.tar.gz: 72ba360c0eab36c64fb845346fbdb24e950a6a55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f339b4d8279553bc7067452b4218cdbbcaf288674dfd7eb5b842534f52e0f907a0e9e7aad3a6132b3d039ad352c96986015257cc30f26bd5e35db8dbaa8550d
|
7
|
+
data.tar.gz: c083878e301b10f8a76487c1987f8325b921058f1f555f5cf6c4d4d43df625f0217bf48cd2fcf97f7657dd5ce4648463ee228e30059ef1b7eec70e0b49d82c80
|
data/lib/cassette-rack/drawer.rb
CHANGED
@@ -3,6 +3,8 @@ require 'cassette-rack/response'
|
|
3
3
|
|
4
4
|
module CassetteRack
|
5
5
|
module Request
|
6
|
+
attr_reader :request_options
|
7
|
+
|
6
8
|
def get(path, params=nil, headers=nil)
|
7
9
|
request(:get, path, params, headers)
|
8
10
|
end
|
@@ -19,19 +21,25 @@ module CassetteRack
|
|
19
21
|
request(:put, path, nil, headers, body)
|
20
22
|
end
|
21
23
|
|
22
|
-
def delete(path)
|
23
|
-
request(:delete, path)
|
24
|
+
def delete(path, headers=nil)
|
25
|
+
request(:delete, path, nil, headers)
|
24
26
|
end
|
25
27
|
|
26
28
|
def request(method, path, params=nil, headers=nil, body=nil, options=nil)
|
27
|
-
|
29
|
+
if request_options
|
30
|
+
options = request_options
|
31
|
+
else
|
32
|
+
options = { url: CassetteRack.config.url, headers: headers }
|
33
|
+
end
|
34
|
+
|
35
|
+
conn = Faraday.new(options)
|
28
36
|
res = conn.send(method) do |req|
|
29
37
|
case method
|
30
38
|
when :get, :delete
|
31
39
|
req.url path
|
32
40
|
when :post, :patch, :put
|
33
41
|
req.path = path
|
34
|
-
req.body = body
|
42
|
+
req.body = parse_content(body, req)
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
@@ -41,5 +49,13 @@ module CassetteRack
|
|
41
49
|
def response
|
42
50
|
@response
|
43
51
|
end
|
52
|
+
|
53
|
+
def parse_content(body, req)
|
54
|
+
if req.headers['content-type'] == 'application/json' and body.class == Hash
|
55
|
+
body.to_json
|
56
|
+
else
|
57
|
+
body
|
58
|
+
end
|
59
|
+
end
|
44
60
|
end
|
45
61
|
end
|
@@ -1,11 +1,16 @@
|
|
1
|
+
#require 'json'
|
2
|
+
|
1
3
|
module CassetteRack
|
2
4
|
class Response
|
3
|
-
attr_reader :status, :headers, :body, :
|
5
|
+
attr_reader :status, :headers, :body, :content
|
6
|
+
#, :method
|
4
7
|
|
5
|
-
def initialize(
|
6
|
-
@
|
7
|
-
@
|
8
|
-
@
|
8
|
+
def initialize(res)
|
9
|
+
@response = res
|
10
|
+
@status = res.status
|
11
|
+
@headers = res.headers
|
12
|
+
@body = res.body
|
13
|
+
parse_content
|
9
14
|
end
|
10
15
|
|
11
16
|
def status_code
|
@@ -15,5 +20,37 @@ module CassetteRack
|
|
15
20
|
def response_headers
|
16
21
|
headers
|
17
22
|
end
|
23
|
+
|
24
|
+
def success?
|
25
|
+
@response.success?
|
26
|
+
end
|
27
|
+
|
28
|
+
def permit(*keys)
|
29
|
+
case content
|
30
|
+
when Hash
|
31
|
+
content.select { |key| keys.include? key }
|
32
|
+
when Array
|
33
|
+
content.map { |item| item.select { |key| keys.include? key } }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def parse_content
|
39
|
+
case body
|
40
|
+
when nil, '', ' '
|
41
|
+
@content = nil
|
42
|
+
when 'true'
|
43
|
+
@content = true
|
44
|
+
when 'false'
|
45
|
+
@content = false
|
46
|
+
else
|
47
|
+
if @response.env.request_headers['accept'] == 'application/json'
|
48
|
+
@content = JSON.parse(body)
|
49
|
+
else
|
50
|
+
@content = body
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
# end private
|
18
55
|
end
|
19
56
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module CassetteRack
|
4
|
+
class Response
|
5
|
+
class RaiseError < Faraday::Response::Middleware
|
6
|
+
def on_complete(env)
|
7
|
+
if error = CassetteRack::Response::Error.status(env)
|
8
|
+
raise error
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Error < StandardError
|
14
|
+
attr_reader :response
|
15
|
+
|
16
|
+
def self.status(env)
|
17
|
+
if klass =
|
18
|
+
case env[:status]
|
19
|
+
when 400 then Response::Error::BadRequest
|
20
|
+
when 401 then Response::Error::Unauthorized
|
21
|
+
when 403 then Response::Error::Forbidden
|
22
|
+
when 404 then Response::Error::NotFound
|
23
|
+
when 405 then Response::Error::MethodNotAllowed
|
24
|
+
when 406 then Response::Error::NotAcceptable
|
25
|
+
when 409 then Response::Error::Conflict
|
26
|
+
when 415 then Response::Error::UnsupportedMediaType
|
27
|
+
when 422 then Response::Error::UnprocessableEntity
|
28
|
+
when 400..499 then Response::Error::ClientError
|
29
|
+
when 500 then Response::Error::InternalServerError
|
30
|
+
when 501 then Response::Error::NotImplemented
|
31
|
+
when 502 then Response::Error::BadGateway
|
32
|
+
when 503 then Response::Error::ServiceUnavailable
|
33
|
+
when 500..599 then Response::Error::ServerError
|
34
|
+
end
|
35
|
+
klass.new(env)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(response)
|
40
|
+
@response = response
|
41
|
+
|
42
|
+
str = response[:body]
|
43
|
+
if response.request_headers['accept'] == 'application/json'
|
44
|
+
begin
|
45
|
+
body = JSON.parse(response[:body])
|
46
|
+
str = body['message'] if body.key?('message')
|
47
|
+
str = body['error_message'] if body.key?('error_message')
|
48
|
+
rescue
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
super(str)
|
53
|
+
end
|
54
|
+
|
55
|
+
def response_status
|
56
|
+
response[:status]
|
57
|
+
end
|
58
|
+
|
59
|
+
def response_body
|
60
|
+
response[:body]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Response::Error::BadRequest < CassetteRack::Response::Error; end
|
65
|
+
class Response::Error::Unauthorized < CassetteRack::Response::Error; end
|
66
|
+
class Response::Error::Forbidden < CassetteRack::Response::Error; end
|
67
|
+
class Response::Error::NotFound < CassetteRack::Response::Error; end
|
68
|
+
class Response::Error::MethodNotAllowed < CassetteRack::Response::Error; end
|
69
|
+
class Response::Error::NotAcceptable < CassetteRack::Response::Error; end
|
70
|
+
class Response::Error::Conflict < CassetteRack::Response::Error; end
|
71
|
+
class Response::Error::InternalServerError < CassetteRack::Response::Error; end
|
72
|
+
class Response::Error::NotImplemented < CassetteRack::Response::Error; end
|
73
|
+
class Response::Error::BadGateway < CassetteRack::Response::Error; end
|
74
|
+
class Response::Error::UnprocessableEntity < CassetteRack::Response::Error; end
|
75
|
+
class Response::Error::ServiceUnavailable < CassetteRack::Response::Error; end
|
76
|
+
end
|
77
|
+
end
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cassette-rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ogom
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -131,6 +131,7 @@ files:
|
|
131
131
|
- lib/cassette-rack/engine.rb
|
132
132
|
- lib/cassette-rack/request.rb
|
133
133
|
- lib/cassette-rack/response.rb
|
134
|
+
- lib/cassette-rack/response/raise_error.rb
|
134
135
|
- lib/cassette-rack/tree.rb
|
135
136
|
- lib/cassette-rack/tree/branch.rb
|
136
137
|
- lib/cassette-rack/tree/leaf.rb
|
@@ -138,7 +139,7 @@ files:
|
|
138
139
|
- lib/templates/layouts/application.html.liquid
|
139
140
|
- lib/templates/layouts/content.md.liquid
|
140
141
|
- lib/templates/layouts/preview.liquid
|
141
|
-
- spec/
|
142
|
+
- spec/cassette-rack_spec.rb
|
142
143
|
- spec/lib/version_spec.rb
|
143
144
|
- spec/spec_helper.rb
|
144
145
|
homepage: http://ogom.github.io/cassette-rack
|
@@ -166,7 +167,7 @@ signing_key:
|
|
166
167
|
specification_version: 4
|
167
168
|
summary: Operate of the VCR cassette
|
168
169
|
test_files:
|
169
|
-
- spec/
|
170
|
+
- spec/cassette-rack_spec.rb
|
170
171
|
- spec/lib/version_spec.rb
|
171
172
|
- spec/spec_helper.rb
|
172
173
|
has_rdoc:
|