json_client 0.2.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -19
- data/Rakefile +2 -4
- data/json_client.gemspec +18 -17
- data/lib/json_client.rb +18 -6
- data/lib/json_client/base.rb +48 -47
- data/lib/json_client/base_serializer.rb +6 -3
- data/lib/json_client/create.rb +9 -0
- data/lib/json_client/destroy.rb +11 -0
- data/lib/json_client/dsl.rb +2 -2
- data/lib/json_client/dsl/on_use_collector.rb +3 -0
- data/lib/json_client/dsl/requests_collector.rb +0 -1
- data/lib/json_client/dsl/serializers_collector.rb +0 -1
- data/lib/json_client/empty_serializer.rb +2 -3
- data/lib/json_client/index.rb +9 -0
- data/lib/json_client/model_serializer.rb +4 -0
- data/lib/json_client/request.rb +48 -0
- data/lib/json_client/request_with_body.rb +30 -0
- data/lib/json_client/request_without_body.rb +17 -0
- data/lib/json_client/requests.rb +58 -16
- data/lib/json_client/response.rb +23 -0
- data/lib/json_client/show.rb +9 -0
- data/lib/json_client/update.rb +15 -0
- data/lib/json_client/uri_builder.rb +34 -0
- data/lib/json_client/version.rb +1 -1
- data/spec/fixtures/vcr_cassettes/all_success.yml +15 -15
- data/spec/fixtures/vcr_cassettes/create_success.yml +19 -20
- data/spec/fixtures/vcr_cassettes/destroy_success.yml +35 -36
- data/spec/fixtures/vcr_cassettes/show_success.yml +17 -16
- data/spec/fixtures/vcr_cassettes/update_success.yml +38 -40
- data/spec/json_client/base_responses/response_spec.rb +8 -4
- data/spec/json_client/base_spec.rb +32 -19
- data/spec/json_client/dsl_spec.rb +10 -12
- data/spec/json_client/model_serializer_spec.rb +14 -4
- data/spec/json_client/{pather_spec.rb → uri_builder_spec.rb} +26 -4
- data/spec/json_client_spec.rb +22 -8
- data/spec/spec_helper.rb +0 -1
- metadata +47 -46
- data/lib/json_client/base_requests/create.rb +0 -16
- data/lib/json_client/base_requests/destroy.rb +0 -13
- data/lib/json_client/base_requests/index.rb +0 -11
- data/lib/json_client/base_requests/request.rb +0 -12
- data/lib/json_client/base_requests/show.rb +0 -11
- data/lib/json_client/base_requests/update.rb +0 -16
- data/lib/json_client/base_responses/create.rb +0 -8
- data/lib/json_client/base_responses/destroy.rb +0 -8
- data/lib/json_client/base_responses/index.rb +0 -8
- data/lib/json_client/base_responses/response.rb +0 -25
- data/lib/json_client/base_responses/show.rb +0 -8
- data/lib/json_client/base_responses/update.rb +0 -8
- data/lib/json_client/pather.rb +0 -20
- data/lib/json_client/resource_handler.rb +0 -9
- data/lib/json_client/responses.rb +0 -29
- data/spec/fixtures/vcr_cassettes/authenticate_success.yml +0 -58
- data/spec/json_client/base_requests/request_spec.rb +0 -14
- data/spec/json_client/requests_spec.rb +0 -43
- data/spec/json_client/responses_spec.rb +0 -43
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'json_client/request'
|
2
|
+
|
3
|
+
module JsonClient
|
4
|
+
class RequestWithBody < Request
|
5
|
+
attr_reader :params
|
6
|
+
|
7
|
+
def md5
|
8
|
+
@md5 ||= ::JsonClient.md5_base64_digest(params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute!(request)
|
12
|
+
if methods.include? request.method
|
13
|
+
request.body = params
|
14
|
+
super(request)
|
15
|
+
else
|
16
|
+
fail "#{request.method} must be in #{methods}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def methods
|
23
|
+
%w(POST PUT)
|
24
|
+
end
|
25
|
+
|
26
|
+
def headers
|
27
|
+
super.merge('Content-MD5' => md5)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module JsonClient
|
2
|
+
class RequestWithoutBody < Request
|
3
|
+
def fetch(request)
|
4
|
+
if methods.include? request.method
|
5
|
+
execute!(request)
|
6
|
+
else
|
7
|
+
fail "#{request.method} must be in #{methods}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
def methods
|
14
|
+
%w(GET DELETE POST PUT)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/json_client/requests.rb
CHANGED
@@ -1,29 +1,71 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require_relative 'base_requests/create'
|
4
|
-
require_relative 'base_requests/update'
|
5
|
-
require_relative 'base_requests/destroy'
|
1
|
+
require 'json_client/request_with_body'
|
2
|
+
require 'json_client/request_without_body'
|
6
3
|
|
7
4
|
module JsonClient
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
module Requests
|
6
|
+
class Create < ::JsonClient::RequestWithBody
|
7
|
+
def fetch
|
8
|
+
execute!(request)
|
9
|
+
end
|
10
|
+
|
11
|
+
def request
|
12
|
+
client::Post.new(
|
13
|
+
uri,
|
14
|
+
headers
|
15
|
+
)
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
|
-
|
14
|
-
|
19
|
+
class Update < ::JsonClient::RequestWithBody
|
20
|
+
def fetch
|
21
|
+
execute!(request)
|
22
|
+
end
|
23
|
+
|
24
|
+
def request
|
25
|
+
client::Put.new(
|
26
|
+
uri,
|
27
|
+
headers
|
28
|
+
)
|
29
|
+
end
|
15
30
|
end
|
16
31
|
|
17
|
-
|
18
|
-
|
32
|
+
class Index < ::JsonClient::RequestWithoutBody
|
33
|
+
def fetch
|
34
|
+
execute!(request)
|
35
|
+
end
|
36
|
+
|
37
|
+
def request
|
38
|
+
client::Get.new(
|
39
|
+
uri,
|
40
|
+
headers
|
41
|
+
)
|
42
|
+
end
|
19
43
|
end
|
20
44
|
|
21
|
-
|
22
|
-
|
45
|
+
class Show < ::JsonClient::RequestWithoutBody
|
46
|
+
def fetch
|
47
|
+
execute!(request)
|
48
|
+
end
|
49
|
+
|
50
|
+
def request
|
51
|
+
client::Get.new(
|
52
|
+
uri,
|
53
|
+
headers
|
54
|
+
)
|
55
|
+
end
|
23
56
|
end
|
24
57
|
|
25
|
-
|
26
|
-
|
58
|
+
class Destroy < ::JsonClient::RequestWithoutBody
|
59
|
+
def fetch
|
60
|
+
execute!(request)
|
61
|
+
end
|
62
|
+
|
63
|
+
def request
|
64
|
+
client::Delete.new(
|
65
|
+
uri,
|
66
|
+
headers
|
67
|
+
)
|
68
|
+
end
|
27
69
|
end
|
28
70
|
end
|
29
71
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module JsonClient
|
2
|
+
class Response
|
3
|
+
attr_reader :raw_response
|
4
|
+
|
5
|
+
def initialize(raw_response)
|
6
|
+
@raw_response = raw_response
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(name, *args)
|
10
|
+
raw_response.public_send(name, *args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def json
|
14
|
+
parse_json
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def parse_json
|
20
|
+
@parse_json ||= JSON.parse(body)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module JsonClient
|
2
|
+
class UriBuilder
|
3
|
+
attr_reader :host, :ext, :name, :port, :ssl
|
4
|
+
|
5
|
+
def initialize(host, ext, name, port = '80', ssl = false)
|
6
|
+
@host = host
|
7
|
+
@ext = ext
|
8
|
+
@name = name
|
9
|
+
@port = port
|
10
|
+
@ssl = ssl
|
11
|
+
end
|
12
|
+
|
13
|
+
def path(id = nil)
|
14
|
+
base = "#{host}:#{port}"
|
15
|
+
if id.nil?
|
16
|
+
base + "/#{ext}/#{name}"
|
17
|
+
else
|
18
|
+
base + "/#{ext}/#{name}/#{id}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def uri(id = nil)
|
23
|
+
p = path(id)
|
24
|
+
|
25
|
+
if ssl
|
26
|
+
p = "https://#{p}"
|
27
|
+
else
|
28
|
+
p = "http://#{p}"
|
29
|
+
end
|
30
|
+
|
31
|
+
URI(p)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/json_client/version.rb
CHANGED
@@ -2,28 +2,30 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: get
|
5
|
-
uri: http://
|
5
|
+
uri: http://127.0.0.1:3000/api/v2/accounts
|
6
6
|
body:
|
7
7
|
encoding: US-ASCII
|
8
8
|
string: ''
|
9
9
|
headers:
|
10
|
+
Content-Type:
|
11
|
+
- application/json
|
10
12
|
Accept:
|
11
|
-
-
|
13
|
+
- application/json
|
12
14
|
Accept-Encoding:
|
13
|
-
- gzip,
|
15
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
16
|
User-Agent:
|
15
17
|
- Ruby
|
18
|
+
Host:
|
19
|
+
- 127.0.0.1:3000
|
20
|
+
Date:
|
21
|
+
- Tue, 11 Aug 2015 05:46:26 GMT
|
22
|
+
Authorization:
|
23
|
+
- APIAuth 1:if37PuD7O2xnNl2F79KfWRC1IsE=
|
16
24
|
response:
|
17
25
|
status:
|
18
26
|
code: 200
|
19
27
|
message: OK
|
20
28
|
headers:
|
21
|
-
Server:
|
22
|
-
- Cowboy
|
23
|
-
Date:
|
24
|
-
- Sun, 04 Jan 2015 20:36:27 GMT
|
25
|
-
Connection:
|
26
|
-
- keep-alive
|
27
29
|
X-Frame-Options:
|
28
30
|
- SAMEORIGIN
|
29
31
|
X-Xss-Protection:
|
@@ -33,20 +35,18 @@ http_interactions:
|
|
33
35
|
Content-Type:
|
34
36
|
- application/json; charset=utf-8
|
35
37
|
Etag:
|
36
|
-
-
|
38
|
+
- W/"d750f6d7ca4471b89f25e4d5c7fa8d78"
|
37
39
|
Cache-Control:
|
38
40
|
- max-age=0, private, must-revalidate
|
39
41
|
X-Request-Id:
|
40
|
-
-
|
42
|
+
- 9f23d55a-b913-460a-b680-18cc77295693
|
41
43
|
X-Runtime:
|
42
|
-
- '0.
|
44
|
+
- '0.397713'
|
43
45
|
Transfer-Encoding:
|
44
46
|
- chunked
|
45
|
-
Via:
|
46
|
-
- 1.1 vegur
|
47
47
|
body:
|
48
48
|
encoding: UTF-8
|
49
49
|
string: '{"accounts":[]}'
|
50
50
|
http_version:
|
51
|
-
recorded_at:
|
51
|
+
recorded_at: Tue, 11 Aug 2015 05:46:27 GMT
|
52
52
|
recorded_with: VCR 2.9.3
|
@@ -2,32 +2,32 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: post
|
5
|
-
uri: http://
|
5
|
+
uri: http://127.0.0.1:3000/api/v2/accounts
|
6
6
|
body:
|
7
7
|
encoding: UTF-8
|
8
|
-
string: '{"
|
8
|
+
string: '{"account":{"username":"new_username","password":"new_password"}}'
|
9
9
|
headers:
|
10
|
+
Content-Type:
|
11
|
+
- application/json
|
10
12
|
Accept:
|
11
13
|
- application/json
|
14
|
+
Content-Md5:
|
15
|
+
- fHr1ZStB7WO1pvI2sBlMug==
|
12
16
|
Accept-Encoding:
|
13
|
-
- gzip,
|
14
|
-
Content-Type:
|
15
|
-
- application/json
|
16
|
-
Content-Length:
|
17
|
-
- '154'
|
17
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
18
18
|
User-Agent:
|
19
19
|
- Ruby
|
20
|
+
Host:
|
21
|
+
- 127.0.0.1:3000
|
22
|
+
Date:
|
23
|
+
- Tue, 11 Aug 2015 05:52:21 GMT
|
24
|
+
Authorization:
|
25
|
+
- APIAuth 1:6s5PqLo32dc6TO9MqxQH8RzAX1Y=
|
20
26
|
response:
|
21
27
|
status:
|
22
28
|
code: 200
|
23
29
|
message: OK
|
24
30
|
headers:
|
25
|
-
Server:
|
26
|
-
- Cowboy
|
27
|
-
Date:
|
28
|
-
- Sun, 04 Jan 2015 20:36:27 GMT
|
29
|
-
Connection:
|
30
|
-
- keep-alive
|
31
31
|
X-Frame-Options:
|
32
32
|
- SAMEORIGIN
|
33
33
|
X-Xss-Protection:
|
@@ -37,22 +37,21 @@ http_interactions:
|
|
37
37
|
Content-Type:
|
38
38
|
- application/json; charset=utf-8
|
39
39
|
Etag:
|
40
|
-
-
|
40
|
+
- W/"8db1fcb20c4c44c6c0c7d4685444c86c"
|
41
41
|
Cache-Control:
|
42
42
|
- max-age=0, private, must-revalidate
|
43
43
|
Set-Cookie:
|
44
44
|
- request_method=POST; path=/
|
45
45
|
X-Request-Id:
|
46
|
-
-
|
46
|
+
- 7ddcde23-0da6-4da6-9344-66281ce62191
|
47
47
|
X-Runtime:
|
48
|
-
- '0.
|
48
|
+
- '0.549678'
|
49
49
|
Transfer-Encoding:
|
50
50
|
- chunked
|
51
|
-
Via:
|
52
|
-
- 1.1 vegur
|
53
51
|
body:
|
54
52
|
encoding: UTF-8
|
55
|
-
string: '{"id":
|
53
|
+
string: '{"id":1,"username":"new_username","created_at":"2015-08-11 05:52:22
|
54
|
+
+0000","updated_at":"2015-08-11 05:52:22 +0000"}'
|
56
55
|
http_version:
|
57
|
-
recorded_at:
|
56
|
+
recorded_at: Tue, 11 Aug 2015 05:52:22 GMT
|
58
57
|
recorded_with: VCR 2.9.3
|
@@ -2,32 +2,32 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: post
|
5
|
-
uri: http://
|
5
|
+
uri: http://127.0.0.1:3000/api/v2/accounts
|
6
6
|
body:
|
7
7
|
encoding: UTF-8
|
8
|
-
string: '{"
|
8
|
+
string: '{"account":{"username":"new_username_2","password":"new_password_2"}}'
|
9
9
|
headers:
|
10
|
+
Content-Type:
|
11
|
+
- application/json
|
10
12
|
Accept:
|
11
13
|
- application/json
|
14
|
+
Content-Md5:
|
15
|
+
- dfp9/6hUVEGb+qoSkIjHGQ==
|
12
16
|
Accept-Encoding:
|
13
|
-
- gzip,
|
14
|
-
Content-Type:
|
15
|
-
- application/json
|
16
|
-
Content-Length:
|
17
|
-
- '158'
|
17
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
18
18
|
User-Agent:
|
19
19
|
- Ruby
|
20
|
+
Host:
|
21
|
+
- 127.0.0.1:3000
|
22
|
+
Date:
|
23
|
+
- Tue, 11 Aug 2015 05:46:28 GMT
|
24
|
+
Authorization:
|
25
|
+
- APIAuth 1:jqoBak/4rQGzOkE68wZkFbnkX7s=
|
20
26
|
response:
|
21
27
|
status:
|
22
28
|
code: 200
|
23
29
|
message: OK
|
24
30
|
headers:
|
25
|
-
Server:
|
26
|
-
- Cowboy
|
27
|
-
Date:
|
28
|
-
- Sun, 04 Jan 2015 20:48:55 GMT
|
29
|
-
Connection:
|
30
|
-
- keep-alive
|
31
31
|
X-Frame-Options:
|
32
32
|
- SAMEORIGIN
|
33
33
|
X-Xss-Protection:
|
@@ -37,48 +37,49 @@ http_interactions:
|
|
37
37
|
Content-Type:
|
38
38
|
- application/json; charset=utf-8
|
39
39
|
Etag:
|
40
|
-
-
|
40
|
+
- W/"5cafc7592864a07d28e51c815e120ba4"
|
41
41
|
Cache-Control:
|
42
42
|
- max-age=0, private, must-revalidate
|
43
43
|
Set-Cookie:
|
44
44
|
- request_method=POST; path=/
|
45
45
|
X-Request-Id:
|
46
|
-
-
|
46
|
+
- b8b99031-607c-4b56-9eda-62fda108b1f6
|
47
47
|
X-Runtime:
|
48
|
-
- '0.
|
48
|
+
- '0.177914'
|
49
49
|
Transfer-Encoding:
|
50
50
|
- chunked
|
51
|
-
Via:
|
52
|
-
- 1.1 vegur
|
53
51
|
body:
|
54
52
|
encoding: UTF-8
|
55
|
-
string: '{"id":
|
53
|
+
string: '{"id":3,"created_at":"2015-08-11 05:46:28 +0000","updated_at":"2015-08-11
|
54
|
+
05:46:28 +0000"}'
|
56
55
|
http_version:
|
57
|
-
recorded_at:
|
56
|
+
recorded_at: Tue, 11 Aug 2015 05:46:28 GMT
|
58
57
|
- request:
|
59
58
|
method: delete
|
60
|
-
uri: http://
|
59
|
+
uri: http://127.0.0.1:3000/api/v2/accounts/3
|
61
60
|
body:
|
62
61
|
encoding: US-ASCII
|
63
62
|
string: ''
|
64
63
|
headers:
|
64
|
+
Content-Type:
|
65
|
+
- application/json
|
65
66
|
Accept:
|
66
|
-
-
|
67
|
+
- application/json
|
67
68
|
Accept-Encoding:
|
68
|
-
- gzip,
|
69
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
69
70
|
User-Agent:
|
70
71
|
- Ruby
|
72
|
+
Host:
|
73
|
+
- 127.0.0.1:3000
|
74
|
+
Date:
|
75
|
+
- Tue, 11 Aug 2015 05:46:28 GMT
|
76
|
+
Authorization:
|
77
|
+
- APIAuth 1:6zJzQcNflZ70uzfDeCHjJz/fEeA=
|
71
78
|
response:
|
72
79
|
status:
|
73
80
|
code: 200
|
74
81
|
message: OK
|
75
82
|
headers:
|
76
|
-
Server:
|
77
|
-
- Cowboy
|
78
|
-
Date:
|
79
|
-
- Sun, 04 Jan 2015 20:48:55 GMT
|
80
|
-
Connection:
|
81
|
-
- keep-alive
|
82
83
|
X-Frame-Options:
|
83
84
|
- SAMEORIGIN
|
84
85
|
X-Xss-Protection:
|
@@ -88,22 +89,20 @@ http_interactions:
|
|
88
89
|
Content-Type:
|
89
90
|
- application/json; charset=utf-8
|
90
91
|
Etag:
|
91
|
-
-
|
92
|
+
- W/"a7f153d21e6c7effbc234f3242315d32"
|
92
93
|
Cache-Control:
|
93
94
|
- max-age=0, private, must-revalidate
|
94
95
|
Set-Cookie:
|
95
96
|
- request_method=DELETE; path=/
|
96
97
|
X-Request-Id:
|
97
|
-
-
|
98
|
+
- 2a02029f-4bf1-4e84-b571-504423727fe5
|
98
99
|
X-Runtime:
|
99
|
-
- '0.
|
100
|
+
- '0.024727'
|
100
101
|
Transfer-Encoding:
|
101
102
|
- chunked
|
102
|
-
Via:
|
103
|
-
- 1.1 vegur
|
104
103
|
body:
|
105
104
|
encoding: UTF-8
|
106
|
-
string: '{"id":
|
105
|
+
string: '{"id":3}'
|
107
106
|
http_version:
|
108
|
-
recorded_at:
|
107
|
+
recorded_at: Tue, 11 Aug 2015 05:46:28 GMT
|
109
108
|
recorded_with: VCR 2.9.3
|