api-client 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -41,6 +41,19 @@ Or install it yourself as:
41
41
 
42
42
  ## Basic Usage
43
43
 
44
+ Create an initializer:
45
+
46
+ ```ruby
47
+ ApiClient.configure do |config|
48
+ # Api path
49
+ config.path = 'http://api.example.com'
50
+ # Default header
51
+ config.header = { 'param1' => '123329845729384759237592348712876817234'}
52
+ # Basic Auth
53
+ config.basic_auth('user', 'pass')
54
+ end
55
+ ```
56
+
44
57
  Add this to your ApplicationController:
45
58
 
46
59
  ```ruby
@@ -109,12 +122,15 @@ This code will create a setter and a getter for houses and cars and initialize t
109
122
  Since version 2.0.0, it is possible to make api calls from the object. The syntax is pretty much the same.
110
123
  It will make the call and update the fields with the response. Look at the examples folder to see more code examples
111
124
 
125
+ ## More Examples
126
+ [Project](https://github.com/zertico/api-client/tree/master/examples)
127
+
112
128
  ## TODO
113
- * Add support for parallel requests
114
- * Add more Response Handlers
129
+ * Add support for parallel requests
130
+ * Add more Response Handlers
115
131
 
116
132
  ## Mantainers
117
- [@plribeiro3000](https://github.com/plribeiro3000)
133
+ [@plribeiro3000](https://github.com/plribeiro3000)
118
134
 
119
135
 
120
136
  ## Contributing
@@ -123,4 +139,4 @@ It will make the call and update the fields with the response. Look at the examp
123
139
  2. Create your feature branch (`git checkout -b my-new-feature`)
124
140
  3. Commit your changes (`git commit -am 'Added some feature'`)
125
141
  4. Push to the branch (`git push origin my-new-feature`)
126
- 5. Create new Pull Request
142
+ 5. Create new Pull Request
@@ -3,5 +3,7 @@ ApiClient.configure do |config|
3
3
  # Api path
4
4
  config.path = 'http://api.example.com'
5
5
  # Default header
6
- config.header = { :token => '123329845729384759237592348712876817234'}
6
+ config.header = { 'param' => '123329845729384759237592348712876817234'}
7
+ # Basic Authentication
8
+ config.basic_auth('user', 'pass')
7
9
  end
@@ -1,3 +1,5 @@
1
1
  source :rubygems
2
2
 
3
+ gem 'activesupport', '>= 3.0', '< 4'
4
+
3
5
  gemspec :path => '../'
@@ -2,4 +2,6 @@ source :rubygems
2
2
 
3
3
  gem 'typhoeus'
4
4
 
5
+ gem 'activesupport', '>= 3.0', '< 4'
6
+
5
7
  gemspec :path => '../'
data/lib/api-client.rb CHANGED
@@ -25,8 +25,9 @@ module ApiClient
25
25
  @config
26
26
  end
27
27
 
28
+ # Default Settings
28
29
  configure do |config|
29
30
  config.path = ''
30
- config.header = { 'Content-Type' => 'application/json' }
31
+ config.header = {}
31
32
  end
32
33
  end
@@ -132,6 +132,10 @@ module ApiClient
132
132
  ApiClient::Collection.new(self, self.resource_path).collection
133
133
  end
134
134
 
135
+ class << self
136
+ alias_method :all, :collection
137
+ end
138
+
135
139
  # Set the hash of errors, making keys symbolic.
136
140
  #
137
141
  # @param [Hash] errs errors of the object.
@@ -23,6 +23,8 @@ module ApiClient
23
23
  build(params)
24
24
  end
25
25
 
26
+ alias_method :find, :get
27
+
26
28
  # Make a post requisition and initialize an object with the response.
27
29
  #
28
30
  # @param [Hash] attributes hash with the attributes to send.
@@ -35,6 +37,8 @@ module ApiClient
35
37
  build(params)
36
38
  end
37
39
 
40
+ alias_method :create, :post
41
+
38
42
  # Make a put requisition and initialize an object with the response.
39
43
  #
40
44
  # @param [Integer] id id of the object.
@@ -48,6 +52,8 @@ module ApiClient
48
52
  build(params)
49
53
  end
50
54
 
55
+ alias_method :update_attributes, :put
56
+
51
57
  # Make a patch requisition and initialize an object with the response.
52
58
  #
53
59
  # @param [Integer] id id of the object.
@@ -73,6 +79,8 @@ module ApiClient
73
79
  build(params)
74
80
  end
75
81
 
82
+ alias_method :destroy, :delete
83
+
76
84
  # Removes the root node attribute if found.
77
85
  #
78
86
  # @param [Hash] attributes the hash with attributes.
@@ -1,6 +1,8 @@
1
1
  module ApiClient
2
2
  # ApiClient::Configuration provides a way to configure ApiClient globally.
3
3
  class Configuration
4
+ attr_reader :header
5
+
4
6
  # Return the api url.
5
7
  #
6
8
  # @return [String] the api url.
@@ -17,19 +19,18 @@ module ApiClient
17
19
  @path = path
18
20
  end
19
21
 
20
- # Return the default header for requisitions.
22
+ # Set the default params of header.
21
23
  #
22
- # @return [Hash] the default header.
23
- def header
24
- return { 'Content-Type' => 'application/json' } unless @header
25
- @header
24
+ # @param [Hash] header the default header for requisitions.
25
+ def header=(header = {})
26
+ @header = { 'Content-Type' => 'application/json' }.merge(header)
26
27
  end
27
28
 
28
- # Set the default header for requisitions.
29
+ # Set a basic authentication for all requisitions.
29
30
  #
30
- # @param [Hash] header the default header for requitions.
31
- def header=(header = {})
32
- @header = { 'Content-Type' => 'application/json' }.merge(header)
31
+ # @param [Hash] header the default header for requisitions.
32
+ def basic_auth(account, password)
33
+ @header.merge!({ 'Authorization' => "Basic #{["#{account}:#{password}"].pack('m').delete("\r\n")}" })
33
34
  end
34
35
  end
35
36
  end
@@ -5,7 +5,7 @@ module ApiClient
5
5
  #
6
6
  # @param [Hash] attributes hash of attributes.
7
7
  # @return [Base] the update_attributes object.
8
- def update_attributes(attributes)
8
+ def update(attributes)
9
9
  hash = remove_root(attributes)
10
10
  hash = hash.merge({ 'response' => attributes })
11
11
  hash.each do |key, value|
@@ -22,9 +22,11 @@ module ApiClient
22
22
  url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
23
23
  response = ApiClient::Dispatcher.get(url, header)
24
24
  attributes = ApiClient::Parser.response(response, url)
25
- update_attributes(attributes)
25
+ update(attributes)
26
26
  end
27
27
 
28
+ alias_method :reload, :get
29
+
28
30
  # Make a post requisition and update the object with the response.
29
31
  #
30
32
  # @param [Hash] header hash with the header options.
@@ -33,9 +35,11 @@ module ApiClient
33
35
  url = "#{ApiClient.config.path}#{self.class.resource_path}"
34
36
  response = ApiClient::Dispatcher.post(url, self.to_hash, header)
35
37
  attributes = ApiClient::Parser.response(response, url)
36
- update_attributes(attributes)
38
+ update(attributes)
37
39
  end
38
40
 
41
+ alias_method :create, :post
42
+
39
43
  # Make a put requisition and update the object with the response.
40
44
  #
41
45
  # @param [Hash] header hash with the header options.
@@ -44,9 +48,11 @@ module ApiClient
44
48
  url = "#{ApiClient.config.path}#{self.class.resource_path}"
45
49
  response = ApiClient::Dispatcher.put(url, self.to_hash, header)
46
50
  attributes = ApiClient::Parser.response(response, url)
47
- update_attributes(attributes)
51
+ update(attributes)
48
52
  end
49
53
 
54
+ alias_method :update_attributes, :put
55
+
50
56
  # Make a patch requisition and update the object with the response.
51
57
  #
52
58
  # @param [Hash] header hash with the header options.
@@ -55,7 +61,7 @@ module ApiClient
55
61
  url = "#{ApiClient.config.path}#{self.class.resource_path}"
56
62
  response = ApiClient::Dispatcher.patch(url, self.to_hash, header)
57
63
  attributes = ApiClient::Parser.response(response, url)
58
- update_attributes(attributes)
64
+ update(attributes)
59
65
  end
60
66
 
61
67
  # Make a delete requisition and update the object with the response.
@@ -66,9 +72,11 @@ module ApiClient
66
72
  url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
67
73
  response = ApiClient::Dispatcher.delete(url, header)
68
74
  attributes = ApiClient::Parser.response(response, url)
69
- update_attributes(attributes)
75
+ update(attributes)
70
76
  end
71
77
 
78
+ alias_method :destroy, :delete
79
+
72
80
  # Removes the root node attribute if found.
73
81
  #
74
82
  # @param [Hash] attributes the hash with attributes.
@@ -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.1.0'
4
+ VERSION = '2.2.0'
5
5
  end
@@ -8,7 +8,7 @@ describe ApiClient::ClassMethods do
8
8
 
9
9
  context '.build' do
10
10
  context 'with a root node' do
11
- it 'should return a new object' do
11
+ it 'should return an user' do
12
12
  User.build('user' => { 'a' => 'b'}).should be_an_instance_of(User)
13
13
  end
14
14
 
@@ -18,7 +18,7 @@ describe ApiClient::ClassMethods do
18
18
  end
19
19
 
20
20
  context 'without a root node' do
21
- it 'should return a new object' do
21
+ it 'should return an user' do
22
22
  User.build('a' => 'b').should be_an_instance_of(User)
23
23
  end
24
24
 
@@ -29,32 +29,56 @@ describe ApiClient::ClassMethods do
29
29
  end
30
30
 
31
31
  context '.get' do
32
- it 'should return a new object' do
32
+ it 'should return an user' do
33
33
  User.get(1).should be_an_instance_of(User)
34
34
  end
35
35
  end
36
36
 
37
+ context '.find' do
38
+ it 'should return an user' do
39
+ User.find(1).should be_an_instance_of(User)
40
+ end
41
+ end
42
+
37
43
  context '.post' do
38
- it 'should return a new object' do
44
+ it 'should return an user' do
39
45
  User.post({}).should be_an_instance_of(User)
40
46
  end
41
47
  end
42
48
 
49
+ context '.create' do
50
+ it 'should return an user' do
51
+ User.create({}).should be_an_instance_of(User)
52
+ end
53
+ end
54
+
43
55
  context '.put' do
44
- it 'should return a new object' do
56
+ it 'should return an user' do
45
57
  User.put(1, {}).should be_an_instance_of(User)
46
58
  end
47
59
  end
48
60
 
61
+ context '.update_attributes' do
62
+ it 'should return an user' do
63
+ User.update_attributes(1, {}).should be_an_instance_of(User)
64
+ end
65
+ end
66
+
49
67
  context '.patch' do
50
- it 'should return a new object' do
68
+ it 'should return an user' do
51
69
  User.patch(1, {}).should be_an_instance_of(User)
52
70
  end
53
71
  end
54
72
 
55
73
  context '.delete' do
56
- it 'should return a new object' do
74
+ it 'should return an user' do
57
75
  User.delete(1).should be_an_instance_of(User)
58
76
  end
59
77
  end
78
+
79
+ context '.destroy' do
80
+ it 'should return an user' do
81
+ User.destroy(1).should be_an_instance_of(User)
82
+ end
83
+ end
60
84
  end
@@ -78,4 +78,23 @@ describe ApiClient::Configuration do
78
78
  ApiClient.config.header.should == { 'Content-Type' => 'application/xml' }
79
79
  end
80
80
  end
81
+
82
+ describe '#basic_auth=' do
83
+ before :each do
84
+ ApiClient.configure do |config|
85
+ config.basic_auth('user', 'pass')
86
+ end
87
+ end
88
+
89
+ after :each do
90
+ ApiClient.configure do |config|
91
+ config.header = {}
92
+ end
93
+ end
94
+
95
+ it 'should merge basic_auth in header params' do
96
+ ApiClient.config.header.should == { 'Content-Type' => 'application/xml',
97
+ 'Authorization' => 'Basic dXNlcjpwYXNz' }
98
+ end
99
+ end
81
100
  end
@@ -8,55 +8,79 @@ describe ApiClient::InstanceMethods do
8
8
  stub_request(:any, 'http://api.example.com/users/1').to_return(:body => {'a' => 'b'}.to_json)
9
9
  end
10
10
 
11
- context '.update_attributes' do
11
+ context '.update' do
12
12
  context 'with a root node' do
13
13
  it 'should update the object' do
14
- user.update_attributes('user' => { 'a' => 'b'}).should be_an_instance_of(User)
14
+ user.update('user' => { 'a' => 'b'}).should be_an_instance_of(User)
15
15
  end
16
16
 
17
17
  it 'should set the response' do
18
- user.update_attributes('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
18
+ user.update('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
19
19
  end
20
20
  end
21
21
 
22
22
  context 'without a root node' do
23
23
  it 'should update the object' do
24
- user.update_attributes('a' => 'b').should be_an_instance_of(User)
24
+ user.update('a' => 'b').should be_an_instance_of(User)
25
25
  end
26
26
 
27
27
  it 'should set the response' do
28
- user.update_attributes('a' => 'b').response.should == { 'a' => 'b' }
28
+ user.update('a' => 'b').response.should == { 'a' => 'b' }
29
29
  end
30
30
  end
31
31
  end
32
32
 
33
33
  context '.get' do
34
- it 'should return a new object' do
34
+ it 'should return an user' do
35
35
  user.get.should be_an_instance_of(User)
36
36
  end
37
37
  end
38
38
 
39
+ context '.reload' do
40
+ it 'should return an user' do
41
+ user.reload.should be_an_instance_of(User)
42
+ end
43
+ end
44
+
39
45
  context '.post' do
40
- it 'should return a new object' do
46
+ it 'should return an user' do
41
47
  user.post.should be_an_instance_of(User)
42
48
  end
43
49
  end
44
50
 
51
+ context '.create' do
52
+ it 'should return an user' do
53
+ user.create.should be_an_instance_of(User)
54
+ end
55
+ end
56
+
45
57
  context '.put' do
46
- it 'should return a new object' do
58
+ it 'should return an user' do
47
59
  user.put.should be_an_instance_of(User)
48
60
  end
49
61
  end
50
62
 
63
+ context '.update_attributes' do
64
+ it 'should return an user' do
65
+ user.update_attributes.should be_an_instance_of(User)
66
+ end
67
+ end
68
+
51
69
  context '.patch' do
52
- it 'should return a new object' do
70
+ it 'should return an user' do
53
71
  user.patch.should be_an_instance_of(User)
54
72
  end
55
73
  end
56
74
 
57
75
  context '.delete' do
58
- it 'should return a new object' do
76
+ it 'should return an user' do
59
77
  user.delete.should be_an_instance_of(User)
60
78
  end
61
79
  end
80
+
81
+ context '.destroy' do
82
+ it 'should return an user' do
83
+ user.destroy.should be_an_instance_of(User)
84
+ end
85
+ end
62
86
  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.1.0
4
+ version: 2.2.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-17 00:00:00.000000000 Z
12
+ date: 2013-07-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -197,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
197
  version: '0'
198
198
  segments:
199
199
  - 0
200
- hash: 3129970379733886747
200
+ hash: 3470123561334596428
201
201
  required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  none: false
203
203
  requirements:
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  version: '0'
207
207
  segments:
208
208
  - 0
209
- hash: 3129970379733886747
209
+ hash: 3470123561334596428
210
210
  requirements: []
211
211
  rubyforge_project:
212
212
  rubygems_version: 1.8.25