json_client 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +7 -1
  3. data/README.md +53 -3
  4. data/json_client.gemspec +2 -0
  5. data/lib/json_client/base.rb +80 -0
  6. data/lib/json_client/base_requests/create.rb +22 -0
  7. data/lib/json_client/base_requests/destroy.rb +13 -0
  8. data/lib/json_client/base_requests/index.rb +11 -0
  9. data/lib/json_client/base_requests/request.rb +12 -0
  10. data/lib/json_client/base_requests/show.rb +11 -0
  11. data/lib/json_client/base_requests/update.rb +22 -0
  12. data/lib/json_client/{abstract_responses → base_responses}/create.rb +1 -1
  13. data/lib/json_client/{abstract_responses → base_responses}/destroy.rb +1 -1
  14. data/lib/json_client/{abstract_responses → base_responses}/index.rb +1 -1
  15. data/lib/json_client/{abstract_responses → base_responses}/response.rb +1 -1
  16. data/lib/json_client/{abstract_responses → base_responses}/show.rb +1 -1
  17. data/lib/json_client/{abstract_responses → base_responses}/update.rb +1 -1
  18. data/lib/json_client/requests.rb +29 -0
  19. data/lib/json_client/responses.rb +29 -0
  20. data/lib/json_client/version.rb +1 -1
  21. data/lib/json_client.rb +2 -2
  22. data/spec/json_client/base_requests/request_spec.rb +14 -0
  23. data/spec/json_client/base_responses/response_spec.rb +49 -0
  24. data/spec/json_client/base_spec.rb +109 -0
  25. data/spec/json_client/pather_spec.rb +51 -0
  26. data/spec/json_client/requests_spec.rb +43 -0
  27. data/spec/json_client/responses_spec.rb +43 -0
  28. data/spec/json_client_spec.rb +19 -13
  29. data/spec/spec_helper.rb +3 -0
  30. metadata +57 -9
  31. data/lib/json_client/abstract_client.rb +0 -111
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f417e4ea4aa055a0b430860a252a43c30d11db30
4
- data.tar.gz: b37605aa0bbe431f75313288e2061cdd6da3651e
3
+ metadata.gz: e7cb4bba261119f65721f26b628f479a38e75f51
4
+ data.tar.gz: 22b36d3aed2ef8d04dc893b687fc2e41cb4d9267
5
5
  SHA512:
6
- metadata.gz: 00e47ebbf6d376b26cbe63dcabb94a52c77ba8a0b412970448c6dfe3643d158e3f2eb9cc1dc62f373e04699e1c6eeb21df8a10f515b822abb7715046fc26ceba
7
- data.tar.gz: 3e027176248cbe4247bf63903e69c3ed4321ad8c65fb5997dcc68f2da5fe7bd509ecd0e9830742278b305285601c9e3b834c98d68749e889a8748e4d3c9f075a
6
+ metadata.gz: 6bf62b6375988b27e103251979fa0127b6a08882b9d46c57726f32ce088c0323dfa5264dc375a9ff9fa7de213c1f1cf8b684085603cfd6c7f2d4db59ecbb504d
7
+ data.tar.gz: a860985b6ffdbf33c15fb8cfd94928aa85712ccbe099b3686f2cbf300f1575be21d8a6d78e4b93cb20be792a729cffb0fc41f774e950b0827e34e946cf0f4291
data/.travis.yml CHANGED
@@ -8,9 +8,15 @@ gemfile:
8
8
 
9
9
  cache: bundler
10
10
 
11
+ env:
12
+ matrix:
13
+ - TEST_SUITE="spec"
14
+
15
+
11
16
  before_script: "bundle update"
12
17
 
13
- script: "bundle exec rake spec"
18
+ script:
19
+ - bundle exec rake $TEST_SUITE
14
20
 
15
21
  notifications:
16
22
  email: false
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # JsonClient
2
+ [![Build
3
+ Status](https://travis-ci.org/johnmcconnell/json_client.svg?branch=master)](https://travis-ci.org/johnmcconnell/json_client)
4
+ [![Coverage
5
+ Status](https://coveralls.io/repos/johnmcconnell/json_client/badge.png?branch=master)](https://coveralls.io/r/johnmcconnell/json_client?branch=master)
2
6
 
3
- TODO: Write a gem description
7
+ A simple crud json client so that I don't need to do this for
8
+ all my web services
4
9
 
5
10
  ## Installation
6
11
 
@@ -20,11 +25,56 @@ Or install it yourself as:
20
25
 
21
26
  ## Usage
22
27
 
23
- TODO: Write usage instructions here
28
+ I've avoided using a DSL in favor of good engineering principals. I am
29
+ hoping the DSL will come after.
30
+
31
+
32
+ ```
33
+ require 'json_client'
34
+
35
+ class Client < JsonClient::Base
36
+ def initialize(config)
37
+ super
38
+ end
39
+
40
+ def pather
41
+ @pather ||=
42
+ JsonClient::Pather.new(
43
+ 'https://example.host.com',
44
+ 'api/v1',
45
+ 'objects'
46
+ )
47
+ end
48
+ end
49
+
50
+ config = {
51
+ api_key: 'api_key',
52
+ api_password: 'api_password'
53
+ }
54
+
55
+
56
+ client = Client.new(config)
57
+
58
+ # GET https://example.host.com/api/v1/objects?api_key=api_key&api_password=api_password
59
+ client.index.json
60
+ => { 'server_json' : 'is_parsed_here' }
61
+
62
+
63
+
64
+ # GET https://my.host.com/api/v1/objects/2?api_key=api_key&api_password=api_password
65
+ client.show(2).json # fetches from
66
+ => { 'object_json' : 'is_parsed_here' }
67
+ ```
68
+
69
+ ## Best Guide
70
+
71
+ Is [this
72
+ test](https://github.com/johnmcconnell/json_client/blob/master/spec/json_client/base_spec.rb).
73
+ Sorry non rspec people
24
74
 
25
75
  ## Contributing
26
76
 
27
- 1. Fork it ( https://github.com/[my-github-username]/json_client/fork )
77
+ 1. Fork it ( https://github.com/johnmcconnell/json_client/fork )
28
78
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
79
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
80
  4. Push to the branch (`git push origin my-new-feature`)
data/json_client.gemspec CHANGED
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'rest-client', '~> 1.7.2'
22
22
 
23
+ spec.add_development_dependency "simplecov", "~> 0.8.0"
24
+ spec.add_development_dependency "coveralls", "~> 0.7.0"
23
25
  spec.add_development_dependency "rake", "~> 10.0"
24
26
  spec.add_development_dependency "rspec"
25
27
  spec.add_development_dependency "rspec-collection_matchers"
@@ -0,0 +1,80 @@
1
+ require 'rest_client'
2
+ require 'json_client/responses'
3
+ require 'json_client/requests'
4
+
5
+ module JsonClient
6
+ class Base
7
+ attr_reader :api_key, :api_password, :pather
8
+
9
+ def initialize(pather, config)
10
+ @api_key = config[:api_key]
11
+ @api_password = config[:api_password]
12
+ @pather = pather
13
+ validate_variables
14
+ end
15
+
16
+ def index
17
+ response = requestors.index.new.fetch(request_path, auth_params)
18
+ responders.index.new(response.body, response.code)
19
+ end
20
+
21
+ def show(id)
22
+ response = requestors.show.new.fetch(request_path(id), auth_params)
23
+ responders.show.new(response.body, response.code)
24
+ end
25
+
26
+ def create(model)
27
+ response = requestors.create.new.fetch(
28
+ request_path, auth_params, model
29
+ )
30
+ responders.create.new(response.body, response.code)
31
+ end
32
+
33
+ def update(id, model)
34
+ response = requestors.update.new.fetch(
35
+ request_path(id), auth_params, model
36
+ )
37
+ responders.update.new(response.body, response.code)
38
+ end
39
+
40
+ def destroy(id)
41
+ response = requestors.destroy.new.fetch(
42
+ request_path(id), auth_params
43
+ )
44
+ responders.destroy.new(response.body, response.code)
45
+ end
46
+
47
+ protected
48
+
49
+ def requestors
50
+ @requestors ||= Requests.new
51
+ end
52
+
53
+ def responders
54
+ @responders ||= Responses.new
55
+ end
56
+
57
+ def update_params(model)
58
+ model.to_json
59
+ end
60
+
61
+ def request_path(id = nil)
62
+ pather.path(id)
63
+ end
64
+
65
+ def auth_params
66
+ {
67
+ api_key: api_key,
68
+ api_password: api_password
69
+ }
70
+ end
71
+
72
+ private
73
+
74
+ def validate_variables
75
+ fail 'api_key must be set' if api_key.nil?
76
+ fail 'api_password must be set' if api_password.nil?
77
+ fail 'pather must be set' if pather.nil?
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'request'
2
+
3
+ module JsonClient
4
+ module BaseRequests
5
+ class Create < Request
6
+ def fetch(url, auth_params, model)
7
+ client.post(
8
+ url,
9
+ auth_params.merge(create_params(model)).to_json,
10
+ content_type: :json,
11
+ accept: :json
12
+ )
13
+ end
14
+
15
+ private
16
+
17
+ def create_params(model)
18
+ model.to_h
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'request'
2
+
3
+ module JsonClient
4
+ module BaseRequests
5
+ class Destroy < Request
6
+ def fetch(url, auth_params)
7
+ client.delete(
8
+ url, params: auth_params
9
+ )
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'request'
2
+
3
+ module JsonClient
4
+ module BaseRequests
5
+ class Index < Request
6
+ def fetch(url, auth_params)
7
+ client.get url, params: auth_params
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module JsonClient
2
+ module BaseRequests
3
+ class Request
4
+
5
+ protected
6
+
7
+ def client
8
+ RestClient
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'request'
2
+
3
+ module JsonClient
4
+ module BaseRequests
5
+ class Show < Request
6
+ def fetch(url, auth_params)
7
+ client.get url, params: auth_params
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'request'
2
+
3
+ module JsonClient
4
+ module BaseRequests
5
+ class Update < Request
6
+ def fetch(url, auth_params, model)
7
+ client.put(
8
+ url,
9
+ auth_params.merge(update_params(model)).to_json,
10
+ content_type: :json,
11
+ accept: :json
12
+ )
13
+ end
14
+
15
+ protected
16
+
17
+ def update_params(model)
18
+ model.to_h
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,7 +1,7 @@
1
1
  require_relative 'response'
2
2
 
3
3
  module JsonClient
4
- module AbstractResponses
4
+ module BaseResponses
5
5
  class Create < Response
6
6
  def initialize(body, code)
7
7
  super
@@ -1,7 +1,7 @@
1
1
  require_relative 'response'
2
2
 
3
3
  module JsonClient
4
- module AbstractResponses
4
+ module BaseResponses
5
5
  class Destroy < Response
6
6
  def initialize(body, code)
7
7
  super
@@ -1,7 +1,7 @@
1
1
  require_relative 'response'
2
2
 
3
3
  module JsonClient
4
- module AbstractResponses
4
+ module BaseResponses
5
5
  class Index < Response
6
6
  def initialize(body, code)
7
7
  super
@@ -1,5 +1,5 @@
1
1
  module JsonClient
2
- module AbstractResponses
2
+ module BaseResponses
3
3
  class Response
4
4
  attr_reader :body, :code
5
5
 
@@ -1,7 +1,7 @@
1
1
  require_relative 'response'
2
2
 
3
3
  module JsonClient
4
- module AbstractResponses
4
+ module BaseResponses
5
5
  class Show < Response
6
6
  def initialize(body, code)
7
7
  super
@@ -1,7 +1,7 @@
1
1
  require_relative 'response'
2
2
 
3
3
  module JsonClient
4
- module AbstractResponses
4
+ module BaseResponses
5
5
  class Update < Response
6
6
  def initialize(body, code)
7
7
  super
@@ -0,0 +1,29 @@
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'
6
+
7
+ module JsonClient
8
+ class Requests
9
+ def index
10
+ BaseRequests::Index
11
+ end
12
+
13
+ def show
14
+ BaseRequests::Show
15
+ end
16
+
17
+ def create
18
+ BaseRequests::Create
19
+ end
20
+
21
+ def update
22
+ BaseRequests::Update
23
+ end
24
+
25
+ def destroy
26
+ BaseRequests::Destroy
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'base_responses/index'
2
+ require_relative 'base_responses/show'
3
+ require_relative 'base_responses/create'
4
+ require_relative 'base_responses/update'
5
+ require_relative 'base_responses/destroy'
6
+
7
+ module JsonClient
8
+ class Responses
9
+ def index
10
+ BaseResponses::Index
11
+ end
12
+
13
+ def show
14
+ BaseResponses::Show
15
+ end
16
+
17
+ def create
18
+ BaseResponses::Create
19
+ end
20
+
21
+ def update
22
+ BaseResponses::Update
23
+ end
24
+
25
+ def destroy
26
+ BaseResponses::Destroy
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module JsonClient
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/json_client.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require "json_client/version"
2
- require "json_client/abstract_client"
2
+ require "json_client/base"
3
3
  require "json_client/pather"
4
4
 
5
5
  module JsonClient
6
6
  def self.new(pather, config)
7
- AbstractClient.new(pather, config)
7
+ Base.new(pather, config)
8
8
  end
9
9
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require 'rspec/collection_matchers'
3
+ require 'json_client'
4
+ require 'vcr'
5
+ require 'json'
6
+
7
+ VCR.configure do |c|
8
+ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
9
+ c.hook_into :webmock
10
+ end
11
+
12
+ describe JsonClient::BaseRequests::Request do
13
+ pending '# TODO'
14
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'rspec/collection_matchers'
3
+ require 'json_client'
4
+ require 'vcr'
5
+ require 'json'
6
+
7
+ VCR.configure do |c|
8
+ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
9
+ c.hook_into :webmock
10
+ end
11
+
12
+ describe JsonClient::BaseResponses::Response do
13
+ let(:code) do
14
+ 200
15
+ end
16
+
17
+ let(:body) do
18
+ %q<{ "hello" : "world" }>
19
+ end
20
+
21
+ let(:json) do
22
+ JSON.parse(body)
23
+ end
24
+
25
+ subject do
26
+ described_class.new(
27
+ body,
28
+ code
29
+ )
30
+ end
31
+
32
+ describe '#json' do
33
+ it 'returns the parsed json from the body' do
34
+ expect(subject.json).to eq json
35
+ end
36
+ end
37
+
38
+ describe '#body' do
39
+ it 'returns the body' do
40
+ expect(subject.body).to eq body
41
+ end
42
+ end
43
+
44
+ describe '#code' do
45
+ it 'returns the response code' do
46
+ expect(subject.code).to eq code
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+ require 'rspec/collection_matchers'
3
+ require 'json_client'
4
+ require 'vcr'
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
8
+ c.hook_into :webmock
9
+ end
10
+
11
+ describe JsonClient::Base do
12
+ let(:config) do
13
+ {
14
+ api_key: '124ecd49-07fd-4553-a5cd-0178b7fa8b3f',
15
+ api_password: 'IIoclO7kmQqJ1wixWrAuOA',
16
+ host: 'http://account-authenticator.herokuapp.com'
17
+ }
18
+ end
19
+
20
+ let(:pather) do
21
+ JsonClient::Pather.new(
22
+ config[:host],
23
+ 'api/v1',
24
+ 'accounts'
25
+ )
26
+ end
27
+
28
+ let(:account) do
29
+ { username: 'new_username', password: 'new_password' }
30
+ end
31
+
32
+ let(:new_account) do
33
+ { username: 'new_username_1', password: 'new_password_1' }
34
+ end
35
+
36
+ let(:destroy_account) do
37
+ { username: 'new_username_2', password: 'new_password_2' }
38
+ end
39
+
40
+ let(:updated_account) do
41
+ { username: 'updated_username', password: 'updated_password' }
42
+ end
43
+
44
+ subject do
45
+ described_class.new(
46
+ pather,
47
+ config
48
+ )
49
+ end
50
+
51
+ describe '#all' do
52
+ it 'fetches all the accounts' do
53
+ VCR.use_cassette('all_success') do
54
+ response = subject.index.json
55
+
56
+ expect(response['accounts']).not_to be nil
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#create' do
62
+ it 'creates an account' do
63
+ VCR.use_cassette('create_success') do
64
+ response = subject.create(account).json
65
+
66
+ expect(response['username']).to eq 'new_username'
67
+ expect(response['id']).not_to be nil
68
+ expect(response['created_at']).not_to be nil
69
+ expect(response['updated_at']).not_to be nil
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '#show' do
75
+ it 'fetches the account' do
76
+ VCR.use_cassette('show_success') do
77
+ response = subject.show(6).json
78
+
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'
83
+ end
84
+ end
85
+ end
86
+
87
+ describe '#update' do
88
+ it 'updates the account' do
89
+ VCR.use_cassette('update_success') do
90
+ id = subject.create(new_account).json['id']
91
+ response = subject.update(id, updated_account).json
92
+
93
+ expect(response['username']).to eq updated_account[:username]
94
+ expect(response['id']).to eq id
95
+ end
96
+ end
97
+ end
98
+
99
+ describe '#destroy' do
100
+ it 'destroys the account' do
101
+ VCR.use_cassette('destroy_success') do
102
+ id = subject.create(destroy_account).json['id']
103
+ response = subject.destroy(id).json
104
+
105
+ expect(response['id']).to eq id
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'rspec/collection_matchers'
3
+ require 'json_client'
4
+ require 'vcr'
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
8
+ c.hook_into :webmock
9
+ end
10
+
11
+ describe JsonClient::Pather do
12
+ let(:host) do
13
+ 'https://www.example.com'
14
+ end
15
+
16
+ let(:relative_path) do
17
+ 'api/v1'
18
+ end
19
+
20
+ let(:model_name) do
21
+ 'models'
22
+ end
23
+
24
+ let(:example_show_path) do
25
+ 'https://www.example.com/api/v1/models/1'
26
+ end
27
+
28
+ let(:example_index_path) do
29
+ 'https://www.example.com/api/v1/models'
30
+ end
31
+
32
+ subject do
33
+ described_class.new(
34
+ host, relative_path, model_name
35
+ )
36
+ end
37
+
38
+ describe '#path' do
39
+ context 'with id not given' do
40
+ it 'creates the full path with id at the end' do
41
+ expect(subject.path).to eq example_index_path
42
+ end
43
+ end
44
+
45
+ context 'with id given' do
46
+ it 'creates the full path with id at the end' do
47
+ expect(subject.path(1)).to eq example_show_path
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'rspec/collection_matchers'
3
+ require 'json_client'
4
+ require 'vcr'
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
8
+ c.hook_into :webmock
9
+ end
10
+
11
+ describe JsonClient::Requests do
12
+ subject { described_class.new }
13
+
14
+ describe '#index' do
15
+ it 'returns a index request class' do
16
+ expect(subject.index).to be JsonClient::BaseRequests::Index
17
+ end
18
+ end
19
+
20
+ describe '#show' do
21
+ it 'returns a show request class' do
22
+ expect(subject.show).to be JsonClient::BaseRequests::Show
23
+ end
24
+ end
25
+
26
+ describe '#create' do
27
+ it 'returns a create request class' do
28
+ expect(subject.create).to be JsonClient::BaseRequests::Create
29
+ end
30
+ end
31
+
32
+ describe '#update' do
33
+ it 'returns an update request class' do
34
+ expect(subject.update).to be JsonClient::BaseRequests::Update
35
+ end
36
+ end
37
+
38
+ describe '#destroy' do
39
+ it 'returns a destroy request class' do
40
+ expect(subject.destroy).to be JsonClient::BaseRequests::Destroy
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'rspec/collection_matchers'
3
+ require 'json_client'
4
+ require 'vcr'
5
+
6
+ VCR.configure do |c|
7
+ c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
8
+ c.hook_into :webmock
9
+ end
10
+
11
+ describe JsonClient::Responses do
12
+ subject { described_class.new }
13
+
14
+ describe '#index' do
15
+ it 'returns a index response class' do
16
+ expect(subject.index).to be JsonClient::BaseResponses::Index
17
+ end
18
+ end
19
+
20
+ describe '#show' do
21
+ it 'returns a show response class' do
22
+ expect(subject.show).to be JsonClient::BaseResponses::Show
23
+ end
24
+ end
25
+
26
+ describe '#create' do
27
+ it 'returns a create response class' do
28
+ expect(subject.create).to be JsonClient::BaseResponses::Create
29
+ end
30
+ end
31
+
32
+ describe '#update' do
33
+ it 'returns an update response class' do
34
+ expect(subject.update).to be JsonClient::BaseResponses::Update
35
+ end
36
+ end
37
+
38
+ describe '#destroy' do
39
+ it 'returns a destroy response class' do
40
+ expect(subject.destroy).to be JsonClient::BaseResponses::Destroy
41
+ end
42
+ end
43
+ end
@@ -17,19 +17,25 @@ describe JsonClient do
17
17
  }
18
18
  end
19
19
 
20
- describe '::register' do
21
- it 'registers a configuration to be used when invoked with ::new' do
22
- client = described_class.new(
23
- JsonClient::Pather.new(
24
- config[:host],
25
- 'etc',
26
- 'end'
27
- ),
28
- config
29
- )
30
- expect(client.pather.host).to eq config[:host]
31
- expect(client.api_key).to eq config[:api_key]
32
- expect(client.api_password).to eq config[:api_password]
20
+ let(:pather) do
21
+ JsonClient::Pather.new(
22
+ config[:host],
23
+ 'etc',
24
+ 'end'
25
+ )
26
+ end
27
+
28
+ subject do
29
+ described_class.new(
30
+ pather,
31
+ config
32
+ )
33
+ end
34
+
35
+ describe '::new' do
36
+ it 'creates a new base client' do
37
+ expect(subject.api_key).to eq config[:api_key]
38
+ expect(subject.api_password).to eq config[:api_password]
33
39
  end
34
40
  end
35
41
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'json_client'
2
2
  require 'vcr'
3
+ require 'coveralls'
4
+
5
+ Coveralls.wear!
3
6
 
4
7
  class SpecHelper
5
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - johnmcconnell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-06 00:00:00.000000000 Z
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.7.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.7.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.7.0
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -109,14 +137,22 @@ files:
109
137
  - Rakefile
110
138
  - json_client.gemspec
111
139
  - lib/json_client.rb
112
- - lib/json_client/abstract_client.rb
113
- - lib/json_client/abstract_responses/create.rb
114
- - lib/json_client/abstract_responses/destroy.rb
115
- - lib/json_client/abstract_responses/index.rb
116
- - lib/json_client/abstract_responses/response.rb
117
- - lib/json_client/abstract_responses/show.rb
118
- - lib/json_client/abstract_responses/update.rb
140
+ - lib/json_client/base.rb
141
+ - lib/json_client/base_requests/create.rb
142
+ - lib/json_client/base_requests/destroy.rb
143
+ - lib/json_client/base_requests/index.rb
144
+ - lib/json_client/base_requests/request.rb
145
+ - lib/json_client/base_requests/show.rb
146
+ - lib/json_client/base_requests/update.rb
147
+ - lib/json_client/base_responses/create.rb
148
+ - lib/json_client/base_responses/destroy.rb
149
+ - lib/json_client/base_responses/index.rb
150
+ - lib/json_client/base_responses/response.rb
151
+ - lib/json_client/base_responses/show.rb
152
+ - lib/json_client/base_responses/update.rb
119
153
  - lib/json_client/pather.rb
154
+ - lib/json_client/requests.rb
155
+ - lib/json_client/responses.rb
120
156
  - lib/json_client/version.rb
121
157
  - spec/fixtures/vcr_cassettes/all_success.yml
122
158
  - spec/fixtures/vcr_cassettes/authenticate_success.yml
@@ -124,6 +160,12 @@ files:
124
160
  - spec/fixtures/vcr_cassettes/destroy_success.yml
125
161
  - spec/fixtures/vcr_cassettes/show_success.yml
126
162
  - spec/fixtures/vcr_cassettes/update_success.yml
163
+ - spec/json_client/base_requests/request_spec.rb
164
+ - spec/json_client/base_responses/response_spec.rb
165
+ - spec/json_client/base_spec.rb
166
+ - spec/json_client/pather_spec.rb
167
+ - spec/json_client/requests_spec.rb
168
+ - spec/json_client/responses_spec.rb
127
169
  - spec/json_client_spec.rb
128
170
  - spec/spec_helper.rb
129
171
  homepage: ''
@@ -157,5 +199,11 @@ test_files:
157
199
  - spec/fixtures/vcr_cassettes/destroy_success.yml
158
200
  - spec/fixtures/vcr_cassettes/show_success.yml
159
201
  - spec/fixtures/vcr_cassettes/update_success.yml
202
+ - spec/json_client/base_requests/request_spec.rb
203
+ - spec/json_client/base_responses/response_spec.rb
204
+ - spec/json_client/base_spec.rb
205
+ - spec/json_client/pather_spec.rb
206
+ - spec/json_client/requests_spec.rb
207
+ - spec/json_client/responses_spec.rb
160
208
  - spec/json_client_spec.rb
161
209
  - spec/spec_helper.rb
@@ -1,111 +0,0 @@
1
- require 'rest_client'
2
-
3
- require_relative 'abstract_responses/index'
4
- require_relative 'abstract_responses/show'
5
- require_relative 'abstract_responses/create'
6
- require_relative 'abstract_responses/update'
7
- require_relative 'abstract_responses/destroy'
8
-
9
- module JsonClient
10
- class AbstractClient
11
- attr_reader :api_key, :api_password, :pather
12
-
13
- def initialize(pather, config)
14
- @api_key = config[:api_key]
15
- @api_password = config[:api_password]
16
- @pather = pather
17
- validate_variables
18
- end
19
-
20
- def index
21
- uri = request_path
22
- response = RestClient.get uri, params: auth_params
23
- index_response_factory.new(response.body, response.code)
24
- end
25
-
26
- def show(id)
27
- uri = request_path(id)
28
- response = RestClient.get uri, params: auth_params
29
- show_response_factory.new(response.body, response.code)
30
- end
31
-
32
- def create(model)
33
- uri = request_path
34
- response = RestClient.post(
35
- uri,
36
- auth_params.merge(create_params(model)).to_json,
37
- content_type: :json,
38
- accept: :json
39
- )
40
- create_response_factory.new(response.body, response.code)
41
- end
42
-
43
- def update(id, model)
44
- uri = request_path(id)
45
- response = RestClient.put(
46
- uri,
47
- auth_params.merge(update_params(model)).to_json,
48
- content_type: :json,
49
- accept: :json
50
- )
51
- update_response_factory.new(response.body, response.code)
52
- end
53
-
54
- def destroy(id)
55
- uri = request_path(id)
56
- response = RestClient.delete(
57
- uri, params: auth_params
58
- )
59
- destroy_response_factory.new(response.body, response.code)
60
- end
61
-
62
- protected
63
-
64
- def index_response_factory
65
- AbstractResponses::Index
66
- end
67
-
68
- def show_response_factory
69
- AbstractResponses::Show
70
- end
71
-
72
- def create_response_factory
73
- AbstractResponses::Create
74
- end
75
-
76
- def update_response_factory
77
- AbstractResponses::Update
78
- end
79
-
80
- def destroy_response_factory
81
- AbstractResponses::Destroy
82
- end
83
-
84
- def create_params(model)
85
- model.to_json
86
- end
87
-
88
- def update_params(model)
89
- model.to_json
90
- end
91
-
92
- def request_path(id = nil)
93
- pather.path(id)
94
- end
95
-
96
- def auth_params
97
- {
98
- api_key: api_key,
99
- api_password: api_password
100
- }
101
- end
102
-
103
- private
104
-
105
- def validate_variables
106
- fail 'api_key must be set' if api_key.nil?
107
- fail 'api_password must be set' if api_key.nil?
108
- fail 'pather must be set' if api_key.nil?
109
- end
110
- end
111
- end