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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +20 -19
  3. data/Rakefile +2 -4
  4. data/json_client.gemspec +18 -17
  5. data/lib/json_client.rb +18 -6
  6. data/lib/json_client/base.rb +48 -47
  7. data/lib/json_client/base_serializer.rb +6 -3
  8. data/lib/json_client/create.rb +9 -0
  9. data/lib/json_client/destroy.rb +11 -0
  10. data/lib/json_client/dsl.rb +2 -2
  11. data/lib/json_client/dsl/on_use_collector.rb +3 -0
  12. data/lib/json_client/dsl/requests_collector.rb +0 -1
  13. data/lib/json_client/dsl/serializers_collector.rb +0 -1
  14. data/lib/json_client/empty_serializer.rb +2 -3
  15. data/lib/json_client/index.rb +9 -0
  16. data/lib/json_client/model_serializer.rb +4 -0
  17. data/lib/json_client/request.rb +48 -0
  18. data/lib/json_client/request_with_body.rb +30 -0
  19. data/lib/json_client/request_without_body.rb +17 -0
  20. data/lib/json_client/requests.rb +58 -16
  21. data/lib/json_client/response.rb +23 -0
  22. data/lib/json_client/show.rb +9 -0
  23. data/lib/json_client/update.rb +15 -0
  24. data/lib/json_client/uri_builder.rb +34 -0
  25. data/lib/json_client/version.rb +1 -1
  26. data/spec/fixtures/vcr_cassettes/all_success.yml +15 -15
  27. data/spec/fixtures/vcr_cassettes/create_success.yml +19 -20
  28. data/spec/fixtures/vcr_cassettes/destroy_success.yml +35 -36
  29. data/spec/fixtures/vcr_cassettes/show_success.yml +17 -16
  30. data/spec/fixtures/vcr_cassettes/update_success.yml +38 -40
  31. data/spec/json_client/base_responses/response_spec.rb +8 -4
  32. data/spec/json_client/base_spec.rb +32 -19
  33. data/spec/json_client/dsl_spec.rb +10 -12
  34. data/spec/json_client/model_serializer_spec.rb +14 -4
  35. data/spec/json_client/{pather_spec.rb → uri_builder_spec.rb} +26 -4
  36. data/spec/json_client_spec.rb +22 -8
  37. data/spec/spec_helper.rb +0 -1
  38. metadata +47 -46
  39. data/lib/json_client/base_requests/create.rb +0 -16
  40. data/lib/json_client/base_requests/destroy.rb +0 -13
  41. data/lib/json_client/base_requests/index.rb +0 -11
  42. data/lib/json_client/base_requests/request.rb +0 -12
  43. data/lib/json_client/base_requests/show.rb +0 -11
  44. data/lib/json_client/base_requests/update.rb +0 -16
  45. data/lib/json_client/base_responses/create.rb +0 -8
  46. data/lib/json_client/base_responses/destroy.rb +0 -8
  47. data/lib/json_client/base_responses/index.rb +0 -8
  48. data/lib/json_client/base_responses/response.rb +0 -25
  49. data/lib/json_client/base_responses/show.rb +0 -8
  50. data/lib/json_client/base_responses/update.rb +0 -8
  51. data/lib/json_client/pather.rb +0 -20
  52. data/lib/json_client/resource_handler.rb +0 -9
  53. data/lib/json_client/responses.rb +0 -29
  54. data/spec/fixtures/vcr_cassettes/authenticate_success.yml +0 -58
  55. data/spec/json_client/base_requests/request_spec.rb +0 -14
  56. data/spec/json_client/requests_spec.rb +0 -43
  57. 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
@@ -1,29 +1,71 @@
1
- require_relative 'base_requests/index'
2
- require_relative 'base_requests/show'
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
- class Requests
9
- def index
10
- BaseRequests::Index
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
- def show
14
- BaseRequests::Show
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
- def create
18
- BaseRequests::Create
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
- def update
22
- BaseRequests::Update
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
- def destroy
26
- BaseRequests::Destroy
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,9 @@
1
+ module JsonClient
2
+ module Show
3
+ class Request < ::JsonClient::Request
4
+ def fetch
5
+ client.get url, params: auth_params
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module JsonClient
2
+ module Update
3
+ class Request < ::JsonClient::RequestWithBody
4
+ def fetch
5
+ body = auth_params.merge(params).to_json
6
+
7
+ client.put(
8
+ url,
9
+ body,
10
+ 'content-md5' => md5(body)
11
+ )
12
+ end
13
+ end
14
+ end
15
+ 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
@@ -1,3 +1,3 @@
1
1
  module JsonClient
2
- VERSION = "0.2.2"
2
+ VERSION = '1.0.0'
3
3
  end
@@ -2,28 +2,30 @@
2
2
  http_interactions:
3
3
  - request:
4
4
  method: get
5
- uri: http://account-authenticator.herokuapp.com/api/v1/accounts?api_key=124ecd49-07fd-4553-a5cd-0178b7fa8b3f&api_password=IIoclO7kmQqJ1wixWrAuOA
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
- - "*/*; q=0.5, application/xml"
13
+ - application/json
12
14
  Accept-Encoding:
13
- - gzip, deflate
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
- - '"d750f6d7ca4471b89f25e4d5c7fa8d78"'
38
+ - W/"d750f6d7ca4471b89f25e4d5c7fa8d78"
37
39
  Cache-Control:
38
40
  - max-age=0, private, must-revalidate
39
41
  X-Request-Id:
40
- - 0c59d284-8ebb-4bbc-9077-a4485d9a5762
42
+ - 9f23d55a-b913-460a-b680-18cc77295693
41
43
  X-Runtime:
42
- - '0.102701'
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: Sun, 04 Jan 2015 20:36:28 GMT
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://account-authenticator.herokuapp.com/api/v1/accounts
5
+ uri: http://127.0.0.1:3000/api/v2/accounts
6
6
  body:
7
7
  encoding: UTF-8
8
- string: '{"api_key":"124ecd49-07fd-4553-a5cd-0178b7fa8b3f","api_password":"IIoclO7kmQqJ1wixWrAuOA","account":{"username":"new_username","password":"new_password"}}'
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, deflate
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
- - '"f26adbb79ceaf5aaa609ed1819b600b6"'
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
- - b554584e-ac2f-45ee-859e-d89f414c7aed
46
+ - 7ddcde23-0da6-4da6-9344-66281ce62191
47
47
  X-Runtime:
48
- - '0.201978'
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":6,"username":"new_username","created_at":"2015-01-04T20:36:28.339Z","updated_at":"2015-01-04T20:36:28.339Z"}'
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: Sun, 04 Jan 2015 20:36:28 GMT
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://account-authenticator.herokuapp.com/api/v1/accounts
5
+ uri: http://127.0.0.1:3000/api/v2/accounts
6
6
  body:
7
7
  encoding: UTF-8
8
- string: '{"api_key":"124ecd49-07fd-4553-a5cd-0178b7fa8b3f","api_password":"IIoclO7kmQqJ1wixWrAuOA","account":{"username":"new_username_2","password":"new_password_2"}}'
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, deflate
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
- - '"03258793b9ebad43ea73e9565d861f80"'
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
- - a764b58c-eda2-4770-9ef3-87fb2bf7854c
46
+ - b8b99031-607c-4b56-9eda-62fda108b1f6
47
47
  X-Runtime:
48
- - '0.201380'
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":10,"username":"new_username_2","created_at":"2015-01-04T20:48:55.695Z","updated_at":"2015-01-04T20:48:55.695Z"}'
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: Sun, 04 Jan 2015 20:48:55 GMT
56
+ recorded_at: Tue, 11 Aug 2015 05:46:28 GMT
58
57
  - request:
59
58
  method: delete
60
- uri: http://account-authenticator.herokuapp.com/api/v1/accounts/10?api_key=124ecd49-07fd-4553-a5cd-0178b7fa8b3f&api_password=IIoclO7kmQqJ1wixWrAuOA
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
- - "*/*; q=0.5, application/xml"
67
+ - application/json
67
68
  Accept-Encoding:
68
- - gzip, deflate
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
- - '"db2883d5f032d326e56b7f4d896e51e9"'
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
- - a4863ea3-30e9-48b1-91c9-628a1b10a858
98
+ - 2a02029f-4bf1-4e84-b571-504423727fe5
98
99
  X-Runtime:
99
- - '0.119341'
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":10}'
105
+ string: '{"id":3}'
107
106
  http_version:
108
- recorded_at: Sun, 04 Jan 2015 20:48:55 GMT
107
+ recorded_at: Tue, 11 Aug 2015 05:46:28 GMT
109
108
  recorded_with: VCR 2.9.3