api-client 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v2.3.0
4
+
5
+ * Add support to global mock of requisitions for testing purposes.
6
+
7
+ ## v2.2.0
8
+
9
+ * Add support global basic authentication.
10
+ * Add support for gem Zertico
11
+
3
12
  ## v2.1.0
4
13
 
5
14
  * Add support for classes with compost names.
@@ -139,4 +148,4 @@
139
148
  ## v0.0.1
140
149
 
141
150
  * get method added.
142
- * handler for response code 404 added.
151
+ * handler for response code 404 added.
data/README.md CHANGED
@@ -122,6 +122,18 @@ This code will create a setter and a getter for houses and cars and initialize t
122
122
  Since version 2.0.0, it is possible to make api calls from the object. The syntax is pretty much the same.
123
123
  It will make the call and update the fields with the response. Look at the examples folder to see more code examples
124
124
 
125
+ ## Testing
126
+
127
+ Set mock config variable as true inside yout spec_helper or test_helper:
128
+
129
+ ```ruby
130
+ ApiClient.configure do |config|
131
+ config.mock = true
132
+ end
133
+ ```
134
+
135
+ With this setting no requisitions will be made. All calls will just return a new object with the attributes received.
136
+
125
137
  ## More Examples
126
138
  [Project](https://github.com/zertico/api-client/tree/master/examples)
127
139
 
@@ -14,7 +14,7 @@ module ApiClient
14
14
 
15
15
  # Configures global settings
16
16
  # ApiClient.configure do |config|
17
- # config.url_path = "api.example.com"
17
+ # config.path = "api.example.com"
18
18
  # end
19
19
  def self.configure(&block)
20
20
  yield @config ||= ApiClient::Configuration.new
@@ -29,5 +29,6 @@ module ApiClient
29
29
  configure do |config|
30
30
  config.path = ''
31
31
  config.header = {}
32
+ config.mock = false
32
33
  end
33
34
  end
@@ -17,6 +17,7 @@ module ApiClient
17
17
  # @param [Hash] header hash with the header options.
18
18
  # @return [Base] the object initialized.
19
19
  def get(id, header = {})
20
+ return build(:id => id) if ApiClient.config.mock
20
21
  url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
21
22
  response = ApiClient::Dispatcher.get(url, header)
22
23
  params = ApiClient::Parser.response(response, url)
@@ -31,6 +32,7 @@ module ApiClient
31
32
  # @param [Hash] header hash with the header options.
32
33
  # @return [Base] the object initialized.
33
34
  def post(attributes, header = {})
35
+ return build(attributes) if ApiClient.config.mock
34
36
  url = "#{ApiClient.config.path}#{self.resource_path}"
35
37
  response = ApiClient::Dispatcher.post(url, attributes, header)
36
38
  params = ApiClient::Parser.response(response, url)
@@ -46,6 +48,7 @@ module ApiClient
46
48
  # @param [Hash] header hash with the header options.
47
49
  # @return [Base] the object initialized.
48
50
  def put(attributes, header = {})
51
+ return build(attributes) if ApiClient.config.mock
49
52
  url = "#{ApiClient.config.path}#{self.resource_path}"
50
53
  response = ApiClient::Dispatcher.put(url, attributes, header)
51
54
  params = ApiClient::Parser.response(response, url)
@@ -61,6 +64,7 @@ module ApiClient
61
64
  # @param [Hash] header hash with the header options.
62
65
  # @return [Base] the object initialized.
63
66
  def patch(attributes, header = {})
67
+ return build(attributes) if ApiClient.config.mock
64
68
  url = "#{ApiClient.config.path}#{self.resource_path}"
65
69
  response = ApiClient::Dispatcher.patch(url, attributes, header)
66
70
  params = ApiClient::Parser.response(response, url)
@@ -73,6 +77,7 @@ module ApiClient
73
77
  # @param [Hash] header hash with the header options.
74
78
  # @return [Base] the object initialized.
75
79
  def delete(id, header = {})
80
+ return build(:id => id) if ApiClient.config.mock
76
81
  url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
77
82
  response = ApiClient::Dispatcher.delete(url, header)
78
83
  params = ApiClient::Parser.response(response, url)
@@ -1,6 +1,7 @@
1
1
  module ApiClient
2
2
  # ApiClient::Configuration provides a way to configure ApiClient globally.
3
3
  class Configuration
4
+ attr_accessor :mock
4
5
  attr_reader :header
5
6
 
6
7
  # Return the api url.
@@ -19,6 +19,7 @@ module ApiClient
19
19
  # @param [Hash] header hash with the header options.
20
20
  # @return [Base] the object updated.
21
21
  def get(header = {})
22
+ return update({}) if ApiClient.config.mock
22
23
  url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
23
24
  response = ApiClient::Dispatcher.get(url, header)
24
25
  attributes = ApiClient::Parser.response(response, url)
@@ -32,6 +33,7 @@ module ApiClient
32
33
  # @param [Hash] header hash with the header options.
33
34
  # @return [Base] the object updated.
34
35
  def post(header = {})
36
+ return update({}) if ApiClient.config.mock
35
37
  url = "#{ApiClient.config.path}#{self.class.resource_path}"
36
38
  response = ApiClient::Dispatcher.post(url, self.to_hash, header)
37
39
  attributes = ApiClient::Parser.response(response, url)
@@ -45,6 +47,7 @@ module ApiClient
45
47
  # @param [Hash] header hash with the header options.
46
48
  # @return [Base] the object updated.
47
49
  def put(header = {})
50
+ return update({}) if ApiClient.config.mock
48
51
  url = "#{ApiClient.config.path}#{self.class.resource_path}"
49
52
  response = ApiClient::Dispatcher.put(url, self.to_hash, header)
50
53
  attributes = ApiClient::Parser.response(response, url)
@@ -58,6 +61,7 @@ module ApiClient
58
61
  # @param [Hash] header hash with the header options.
59
62
  # @return [Base] the object updated.
60
63
  def patch(header = {})
64
+ return update({}) if ApiClient.config.mock
61
65
  url = "#{ApiClient.config.path}#{self.class.resource_path}"
62
66
  response = ApiClient::Dispatcher.patch(url, self.to_hash, header)
63
67
  attributes = ApiClient::Parser.response(response, url)
@@ -69,6 +73,7 @@ module ApiClient
69
73
  # @param [Hash] header hash with the header options.
70
74
  # @return [Base] the object updated.
71
75
  def delete(header = {})
76
+ return update({}) if ApiClient.config.mock
72
77
  url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
73
78
  response = ApiClient::Dispatcher.delete(url, header)
74
79
  attributes = ApiClient::Parser.response(response, url)
@@ -1,5 +1,5 @@
1
1
  # High Level Namespace of the library ApiClient.
2
2
  module ApiClient
3
3
  # Version of the library.
4
- VERSION = '2.2.0'
4
+ VERSION = '2.3.0'
5
5
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiClient do
4
+ context 'default settings' do
5
+ it 'should have a blank path' do
6
+ ApiClient.config.path.should == ''
7
+ end
8
+
9
+ it 'should have an empty header' do
10
+ ApiClient.config.header.should == {}
11
+ end
12
+
13
+ it 'should have mock equal false' do
14
+ ApiClient.config.mock.should be_false
15
+ end
16
+ end
17
+ end
@@ -1,84 +1,154 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ApiClient::ClassMethods do
4
- before :each do
5
- stub_request(:any, 'http://api.example.com/users').to_return(:body => {'a' => 'b'}.to_json)
6
- stub_request(:any, 'http://api.example.com/users/1').to_return(:body => {'a' => 'b'}.to_json)
7
- end
4
+ context 'with mock equal false' do
5
+ before :each do
6
+ stub_request(:any, 'http://api.example.com/users').to_return(:body => {'a' => 'b'}.to_json)
7
+ stub_request(:any, 'http://api.example.com/users/1').to_return(:body => {'a' => 'b'}.to_json)
8
+ end
9
+
10
+ context '.build' do
11
+ context 'with a root node' do
12
+ it 'should return an user' do
13
+ User.build('user' => { 'a' => 'b'}).should be_an_instance_of(User)
14
+ end
15
+
16
+ it 'should set the response' do
17
+ User.build('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
18
+ end
19
+ end
20
+
21
+ context 'without a root node' do
22
+ it 'should return an user' do
23
+ User.build('a' => 'b').should be_an_instance_of(User)
24
+ end
25
+
26
+ it 'should set the response' do
27
+ User.build('a' => 'b').response.should == { 'a' => 'b' }
28
+ end
29
+ end
30
+ end
8
31
 
9
- context '.build' do
10
- context 'with a root node' do
32
+ context '.get' do
11
33
  it 'should return an user' do
12
- User.build('user' => { 'a' => 'b'}).should be_an_instance_of(User)
34
+ User.get(1).should be_an_instance_of(User)
13
35
  end
36
+ end
14
37
 
15
- it 'should set the response' do
16
- User.build('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
38
+ context '.find' do
39
+ it 'should return an user' do
40
+ User.find(1).should be_an_instance_of(User)
17
41
  end
18
42
  end
19
43
 
20
- context 'without a root node' do
44
+ context '.post' do
21
45
  it 'should return an user' do
22
- User.build('a' => 'b').should be_an_instance_of(User)
46
+ User.post({}).should be_an_instance_of(User)
23
47
  end
48
+ end
24
49
 
25
- it 'should set the response' do
26
- User.build('a' => 'b').response.should == { 'a' => 'b' }
50
+ context '.create' do
51
+ it 'should return an user' do
52
+ User.create({}).should be_an_instance_of(User)
27
53
  end
28
54
  end
29
- end
30
55
 
31
- context '.get' do
32
- it 'should return an user' do
33
- User.get(1).should be_an_instance_of(User)
56
+ context '.put' do
57
+ it 'should return an user' do
58
+ User.put(1, {}).should be_an_instance_of(User)
59
+ end
34
60
  end
35
- end
36
61
 
37
- context '.find' do
38
- it 'should return an user' do
39
- User.find(1).should be_an_instance_of(User)
62
+ context '.update_attributes' do
63
+ it 'should return an user' do
64
+ User.update_attributes(1, {}).should be_an_instance_of(User)
65
+ end
40
66
  end
41
- end
42
67
 
43
- context '.post' do
44
- it 'should return an user' do
45
- User.post({}).should be_an_instance_of(User)
68
+ context '.patch' do
69
+ it 'should return an user' do
70
+ User.patch(1, {}).should be_an_instance_of(User)
71
+ end
46
72
  end
47
- end
48
73
 
49
- context '.create' do
50
- it 'should return an user' do
51
- User.create({}).should be_an_instance_of(User)
74
+ context '.delete' do
75
+ it 'should return an user' do
76
+ User.delete(1).should be_an_instance_of(User)
77
+ end
52
78
  end
53
- end
54
79
 
55
- context '.put' do
56
- it 'should return an user' do
57
- User.put(1, {}).should be_an_instance_of(User)
80
+ context '.destroy' do
81
+ it 'should return an user' do
82
+ User.destroy(1).should be_an_instance_of(User)
83
+ end
58
84
  end
59
85
  end
60
86
 
61
- context '.update_attributes' do
62
- it 'should return an user' do
63
- User.update_attributes(1, {}).should be_an_instance_of(User)
87
+ context 'with mock equal true' do
88
+ before :each do
89
+ ApiClient.configure do |config|
90
+ config.mock = true
91
+ end
64
92
  end
65
- end
66
93
 
67
- context '.patch' do
68
- it 'should return an user' do
69
- User.patch(1, {}).should be_an_instance_of(User)
94
+ after :each do
95
+ ApiClient.configure do |config|
96
+ config.mock = false
97
+ end
70
98
  end
71
- end
72
99
 
73
- context '.delete' do
74
- it 'should return an user' do
75
- User.delete(1).should be_an_instance_of(User)
100
+ context '.get' do
101
+ it 'should return an user' do
102
+ User.get(1).should be_an_instance_of(User)
103
+ end
104
+ end
105
+
106
+ context '.find' do
107
+ it 'should return an user' do
108
+ User.find(1).should be_an_instance_of(User)
109
+ end
110
+ end
111
+
112
+ context '.post' do
113
+ it 'should return an user' do
114
+ User.post({}).should be_an_instance_of(User)
115
+ end
116
+ end
117
+
118
+ context '.create' do
119
+ it 'should return an user' do
120
+ User.create({}).should be_an_instance_of(User)
121
+ end
122
+ end
123
+
124
+ context '.put' do
125
+ it 'should return an user' do
126
+ User.put({}).should be_an_instance_of(User)
127
+ end
128
+ end
129
+
130
+ context '.update_attributes' do
131
+ it 'should return an user' do
132
+ User.update_attributes({}).should be_an_instance_of(User)
133
+ end
134
+ end
135
+
136
+ context '.patch' do
137
+ it 'should return an user' do
138
+ User.patch({}).should be_an_instance_of(User)
139
+ end
140
+ end
141
+
142
+ context '.delete' do
143
+ it 'should return an user' do
144
+ User.delete(1).should be_an_instance_of(User)
145
+ end
76
146
  end
77
- end
78
147
 
79
- context '.destroy' do
80
- it 'should return an user' do
81
- User.destroy(1).should be_an_instance_of(User)
148
+ context '.destroy' do
149
+ it 'should return an user' do
150
+ User.destroy(1).should be_an_instance_of(User)
151
+ end
82
152
  end
83
153
  end
84
154
  end
@@ -2,85 +2,155 @@ require 'spec_helper'
2
2
 
3
3
  describe ApiClient::InstanceMethods do
4
4
  let(:user) { User.new(:id => 1) }
5
-
6
- before :each do
7
- stub_request(:any, 'http://api.example.com/users').to_return(:body => {'a' => 'b'}.to_json)
8
- stub_request(:any, 'http://api.example.com/users/1').to_return(:body => {'a' => 'b'}.to_json)
9
- end
10
5
 
11
- context '.update' do
12
- context 'with a root node' do
13
- it 'should update the object' do
14
- user.update('user' => { 'a' => 'b'}).should be_an_instance_of(User)
6
+ context 'with mock equal false' do
7
+ before :each do
8
+ stub_request(:any, 'http://api.example.com/users').to_return(:body => {'a' => 'b'}.to_json)
9
+ stub_request(:any, 'http://api.example.com/users/1').to_return(:body => {'a' => 'b'}.to_json)
10
+ end
11
+
12
+ context '.update' do
13
+ context 'with a root node' do
14
+ it 'should update the object' do
15
+ user.update('user' => { 'a' => 'b'}).should be_an_instance_of(User)
16
+ end
17
+
18
+ it 'should set the response' do
19
+ user.update('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
20
+ end
15
21
  end
16
22
 
17
- it 'should set the response' do
18
- user.update('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
23
+ context 'without a root node' do
24
+ it 'should update the object' do
25
+ user.update('a' => 'b').should be_an_instance_of(User)
26
+ end
27
+
28
+ it 'should set the response' do
29
+ user.update('a' => 'b').response.should == { 'a' => 'b' }
30
+ end
19
31
  end
20
32
  end
21
33
 
22
- context 'without a root node' do
23
- it 'should update the object' do
24
- user.update('a' => 'b').should be_an_instance_of(User)
34
+ context '.get' do
35
+ it 'should return an user' do
36
+ user.get.should be_an_instance_of(User)
25
37
  end
38
+ end
26
39
 
27
- it 'should set the response' do
28
- user.update('a' => 'b').response.should == { 'a' => 'b' }
40
+ context '.reload' do
41
+ it 'should return an user' do
42
+ user.reload.should be_an_instance_of(User)
29
43
  end
30
44
  end
31
- end
32
45
 
33
- context '.get' do
34
- it 'should return an user' do
35
- user.get.should be_an_instance_of(User)
46
+ context '.post' do
47
+ it 'should return an user' do
48
+ user.post.should be_an_instance_of(User)
49
+ end
36
50
  end
37
- end
38
51
 
39
- context '.reload' do
40
- it 'should return an user' do
41
- user.reload.should be_an_instance_of(User)
52
+ context '.create' do
53
+ it 'should return an user' do
54
+ user.create.should be_an_instance_of(User)
55
+ end
42
56
  end
43
- end
44
57
 
45
- context '.post' do
46
- it 'should return an user' do
47
- user.post.should be_an_instance_of(User)
58
+ context '.put' do
59
+ it 'should return an user' do
60
+ user.put.should be_an_instance_of(User)
61
+ end
48
62
  end
49
- end
50
63
 
51
- context '.create' do
52
- it 'should return an user' do
53
- user.create.should be_an_instance_of(User)
64
+ context '.update_attributes' do
65
+ it 'should return an user' do
66
+ user.update_attributes.should be_an_instance_of(User)
67
+ end
54
68
  end
55
- end
56
69
 
57
- context '.put' do
58
- it 'should return an user' do
59
- user.put.should be_an_instance_of(User)
70
+ context '.patch' do
71
+ it 'should return an user' do
72
+ user.patch.should be_an_instance_of(User)
73
+ end
60
74
  end
61
- end
62
75
 
63
- context '.update_attributes' do
64
- it 'should return an user' do
65
- user.update_attributes.should be_an_instance_of(User)
76
+ context '.delete' do
77
+ it 'should return an user' do
78
+ user.delete.should be_an_instance_of(User)
79
+ end
66
80
  end
67
- end
68
81
 
69
- context '.patch' do
70
- it 'should return an user' do
71
- user.patch.should be_an_instance_of(User)
82
+ context '.destroy' do
83
+ it 'should return an user' do
84
+ user.destroy.should be_an_instance_of(User)
85
+ end
72
86
  end
73
87
  end
74
88
 
75
- context '.delete' do
76
- it 'should return an user' do
77
- user.delete.should be_an_instance_of(User)
89
+ context 'with mock equal true' do
90
+ before :each do
91
+ ApiClient.configure do |config|
92
+ config.mock = true
93
+ end
94
+ end
95
+
96
+ after :each do
97
+ ApiClient.configure do |config|
98
+ config.mock = false
99
+ end
78
100
  end
79
- end
80
101
 
81
- context '.destroy' do
82
- it 'should return an user' do
83
- user.destroy.should be_an_instance_of(User)
102
+ context '.get' do
103
+ it 'should return an user' do
104
+ user.get.should be_an_instance_of(User)
105
+ end
106
+ end
107
+
108
+ context '.reload' do
109
+ it 'should return an user' do
110
+ user.reload.should be_an_instance_of(User)
111
+ end
112
+ end
113
+
114
+ context '.post' do
115
+ it 'should return an user' do
116
+ user.post.should be_an_instance_of(User)
117
+ end
118
+ end
119
+
120
+ context '.create' do
121
+ it 'should return an user' do
122
+ user.create.should be_an_instance_of(User)
123
+ end
124
+ end
125
+
126
+ context '.put' do
127
+ it 'should return an user' do
128
+ user.put.should be_an_instance_of(User)
129
+ end
130
+ end
131
+
132
+ context '.update_attributes' do
133
+ it 'should return an user' do
134
+ user.update_attributes.should be_an_instance_of(User)
135
+ end
136
+ end
137
+
138
+ context '.patch' do
139
+ it 'should return an user' do
140
+ user.patch.should be_an_instance_of(User)
141
+ end
142
+ end
143
+
144
+ context '.delete' do
145
+ it 'should return an user' do
146
+ user.delete.should be_an_instance_of(User)
147
+ end
148
+ end
149
+
150
+ context '.destroy' do
151
+ it 'should return an user' do
152
+ user.destroy.should be_an_instance_of(User)
153
+ end
84
154
  end
85
155
  end
86
156
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-23 00:00:00.000000000 Z
12
+ date: 2013-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -174,6 +174,7 @@ files:
174
174
  - lib/api-client/net/http.rb
175
175
  - lib/api-client/parser.rb
176
176
  - lib/api-client/version.rb
177
+ - spec/api-client.rb
177
178
  - spec/api-client/base_spec.rb
178
179
  - spec/api-client/class_methods_spec.rb
179
180
  - spec/api-client/collection_spec.rb
@@ -197,7 +198,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
198
  version: '0'
198
199
  segments:
199
200
  - 0
200
- hash: 3470123561334596428
201
+ hash: -2067654543697834646
201
202
  required_rubygems_version: !ruby/object:Gem::Requirement
202
203
  none: false
203
204
  requirements:
@@ -206,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
207
  version: '0'
207
208
  segments:
208
209
  - 0
209
- hash: 3470123561334596428
210
+ hash: -2067654543697834646
210
211
  requirements: []
211
212
  rubyforge_project:
212
213
  rubygems_version: 1.8.25
@@ -225,6 +226,7 @@ test_files:
225
226
  - examples/scripts/example.rb
226
227
  - gemfiles/Gemfile.net_http
227
228
  - gemfiles/Gemfile.typhoeus
229
+ - spec/api-client.rb
228
230
  - spec/api-client/base_spec.rb
229
231
  - spec/api-client/class_methods_spec.rb
230
232
  - spec/api-client/collection_spec.rb