json_client 0.2.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -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/6?api_key=124ecd49-07fd-4553-a5cd-0178b7fa8b3f&api_password=IIoclO7kmQqJ1wixWrAuOA
5
+ uri: http://127.0.0.1:3000/api/v2/accounts/2
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:51:01 GMT
22
+ Authorization:
23
+ - APIAuth 1:8BuNvv4r8jvbtseLR4b8OQARP0s=
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:37:35 GMT
25
- Connection:
26
- - keep-alive
27
29
  X-Frame-Options:
28
30
  - SAMEORIGIN
29
31
  X-Xss-Protection:
@@ -33,20 +35,19 @@ http_interactions:
33
35
  Content-Type:
34
36
  - application/json; charset=utf-8
35
37
  Etag:
36
- - '"f26adbb79ceaf5aaa609ed1819b600b6"'
38
+ - W/"bc1fb32f480f3719269da3d3da13d331"
37
39
  Cache-Control:
38
40
  - max-age=0, private, must-revalidate
39
41
  X-Request-Id:
40
- - 31c54ba5-3226-4433-9950-ddd9fadc1c64
42
+ - 2d917c16-fc5f-4e9e-8d96-2d1e951efd3c
41
43
  X-Runtime:
42
- - '0.105063'
44
+ - '0.042517'
43
45
  Transfer-Encoding:
44
46
  - chunked
45
- Via:
46
- - 1.1 vegur
47
47
  body:
48
48
  encoding: UTF-8
49
- string: '{"id":6,"username":"new_username","created_at":"2015-01-04T20:36:28.339Z","updated_at":"2015-01-04T20:36:28.339Z"}'
49
+ string: '{"id":2,"username":"updated_username","created_at":"2015-08-11 05:46:28
50
+ +0000","updated_at":"2015-08-11 05:46:28 +0000"}'
50
51
  http_version:
51
- recorded_at: Sun, 04 Jan 2015 20:37:35 GMT
52
+ recorded_at: Tue, 11 Aug 2015 05:51:01 GMT
52
53
  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_1","password":"new_password_1"}}'
8
+ string: '{"account":{"username":"new_username_1","password":"new_password_1"}}'
9
9
  headers:
10
+ Content-Type:
11
+ - application/json
10
12
  Accept:
11
13
  - application/json
14
+ Content-Md5:
15
+ - 1Z1F1heTE7g9wLVUtGcoZw==
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:27 GMT
24
+ Authorization:
25
+ - APIAuth 1:n8vZc41otcX/Xb1f23RafC2qd04=
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:28 GMT
29
- Connection:
30
- - keep-alive
31
31
  X-Frame-Options:
32
32
  - SAMEORIGIN
33
33
  X-Xss-Protection:
@@ -37,52 +37,51 @@ http_interactions:
37
37
  Content-Type:
38
38
  - application/json; charset=utf-8
39
39
  Etag:
40
- - '"f21a93c307ee794341d6e751a83b5c44"'
40
+ - W/"1ede0faa3e22ec7cbb3d09f5e540c07d"
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
- - 60e75ec5-53eb-4552-8e86-29f2271351ee
46
+ - 1d6fa3af-982e-43f8-bb51-5abb295a7aee
47
47
  X-Runtime:
48
- - '0.199750'
48
+ - '0.165223'
49
49
  Transfer-Encoding:
50
50
  - chunked
51
- Via:
52
- - 1.1 vegur
53
51
  body:
54
52
  encoding: UTF-8
55
- string: '{"id":7,"username":"new_username_1","created_at":"2015-01-04T20:36:28.949Z","updated_at":"2015-01-04T20:36:28.949Z"}'
53
+ string: '{"id":2,"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:36:28 GMT
56
+ recorded_at: Tue, 11 Aug 2015 05:46:28 GMT
58
57
  - request:
59
58
  method: put
60
- uri: http://account-authenticator.herokuapp.com/api/v1/accounts/7
59
+ uri: http://127.0.0.1:3000/api/v2/accounts/2
61
60
  body:
62
61
  encoding: UTF-8
63
- string: '{"api_key":"124ecd49-07fd-4553-a5cd-0178b7fa8b3f","api_password":"IIoclO7kmQqJ1wixWrAuOA","account":{"username":"updated_username","password":"updated_password"}}'
62
+ string: '{"account":{"username":"updated_username","password":"updated_password"}}'
64
63
  headers:
64
+ Content-Type:
65
+ - application/json
65
66
  Accept:
66
67
  - application/json
68
+ Content-Md5:
69
+ - wgNL0L93RiZqqaXV8CYeYw==
67
70
  Accept-Encoding:
68
- - gzip, deflate
69
- Content-Type:
70
- - application/json
71
- Content-Length:
72
- - '162'
71
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
73
72
  User-Agent:
74
73
  - Ruby
74
+ Host:
75
+ - 127.0.0.1:3000
76
+ Date:
77
+ - Tue, 11 Aug 2015 05:46:28 GMT
78
+ Authorization:
79
+ - APIAuth 1:RzvvQ7IXETtpnxT4t9VvAFvpoBg=
75
80
  response:
76
81
  status:
77
82
  code: 200
78
83
  message: OK
79
84
  headers:
80
- Server:
81
- - Cowboy
82
- Date:
83
- - Sun, 04 Jan 2015 20:36:28 GMT
84
- Connection:
85
- - keep-alive
86
85
  X-Frame-Options:
87
86
  - SAMEORIGIN
88
87
  X-Xss-Protection:
@@ -92,22 +91,21 @@ http_interactions:
92
91
  Content-Type:
93
92
  - application/json; charset=utf-8
94
93
  Etag:
95
- - '"80904310aa88a944d24696f6d4c81fa1"'
94
+ - W/"1ede0faa3e22ec7cbb3d09f5e540c07d"
96
95
  Cache-Control:
97
96
  - max-age=0, private, must-revalidate
98
97
  Set-Cookie:
99
98
  - request_method=PUT; path=/
100
99
  X-Request-Id:
101
- - c82ee34e-2392-4999-a65b-e646f09a8495
100
+ - 03f29738-2fb9-4033-a453-bd6cb3764e90
102
101
  X-Runtime:
103
- - '0.253034'
102
+ - '0.164451'
104
103
  Transfer-Encoding:
105
104
  - chunked
106
- Via:
107
- - 1.1 vegur
108
105
  body:
109
106
  encoding: UTF-8
110
- string: '{"id":7,"username":"updated_username","created_at":"2015-01-04T20:36:28.949Z","updated_at":"2015-01-04T20:36:29.353Z"}'
107
+ string: '{"id":2,"created_at":"2015-08-11 05:46:28 +0000","updated_at":"2015-08-11
108
+ 05:46:28 +0000"}'
111
109
  http_version:
112
- recorded_at: Sun, 04 Jan 2015 20:36:29 GMT
110
+ recorded_at: Tue, 11 Aug 2015 05:46:28 GMT
113
111
  recorded_with: VCR 2.9.3
@@ -9,19 +9,23 @@ VCR.configure do |c|
9
9
  c.hook_into :webmock
10
10
  end
11
11
 
12
- describe JsonClient::BaseResponses::Response do
12
+ describe JsonClient::Response do
13
13
  let(:code) do
14
14
  200
15
15
  end
16
16
 
17
17
  let(:body) do
18
- %q<{ "hello" : "world" }>
18
+ '{ "hello" : "world" }'
19
19
  end
20
20
 
21
21
  let(:response) do
22
22
  Object.new.tap do |r|
23
- def r.code; 200; end
24
- def r.body; %q<{ "hello" : "world" }>; end
23
+ def r.code
24
+ 200
25
+ end
26
+ def r.body
27
+ '{ "hello" : "world" }'
28
+ end
25
29
  end
26
30
  end
27
31
 
@@ -11,39 +11,49 @@ end
11
11
  describe JsonClient::Base do
12
12
  let(:config) do
13
13
  {
14
- api_key: '124ecd49-07fd-4553-a5cd-0178b7fa8b3f',
15
- api_password: 'IIoclO7kmQqJ1wixWrAuOA',
16
- host: 'http://account-authenticator.herokuapp.com'
14
+ client_id: '1',
15
+ secret_key: 'dummmy_key',
16
+ host: '127.0.0.1',
17
+ port: '3000'
17
18
  }
18
19
  end
19
20
 
20
- let(:pather) do
21
- JsonClient::Pather.new(
21
+ let(:uri_builder) do
22
+ JsonClient::UriBuilder.new(
22
23
  config[:host],
23
- 'api/v1',
24
- 'accounts'
24
+ 'api/v2',
25
+ 'accounts',
26
+ config[:port]
25
27
  )
26
28
  end
27
29
 
28
30
  let(:account) do
29
- { username: 'new_username', password: 'new_password' }
31
+ {
32
+ account: { username: 'new_username', password: 'new_password' }
33
+ }
30
34
  end
31
35
 
32
36
  let(:new_account) do
33
- { username: 'new_username_1', password: 'new_password_1' }
37
+ {
38
+ account: { username: 'new_username_1', password: 'new_password_1' }
39
+ }
34
40
  end
35
41
 
36
42
  let(:destroy_account) do
37
- { username: 'new_username_2', password: 'new_password_2' }
43
+ {
44
+ account: { username: 'new_username_2', password: 'new_password_2' }
45
+ }
38
46
  end
39
47
 
40
48
  let(:updated_account) do
41
- { username: 'updated_username', password: 'updated_password' }
49
+ {
50
+ account: { username: 'updated_username', password: 'updated_password' }
51
+ }
42
52
  end
43
53
 
44
54
  subject do
45
55
  described_class.new(
46
- pather,
56
+ uri_builder,
47
57
  config
48
58
  )
49
59
  end
@@ -74,12 +84,11 @@ describe JsonClient::Base do
74
84
  describe '#show' do
75
85
  it 'fetches the account' do
76
86
  VCR.use_cassette('show_success') do
77
- response = subject.show(6).json
87
+ response = subject.show(2).json
78
88
 
79
- expect(response['username']).to eq 'new_username'
80
- expect(response['id']).to be 6
81
- expect(response['created_at']).to eq '2015-01-04T20:36:28.339Z'
82
- expect(response['updated_at']).to eq '2015-01-04T20:36:28.339Z'
89
+ expect(response['id']).to be 2
90
+ expect(response['created_at']).to eq '2015-08-11 05:46:28 +0000'
91
+ expect(response['updated_at']).to eq '2015-08-11 05:46:28 +0000'
83
92
  end
84
93
  end
85
94
  end
@@ -87,7 +96,9 @@ describe JsonClient::Base do
87
96
  describe '#update' do
88
97
  it 'updates the account' do
89
98
  VCR.use_cassette('update_success') do
90
- id = subject.create(new_account).json['id']
99
+ r = subject.create(new_account)
100
+ id = r.json['id']
101
+
91
102
  response = subject.update(id, updated_account).json
92
103
 
93
104
  expect(response['username']).to eq updated_account[:username]
@@ -99,7 +110,9 @@ describe JsonClient::Base do
99
110
  describe '#destroy' do
100
111
  it 'destroys the account' do
101
112
  VCR.use_cassette('destroy_success') do
102
- id = subject.create(destroy_account).json['id']
113
+ r = subject.create(destroy_account)
114
+ id = r.json['id']
115
+
103
116
  response = subject.destroy(id).json
104
117
 
105
118
  expect(response['id']).to eq id
@@ -37,18 +37,16 @@ describe JsonClient::DSL do
37
37
  example_class.new.serializers
38
38
  end
39
39
 
40
- let(:requests) do
41
- example_class.new.requests
40
+ let(:requestors) do
41
+ example_class.new.requestors
42
42
  end
43
43
 
44
- let(:responses) do
45
- example_class.new.responses
44
+ let(:responders) do
45
+ example_class.new.responders
46
46
  end
47
47
 
48
-
49
48
  describe '#serializers' do
50
49
  it 'adds a way of dynamically adding serializers' do
51
-
52
50
  expect(serializers.index).to eq 'hello'
53
51
  expect(serializers.show).to eq 'hello'
54
52
  expect(serializers.read).to eq 'hello'
@@ -57,17 +55,17 @@ describe JsonClient::DSL do
57
55
  end
58
56
  end
59
57
 
60
- describe '#responses' do
58
+ describe '#responders' do
61
59
  it 'adds a way of dynamically adding responses' do
62
- expect(responses.index).to eq '0'
63
- expect(responses.show).to eq '1'
60
+ expect(responders.index).to eq '0'
61
+ expect(responders.show).to eq '1'
64
62
  end
65
63
  end
66
64
 
67
- describe '#requests' do
65
+ describe '#requestors' do
68
66
  it 'adds a way of dynamically adding requests' do
69
- expect(requests.index).to eq 'abc'
70
- expect(requests.delete).to eq 'xyz'
67
+ expect(requestors.index).to eq 'abc'
68
+ expect(requestors.delete).to eq 'xyz'
71
69
  end
72
70
  end
73
71
  end
@@ -14,20 +14,30 @@ describe JsonClient::ModelSerializer do
14
14
  end
15
15
 
16
16
  let(:model) do
17
- {a: 0, b: 1}
17
+ { a: 0, b: 1 }
18
18
  end
19
19
 
20
- let(:serialized_model) do
21
- {account: {a: 0, b: 1}}
20
+ let(:hash_model) do
21
+ { account: { a: 0, b: 1 } }
22
+ end
23
+
24
+ let(:serialized) do
25
+ hash_model.to_json
22
26
  end
23
27
 
24
28
  subject do
25
29
  described_class.new(model_name: model_name)
26
30
  end
27
31
 
32
+ describe '#to_h' do
33
+ it 'serializes the model adds the model name as key' do
34
+ expect(subject.to_h(model)).to eq hash_model
35
+ end
36
+ end
37
+
28
38
  describe '#serialize' do
29
39
  it 'serializes the model adds the model name as key' do
30
- expect(subject.serialize(model)).to eq serialized_model
40
+ expect(subject.serialize(model)).to eq serialized
31
41
  end
32
42
  end
33
43
  end
@@ -8,9 +8,9 @@ VCR.configure do |c|
8
8
  c.hook_into :webmock
9
9
  end
10
10
 
11
- describe JsonClient::Pather do
11
+ describe JsonClient::UriBuilder do
12
12
  let(:host) do
13
- 'https://www.example.com'
13
+ 'www.example.com'
14
14
  end
15
15
 
16
16
  let(:relative_path) do
@@ -22,11 +22,19 @@ describe JsonClient::Pather do
22
22
  end
23
23
 
24
24
  let(:example_show_path) do
25
- 'https://www.example.com/api/v1/models/1'
25
+ 'www.example.com:80/api/v1/models/1'
26
26
  end
27
27
 
28
28
  let(:example_index_path) do
29
- 'https://www.example.com/api/v1/models'
29
+ 'www.example.com:80/api/v1/models'
30
+ end
31
+
32
+ let(:example_show_uri) do
33
+ 'http://www.example.com/api/v1/models/1'
34
+ end
35
+
36
+ let(:example_index_uri) do
37
+ 'http://www.example.com/api/v1/models'
30
38
  end
31
39
 
32
40
  subject do
@@ -48,4 +56,18 @@ describe JsonClient::Pather do
48
56
  end
49
57
  end
50
58
  end
59
+
60
+ describe '#uri' do
61
+ context 'with id not given' do
62
+ it 'creates the full path with id at the end' do
63
+ expect(subject.uri.to_s).to eq example_index_uri
64
+ end
65
+ end
66
+
67
+ context 'with id given' do
68
+ it 'creates the full path with id at the end' do
69
+ expect(subject.uri(1).to_s).to eq example_show_uri
70
+ end
71
+ end
72
+ end
51
73
  end