api-client 2.0.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,34 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## v2.0.3
4
+
5
+ * Fix put and patch requisitions.
6
+
7
+ ## v2.0.2
8
+
9
+ * Changed to_hash method behavior to return a root node.
10
+ * Fix instance requisitions by send a root node.
11
+
12
+ ## v2.0.1
13
+
14
+ * Removed trailing slash from post requisitions.
15
+
16
+ ## v2.0.0
17
+
18
+ * Fix typhoeus requisitions by changing params method to body.
19
+ * Add Support to configure header globally.
20
+ * Improved readability by changing methods names.
21
+
22
+ ## v2.0.0.rc2
23
+
24
+ * Fix initialization with root nodes for nested objects.
25
+ * Add Support for object initialization with strings as keys.
26
+
3
27
  ## v2.0.0.rc1
4
28
 
5
29
  * Add Support for global url.
6
30
  * Add Support for instance requests.
7
- * Changed behavior of request methods to only accepts the resource id if needed..
31
+ * Changed behavior of request methods to only accepts the resource id if needed.
8
32
 
9
33
  ## v1.10.0
10
34
 
data/README.md CHANGED
@@ -84,7 +84,15 @@ In Some cases, that is not the required behavior. To Redefine it, use remote_obj
84
84
 
85
85
  ```ruby
86
86
  class Admin < ApiClient::Base
87
- self.remote_object = "user"
87
+ self.root_node = 'user'
88
+ end
89
+ ```
90
+
91
+ To specify a resource path different from the expected, you can overwrite the behavior by setting resouce_path:
92
+
93
+ ```ruby
94
+ class Admin < ApiClient::Base
95
+ self.resource_path = 'users?type=admin'
88
96
  end
89
97
  ```
90
98
 
@@ -98,6 +106,9 @@ end
98
106
 
99
107
  This code will create a setter and a getter for houses and cars and initialize the respective class.
100
108
 
109
+ Since version 2.0.0, it is possible to make api calls from the object. The syntax is pretty much the same.
110
+ It will make the call and update the fields with the response. Look at the examples folder to see more code examples
111
+
101
112
  ## TODO
102
113
  * Add support for parallel requests
103
114
  * Add more Response Handlers
@@ -112,4 +123,4 @@ This code will create a setter and a getter for houses and cars and initialize t
112
123
  2. Create your feature branch (`git checkout -b my-new-feature`)
113
124
  3. Commit your changes (`git commit -am 'Added some feature'`)
114
125
  4. Push to the branch (`git push origin my-new-feature`)
115
- 5. Create new Pull Request
126
+ 5. Create new Pull Request
@@ -1,5 +1,7 @@
1
1
  # Global Configuration
2
- # Api path
3
2
  ApiClient.configure do |config|
3
+ # Api path
4
4
  config.path = 'http://api.example.com'
5
+ # Default header
6
+ config.header = { :token => '123329845729384759237592348712876817234'}
5
7
  end
@@ -15,7 +15,7 @@ class UserController < ApplicationController
15
15
 
16
16
  # It will hit http://api.example.com/users with a post request
17
17
  def create
18
- @user = User.post('', :user => params[:user])
18
+ @user = User.post(:user => params[:user])
19
19
  respond_with(@user)
20
20
  end
21
21
 
@@ -1,9 +1,9 @@
1
1
  class Admin < ApiClient::Base
2
- self.remote_object = 'user'
2
+ self.resource_path = 'users?type=admin'
3
3
  self.association = { :groups => 'Group' }
4
4
 
5
5
  # Any of this fields can be called to manage rails form.
6
- attr_accessor :id, :email, :password, :password_confirmation
6
+ attr_accessor :email, :password, :password_confirmation
7
7
 
8
8
  # Validations will work as well
9
9
  validates :email, :presence => true, :uniqueness => true
@@ -0,0 +1,3 @@
1
+ class Author < ApiClient::Base
2
+ attr_accessor :name
3
+ end
@@ -0,0 +1,5 @@
1
+ class Book < ApiClient::Base
2
+ self.association = { :author => 'Author' }
3
+
4
+ attr_accessor :name, :author_id, :publish_date
5
+ end
@@ -1,3 +1,4 @@
1
1
  class Group < ApiClient::Base
2
2
  self.associations = { :members => 'User', :owner => 'Admin' }
3
+ self.root_node = 'people'
3
4
  end
@@ -1,8 +1,8 @@
1
1
  class User < ApiClient::Base
2
- self.association = { :groups => 'Group' }
2
+ self.associations = { :groups => 'Group', :books => 'Book' }
3
3
 
4
4
  # Any of this fields can be called to manage rails form.
5
- attr_accessor :id, :email, :password, :password_confirmation
5
+ attr_accessor :email, :password, :password_confirmation, :type
6
6
 
7
7
  # Validations will work as well
8
8
  validates :email, :presence => true, :uniqueness => true
@@ -0,0 +1,42 @@
1
+ require 'api-client'
2
+
3
+ # Make a get requisition to http://api.example.com/users and initialize an user object for each user returned
4
+ users = User.collection
5
+
6
+ # Get the first user
7
+ user = users.first
8
+
9
+ # Set the attribute type to 'admin'
10
+ user.type = 'admin'
11
+
12
+ # Make a put requisition to http://api.example.com/users/:id to update the user attributes
13
+ user.put # or user.patch
14
+
15
+ # Iterate over the books of the given user
16
+ user.books.each do |book|
17
+ # Make a get requisition to http://api.example.com/books/:id to update the given book attributes
18
+ book.get
19
+ # Make a delete requisition to http://api.example.com/books/:id destroy the object if the publish_date is equal '1990'
20
+ book.delete if book.publish_date == '1990'
21
+ end
22
+
23
+ # Make a get requisition to http://api.example.com/authors and initialize an author object for each author returned
24
+ authors = Author.collection
25
+
26
+ # Iterate over the authors
27
+ authors.each do |author|
28
+ # Set @author to the current author if it is the one
29
+ @author = author if author.name == 'Author Name'
30
+ end
31
+
32
+ # Initialize a new object
33
+ book = Book.new
34
+
35
+ # Set the name on the book object
36
+ book.name = 'book'
37
+ # Set the author_id of the book object with the chosen author id
38
+ book.author_id = @author.id
39
+ # Set the publish date of the book object
40
+ book.publish_date = '2002'
41
+ # Make a post requisition to http://api.example.com/books to create the book with the object attributes
42
+ book.post
@@ -27,5 +27,6 @@ module ApiClient
27
27
 
28
28
  configure do |config|
29
29
  config.path = ''
30
+ config.header = { 'Content-Type' => 'application/json' }
30
31
  end
31
32
  end
@@ -41,8 +41,8 @@ module ApiClient
41
41
  # @param [Hash] attributes hash with the attributes to send.
42
42
  # @param [Hash] header hash with the header options.
43
43
  # @return [Base] the object initialized.
44
- def put(id, attributes, header = {})
45
- url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
44
+ def put(attributes, header = {})
45
+ url = "#{ApiClient.config.path}#{self.resource_path}"
46
46
  response = ApiClient::Dispatcher.put(url, attributes, header)
47
47
  params = ApiClient::Parser.response(response, url)
48
48
  build(params)
@@ -54,8 +54,8 @@ module ApiClient
54
54
  # @param [Hash] attributes hash with the attributes to send.
55
55
  # @param [Hash] header hash with the header options.
56
56
  # @return [Base] the object initialized.
57
- def patch(id, attributes, header = {})
58
- url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
57
+ def patch(attributes, header = {})
58
+ url = "#{ApiClient.config.path}#{self.resource_path}"
59
59
  response = ApiClient::Dispatcher.patch(url, attributes, header)
60
60
  params = ApiClient::Parser.response(response, url)
61
61
  build(params)
@@ -73,6 +73,10 @@ module ApiClient
73
73
  build(params)
74
74
  end
75
75
 
76
+ # Removes the root node attribute if found.
77
+ #
78
+ # @param [Hash] attributes the hash with attributes.
79
+ # @return [Hash] the hash with attributes without the root node.
76
80
  def remove_root(attributes = {})
77
81
  attributes = attributes[self.root_node.to_sym] if attributes.key?(self.root_node.to_sym)
78
82
  attributes = attributes[self.root_node.to_s] if attributes.key?(self.root_node.to_s)
@@ -8,7 +8,7 @@ module ApiClient::Dispatcher::Typhoeus
8
8
  # @param [Hash] header attributes of the request.
9
9
  # @return [Typhoeus::Request] the response object.
10
10
  def self.get(url, header = {})
11
- ::Typhoeus::Request.get(url, :headers => ApiClient.config.header.merge(header))
11
+ ::Typhoeus.get(url, :headers => ApiClient.config.header.merge(header))
12
12
  end
13
13
 
14
14
  # Make a post request and returns it.
@@ -18,7 +18,7 @@ module ApiClient::Dispatcher::Typhoeus
18
18
  # @param [Hash] header attributes of the request.
19
19
  # @return [Typhoeus::Request] the response object.
20
20
  def self.post(url, args, header = {})
21
- ::Typhoeus::Request.post(url, :body => args, :headers => ApiClient.config.header.merge(header))
21
+ ::Typhoeus.post(url, :body => args, :headers => ApiClient.config.header.merge(header))
22
22
  end
23
23
 
24
24
  # Make a put request and returns it.
@@ -28,7 +28,7 @@ module ApiClient::Dispatcher::Typhoeus
28
28
  # @param [Hash] header attributes of the request.
29
29
  # @return [Typhoeus::Request] the response object.
30
30
  def self.put(url, args, header = {})
31
- ::Typhoeus::Request.put(url, :body => args, :headers => ApiClient.config.header.merge(header))
31
+ ::Typhoeus.put(url, :body => args, :headers => ApiClient.config.header.merge(header))
32
32
  end
33
33
 
34
34
  # Make a patch request and returns it.
@@ -38,7 +38,7 @@ module ApiClient::Dispatcher::Typhoeus
38
38
  # @param [Hash] header attributes of the request.
39
39
  # @return [Typhoeus::Request] the response object.
40
40
  def self.patch(url, args, header = {})
41
- ::Typhoeus::Request.patch(url, :body => args, :headers => ApiClient.config.header.merge(header))
41
+ ::Typhoeus.patch(url, :body => args, :headers => ApiClient.config.header.merge(header))
42
42
  end
43
43
 
44
44
  # Make a delete request and returns it.
@@ -47,6 +47,6 @@ module ApiClient::Dispatcher::Typhoeus
47
47
  # @param [Hash] header attributes of the request.
48
48
  # @return [Typhoeus::Request] the response object.
49
49
  def self.delete(url, header = {})
50
- ::Typhoeus::Request.delete(url, :headers => ApiClient.config.header.merge(header))
50
+ ::Typhoeus.delete(url, :headers => ApiClient.config.header.merge(header))
51
51
  end
52
52
  end
@@ -3,7 +3,7 @@ module ApiClient
3
3
  module InstanceMethods
4
4
  # Update an object based on a hash of attributes.
5
5
  #
6
- # @param [Hash] params hash of attributes.
6
+ # @param [Hash] attributes hash of attributes.
7
7
  # @return [Base] the update_attributes object.
8
8
  def update_attributes(attributes)
9
9
  hash = remove_root(attributes)
@@ -14,6 +14,10 @@ module ApiClient
14
14
  self
15
15
  end
16
16
 
17
+ # Make a get requisition and update the object with the response.
18
+ #
19
+ # @param [Hash] header hash with the header options.
20
+ # @return [Base] the object updated.
17
21
  def get(header = {})
18
22
  url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
19
23
  response = ApiClient::Dispatcher.get(url, header)
@@ -21,6 +25,10 @@ module ApiClient
21
25
  update_attributes(attributes)
22
26
  end
23
27
 
28
+ # Make a post requisition and update the object with the response.
29
+ #
30
+ # @param [Hash] header hash with the header options.
31
+ # @return [Base] the object updated.
24
32
  def post(header = {})
25
33
  url = "#{ApiClient.config.path}#{self.class.resource_path}"
26
34
  response = ApiClient::Dispatcher.post(url, self.to_hash, header)
@@ -28,27 +36,43 @@ module ApiClient
28
36
  update_attributes(attributes)
29
37
  end
30
38
 
39
+ # Make a put requisition and update the object with the response.
40
+ #
41
+ # @param [Hash] header hash with the header options.
42
+ # @return [Base] the object updated.
31
43
  def put(header = {})
32
- url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
44
+ url = "#{ApiClient.config.path}#{self.class.resource_path}"
33
45
  response = ApiClient::Dispatcher.put(url, self.to_hash, header)
34
46
  attributes = ApiClient::Parser.response(response, url)
35
47
  update_attributes(attributes)
36
48
  end
37
49
 
50
+ # Make a patch requisition and update the object with the response.
51
+ #
52
+ # @param [Hash] header hash with the header options.
53
+ # @return [Base] the object updated.
38
54
  def patch(header = {})
39
- url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
40
- response = ApiClient::Dispatcher.post(url, self.to_hash, header)
55
+ url = "#{ApiClient.config.path}#{self.class.resource_path}"
56
+ response = ApiClient::Dispatcher.patch(url, self.to_hash, header)
41
57
  attributes = ApiClient::Parser.response(response, url)
42
58
  update_attributes(attributes)
43
59
  end
44
60
 
61
+ # Make a delete requisition and update the object with the response.
62
+ #
63
+ # @param [Hash] header hash with the header options.
64
+ # @return [Base] the object updated.
45
65
  def delete(header = {})
46
66
  url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
47
- response = ApiClient::Dispatcher.post(url, header)
67
+ response = ApiClient::Dispatcher.delete(url, header)
48
68
  attributes = ApiClient::Parser.response(response, url)
49
69
  update_attributes(attributes)
50
70
  end
51
71
 
72
+ # Removes the root node attribute if found.
73
+ #
74
+ # @param [Hash] attributes the hash with attributes.
75
+ # @return [Hash] the hash with attributes without the root node.
52
76
  def remove_root(attributes = {})
53
77
  attributes = attributes[self.class.root_node.to_sym] if attributes.key?(self.class.root_node.to_sym)
54
78
  attributes = attributes[self.class.root_node.to_s] if attributes.key?(self.class.root_node.to_s)
@@ -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.0.2'
4
+ VERSION = '2.0.3'
5
5
  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.0.2
4
+ version: 2.0.3
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-06-14 00:00:00.000000000 Z
12
+ date: 2013-06-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -144,8 +144,11 @@ files:
144
144
  - examples/controllers/application_controller.rb
145
145
  - examples/controllers/user_controller.rb
146
146
  - examples/models/admin.rb
147
+ - examples/models/author.rb
148
+ - examples/models/book.rb
147
149
  - examples/models/group.rb
148
150
  - examples/models/user.rb
151
+ - examples/scripts/example.rb
149
152
  - gemfiles/Gemfile.net_http
150
153
  - gemfiles/Gemfile.typhoeus
151
154
  - lib/api-client.rb
@@ -194,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
194
197
  version: '0'
195
198
  segments:
196
199
  - 0
197
- hash: 725018755031888977
200
+ hash: -3513012909283666012
198
201
  required_rubygems_version: !ruby/object:Gem::Requirement
199
202
  none: false
200
203
  requirements:
@@ -203,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
206
  version: '0'
204
207
  segments:
205
208
  - 0
206
- hash: 725018755031888977
209
+ hash: -3513012909283666012
207
210
  requirements: []
208
211
  rubyforge_project:
209
212
  rubygems_version: 1.8.25
@@ -215,8 +218,11 @@ test_files:
215
218
  - examples/controllers/application_controller.rb
216
219
  - examples/controllers/user_controller.rb
217
220
  - examples/models/admin.rb
221
+ - examples/models/author.rb
222
+ - examples/models/book.rb
218
223
  - examples/models/group.rb
219
224
  - examples/models/user.rb
225
+ - examples/scripts/example.rb
220
226
  - gemfiles/Gemfile.net_http
221
227
  - gemfiles/Gemfile.typhoeus
222
228
  - spec/api-client/base_spec.rb