rest_api 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # RestApi - RESTful client for everything
2
2
 
3
- **RestApi** aims to be a generic RESTful client for ruby. The goal is to build a friendly API client that you can use right away with minimum configuration. But, if you want, you can bend the client as you wish
3
+ **RestApi** aims to be a generic RESTful client for ruby. The goal is to build a friendly API client that you can use right away with minimum configuration. But, if you want, you can bend the client as you wish.
4
4
 
5
5
  ## Installation
6
6
 
@@ -38,7 +38,7 @@ Just put the above code in /config/initializers/rest_api.rb
38
38
 
39
39
  ## Usage
40
40
 
41
- Every request must be made through the RestApi.request object.
41
+ Every request must be made through the RestApi.request object. And the expected response must be in JSON format. Then it will be parsed to a ruby hash.
42
42
 
43
43
  ```ruby
44
44
  RestApi.request.my_request_method(arguments)
@@ -59,26 +59,26 @@ These are reserved words that the resources cannot use as name:
59
59
  **EXAMPLES**
60
60
 
61
61
  ```ruby
62
- RestApi.request.get_users
63
62
  # GET to "http://www.myapiurl.com/users"
63
+ RestApi.request.get_users
64
64
 
65
- RestApi.request.get_cars_from_users
66
65
  # GET to "http://www.myapiurl.com/users/cars"
66
+ RestApi.request.get_cars_from_users
67
67
 
68
- RestApi.request.get_cars_users
69
68
  # GET to "http://www.myapiurl.com/users/cars"
69
+ RestApi.request.get_cars_users
70
70
 
71
- RestApi.request.post_cars
72
71
  # POST to "http://www.myapiurl.com/cars"
72
+ RestApi.request.post_cars
73
73
 
74
- RestApi.request.put_cars_users
75
74
  # PUT to "http://www.myapiurl.com/users/cars"
75
+ RestApi.request.put_cars_users
76
76
 
77
- RestApi.request.delete_users
78
77
  # DELETE to "http://www.myapiurl.com/users"
78
+ RestApi.request.delete_users
79
79
 
80
- RestApi.request.delete_houses_from_users
81
80
  # DELETE to "http://www.myapiurl.com/users/houses"
81
+ RestApi.request.delete_houses_from_users
82
82
  ```
83
83
 
84
84
  ### arguments
@@ -97,17 +97,17 @@ You can pass a hash in the :resource_params where, for each pair, the key is the
97
97
  **EXAMPLES**
98
98
 
99
99
  ```ruby
100
- RestApi.request.delete_users(:resources_params => { :users => 7})
101
100
  # DELETE to "http://www.myapiurl.com/users/7"
101
+ RestApi.request.delete_users(:resources_params => { :users => 7})
102
102
 
103
- RestApi.request.put_cars_in_users(:resources_params => { :users => 7})
104
103
  # PUT to "http://www.myapiurl.com/users/7/cars/"
104
+ RestApi.request.put_cars_in_users(:resources_params => { :users => 7})
105
105
 
106
- RestApi.request.put_cars_in_users(:resources_params => { :cars => 18})
107
- # PUT to "http://www.myapiurl.com/users/cars/18"
106
+ # PUT to "http://www.myapiurl.com/users/5/cars/18"
107
+ RestApi.request.put_cars_in_users(:resources_params => { :cars => 18, :users => 5})
108
108
 
109
- RestApi.request.get_users(:resources_params => { :users => 18})
110
109
  # GET to "http://www.myapiurl.com/users/18"
110
+ RestApi.request.get_users(:resources_params => { :users => 18})
111
111
  ```
112
112
 
113
113
  **:request_params**
@@ -118,11 +118,11 @@ You can pass a hash in the :request_params where, for each pair, the key is the
118
118
  **EXAMPLE**
119
119
 
120
120
  ```ruby
121
- RestApi.request.post_users(:request_params => { :user => {:name => "myname"})
122
121
  # POST to "http://www.myapiurl.com/users/" with "{ :user => {:name => "name"}" in the header
122
+ RestApi.request.post_users(:request_params => { :user => {:name => "myname"}})
123
123
 
124
- RestApi.request.get_users(:request_params => { :user => {:name => "name"})
125
124
  # GET to "http://www.myapiurl.com/users?name=myname"
125
+ RestApi.request.get_users(:request_params => { :user => {:name => "name"}})
126
126
  ```
127
127
 
128
128
  **Using both :resource_params and :request_params**
@@ -133,11 +133,14 @@ Obviously you can use them together.
133
133
  **EXAMPLES**
134
134
 
135
135
  ```ruby
136
- RestApi.request.get_cars_from_users(:request_params => { :page => 5, :resource_params => { :users => 8})
137
136
  # GET to "http://www.myapiurl.com/users/8/cars?page=5"
137
+ RestApi.request.get_cars_from_users(:request_params => { :page => 5},
138
+ :resources_params => { :users => 8})
138
139
 
139
- RestApi.request.post_cars_in_users(:request_params => { :car => {:model => "ferrari"}, :resource_params => { :users => 8})
140
- # POST to "http://www.myapiurl.com/users/8/cars" with "{ :car => {:model => "ferrari"}" in the header
140
+ # POST to "http://www.myapiurl.com/users/8/cars"
141
+ # with "{ :car => {:model => "ferrari"}" in the header
142
+ RestApi.request.post_cars_in_users(:request_params => { :car => {:model => "ferrari"}},
143
+ :resources_params => { :users => 8})
141
144
  ```
142
145
 
143
146
  **Short Syntax**
@@ -147,17 +150,17 @@ You can pass as many arguments you want. They will be considered as a **:resourc
147
150
  **EXAMPLES**
148
151
 
149
152
  ```ruby
150
- RestApi.request.get_users(18)
151
153
  # GET to "http://www.myapiurl.com/users/18"
154
+ RestApi.request.get_users(18)
152
155
 
153
- RestApi.request.get_cars_from_users(18)
154
156
  # GET to "http://www.myapiurl.com/users/18/cars"
157
+ RestApi.request.get_cars_from_users(18)
155
158
 
156
- RestApi.request.get_cars_from_users(18, 6)
157
159
  # GET to "http://www.myapiurl.com/users/18/cars/6"
160
+ RestApi.request.get_cars_from_users(18, 6)
158
161
 
159
- RestApi.request.put_cars_in_users(18, 6)
160
162
  # PUT to "http://www.myapiurl.com/users/18/cars/6"
163
+ RestApi.request.put_cars_in_users(18, 6)
161
164
  ```
162
165
 
163
166
  **IF** the last argument is a hash then it will be considered the **:request_params**.
@@ -165,14 +168,16 @@ RestApi.request.put_cars_in_users(18, 6)
165
168
  **EXAMPLES**
166
169
 
167
170
  ```ruby
168
- RestApi.request.get_cars_from_users(8, :page => 5)
169
171
  # GET to "http://www.myapiurl.com/users/8/cars?page=5"
172
+ RestApi.request.get_cars_from_users(8, :page => 5)
170
173
 
174
+ # PUT to "http://www.myapiurl.com/users/18/"
175
+ # with { :user => {:name => "myname"} } in the header
171
176
  RestApi.request.put_users(18, :user => {:name => "myname"})
172
- # PUT to "http://www.myapiurl.com/users/18/" with { :user => {:name => "myname"} } in the header
173
177
 
178
+ # PUT to "http://www.myapiurl.com/users/18/cars/6"
179
+ # with { :car => {:model => "mercedes"} } in the header
174
180
  RestApi.request.put_cars_in_users(18, 6, :car => {:model => "mercedes"})
175
- # PUT to "http://www.myapiurl.com/users/18/cars/6" with { :car => {:model => "mercedes"} } in the header
176
181
  ```
177
182
 
178
183
  If there is only one argument and it is a hash then it will be considered as the **:request_params**.
@@ -180,16 +185,17 @@ If there is only one argument and it is a hash then it will be considered as the
180
185
  **EXAMPLES**
181
186
 
182
187
  ```ruby
188
+ # GET to "http://www.myapiurl.com/users/?page=5
183
189
  RestApi.request.get_users(:page => 5)
184
- # GET to "http://www.myapiurl.com/users/?page=8
185
190
 
186
- RestApi.request.post_cars_in_users(:car => {:model => "mercedes"})
187
- # POST to "http://www.myapiurl.com/users/18/cars/6" with {:car => {:model => "mercedes"} in the header
191
+ # POST to "http://www.myapiurl.com/users/18/cars/6"
192
+ # with {:car => {:model => "mercedes"} in the header
193
+ RestApi.request.post_cars_in_users(18, 6, :car => {:model => "mercedes"})
188
194
  ```
189
195
 
190
196
  ## Advanced Configuration
191
197
 
192
- There are some considerations in the current implementation that you must be aware of to make RestApi to work properly. Here we will show them to you and the configuration for every case.
198
+ There are some considerations in the current implementation that you must be aware of to make RestApi work properly. Here we will show them to you and the configuration for each case.
193
199
 
194
200
  #### RestApi.request_parser#ensure_resource_name
195
201
  ****
@@ -204,7 +210,7 @@ RestApi.request.get_public_users
204
210
 
205
211
  It will make a GET to:
206
212
 
207
- *http://www.myapiurl.com/users/public_users*
213
+ *http://www.myapiurl.com/users/users/public*
208
214
 
209
215
  To make things work properly you must ensure the name of the resource like this:
210
216
 
@@ -225,12 +231,12 @@ It will make a GET to:
225
231
  And you can do something like this:
226
232
 
227
233
  ```ruby
228
- RestApi.request.get_public_users :resources_params => {:public_users => 2}
234
+ RestApi.request.get_public_users(:resources_params => {:public_users => 2})
229
235
  ```
230
236
 
231
237
  It will make a GET to:
232
238
 
233
- *http://www.myapiurl.com/public_users/2
239
+ *http://www.myapiurl.com/public_users/2*
234
240
 
235
241
  You can ensure more than one resource name at once:
236
242
 
@@ -260,7 +266,7 @@ And you have a RESTful url like this:
260
266
 
261
267
  *http://www.myapiurl.com/categories/2/categories*
262
268
 
263
- That allows to get/post/put/delete the child categories of category 2. But if you try to do:
269
+ That allows you to get/post/put/delete the child categories of category 2. But if you try to do:
264
270
 
265
271
  ```ruby
266
272
  RestApi.request.get_categories_in_categories(2)
@@ -289,7 +295,7 @@ end
289
295
  Now when you do:
290
296
 
291
297
  ```ruby
292
- RestApi.request.get_subcategories_in_categories :resources_params => {:subcategories => 2}
298
+ RestApi.request.get_subcategories_in_categories(:resources_params => {:categories => 2})
293
299
  ```
294
300
 
295
301
  Will make a GET to:
@@ -301,14 +307,14 @@ If you don't define a map to some resource name in the method, it will be parsed
301
307
  #### RestApi#add_restful_api_methods
302
308
  ****
303
309
 
304
- Note that *map_custom_api_method* only define one request type per time. If you try:
310
+ Note that *map_custom_api_method* only declares one method per call. If you try:
305
311
 
306
312
  ```ruby
307
313
  RestApi.map_custom_api_method :get, :subcategories_in_categories do |map|
308
314
  map.subcategories = "categories"
309
315
  end
310
316
 
311
- RestApi.request.put_subcategories_in_categories :resources_params => {:categories => 2}
317
+ RestApi.request.put_subcategories_in_categories(:resources_params => {:categories => 2})
312
318
  ```
313
319
 
314
320
  The custom *GET* mapping will be ignored and will be made a *PUT* request with the default map to:
@@ -328,7 +334,7 @@ end
328
334
  Now you can do:
329
335
 
330
336
  ```ruby
331
- RestApi.request.put_subcategories_in_categories :resources_params => {:subcategories => 5, :categories => 2}
337
+ RestApi.request.put_subcategories_in_categories(:resources_params => {:subcategories => 5, :categories => 2})
332
338
  ```
333
339
  That a PUT will be made to:
334
340
 
@@ -347,6 +353,7 @@ RestApi.unmap_resources
347
353
 
348
354
  Here we will put the features that will be implemented in the future.
349
355
 
356
+ * Module architecture refactoring
350
357
  * API autentication
351
358
  * Extend the client to accept others formats than JSON
352
359
  * Create the RestApi::Model to work like an ActiveRecord model (ok. we have a long way to go)
@@ -4,18 +4,18 @@ module RestApi
4
4
  module API
5
5
  module RequestParser
6
6
  class << self
7
- def get_url_from_method method_id, resources_params = nil
8
- tokens_array = get_url_tokens_from_method method_id
9
- tokens_array = insert_resources_params_in_tokens_array tokens_array, resources_params unless (resources_params.nil? || resources_params.empty?)
7
+ def get_url_from_method(method_id, resources_params = nil)
8
+ tokens_array = get_url_tokens_from_method(method_id)
9
+ tokens_array = insert_resources_params_in_tokens_array(tokens_array, resources_params) unless (resources_params.nil? || resources_params.empty?)
10
10
  API.url + tokens_array.join("/")
11
11
  end
12
12
 
13
- def get_url_from_map array_resources_names, resources_map, resources_params = nil
13
+ def get_url_from_map(array_resources_names, resources_map, resources_params = nil)
14
14
  # insert the params before translate the url with a map so the rousources_params
15
15
  # must be relative the array resrouces_names
16
16
  # tokens_array = array_resources_names
17
17
 
18
- tokens_array = insert_resources_params_in_tokens_array array_resources_names, resources_params unless (resources_params.nil? || resources_params.empty?)
18
+ tokens_array = insert_resources_params_in_tokens_array(array_resources_names, resources_params) unless (resources_params.nil? || resources_params.empty?)
19
19
  tokens_array ||= array_resources_names
20
20
  tokens_array_mapped = tokens_array.map do |resource_name|
21
21
  if resources_map.respond_to?(resource_name.to_sym)
@@ -28,12 +28,12 @@ module RestApi
28
28
  API.url + tokens_array_mapped.join("/")
29
29
  end
30
30
 
31
- def get_request_type_from_method method_id
31
+ def get_request_type_from_method(method_id)
32
32
  request_type = method_id.to_s.match(/^get|put|delete|post/i)
33
33
  request_type.nil? ? request_type : request_type[0].to_sym
34
34
  end
35
35
 
36
- def get_url_tokens_from_method method_id
36
+ def get_url_tokens_from_method(method_id)
37
37
  method_name_without_pronouns = method_id.to_s.gsub(/_(in|of|from)_/,"_").gsub(/(put|get|post|delete)_/, "")
38
38
  method_name_with_reserved_mask = method_name_without_pronouns
39
39
  ensured_resources_names.each { |ensured_resource_name|
@@ -43,25 +43,30 @@ module RestApi
43
43
  url_tokens.reverse
44
44
  end
45
45
 
46
- def insert_resources_params_in_tokens_array tokens_array, resources_params
46
+ def insert_resources_params_in_tokens_array(tokens_array, resources_params)
47
47
  resources_params_hash = {}
48
48
  tokens_array_copy = Array.new tokens_array #copy so the original remains intact
49
- if resources_params.is_a? Hash
49
+
50
+ if resources_params.is_a?(Hash)
50
51
  resources_params_hash = resources_params
51
52
  else
52
- tokens_array_copy.each_with_index { |token, index|
53
+ tokens_array_copy.each_with_index do |token, index|
53
54
  resources_params_hash[token.to_sym] = resources_params[index] unless resources_params[index].nil?
54
- }
55
+ end
55
56
  end
57
+
56
58
  params_insert_positions = Array.new
57
59
  tokens_array_copy.each_with_index do |token, index|
58
- params_insert_positions << { :param => resources_params_hash[token.to_sym], :position => (index + 1)} if resources_params_hash.has_key? token.to_sym
60
+ params_insert_positions << {
61
+ :param => resources_params_hash[token.to_sym],
62
+ :position => (index + 1)
63
+ } if resources_params_hash.has_key? token.to_sym
59
64
  end
60
65
 
61
66
  params_insert_positions.inject(0) do |insert_offset,insert_position_hash|
62
67
  insert_position = insert_position_hash[:position] + insert_offset
63
68
  tokens_array_copy.insert(insert_position, insert_position_hash[:param].to_s)
64
- insert_offset = insert_offset + 1
69
+ insert_offset + 1
65
70
  end
66
71
 
67
72
  tokens_array_copy
@@ -15,8 +15,8 @@ module RestApi
15
15
  # RestApi.request.get_boss_from_offices -> GET "<APIURL>/offices/boss"
16
16
  # RestApi.request.get_boss_from_offices(:resources_params => { :offices => 5} ) -> GET "<APIURL>/offices/5/boss"
17
17
 
18
- def self.map_custom_api_method request_type, request_resources
19
- array_resources_names = API::RequestParser.get_url_tokens_from_method request_resources
18
+ def self.map_custom_api_method(request_type, request_resources)
19
+ array_resources_names = API::RequestParser.get_url_tokens_from_method(request_resources)
20
20
 
21
21
  # initialize the resource map and pass to the yield
22
22
  resources_map = OpenStruct.new
@@ -31,7 +31,6 @@ module RestApi
31
31
 
32
32
  # create the method
33
33
  method_name = "#{request_type}_#{request_resources}"
34
-
35
34
  self.mapped_methods << method_name.to_sym
36
35
 
37
36
  RequestHandler.class_eval do
@@ -40,11 +39,11 @@ module RestApi
40
39
  end
41
40
 
42
41
  eigenclass.class_eval do
43
- # self = RestApi::RequestHandler
42
+ # self <- RestApi::RequestHandler eigenclass
44
43
  define_method method_name do |*arguments|
45
- params = get_params_from_array_arguments arguments
44
+ params = get_params_from_array_arguments(arguments)
46
45
  request_url = API::RequestParser.get_url_from_map(array_resources_names, resources_map, params[:resources_params])
47
- make_request request_type, request_url, params[:request_params]
46
+ make_request(request_type, request_url, params[:request_params])
48
47
  end
49
48
  end
50
49
  end
@@ -58,7 +57,7 @@ module RestApi
58
57
  end
59
58
 
60
59
  eigenclass.class_eval do
61
- undef_method mapped_method_id
60
+ undef_method(mapped_method_id)
62
61
  end
63
62
  end
64
63
  end
@@ -69,9 +68,9 @@ module RestApi
69
68
  @@mapped_methods
70
69
  end
71
70
 
72
- def self.add_restful_api_methods request_resources, &block
71
+ def self.add_restful_api_methods(request_resources, &block)
73
72
  [:get, :put, :post, :delete].each do |request_type|
74
- self.map_custom_api_method request_type, request_resources, &block
73
+ self.map_custom_api_method(request_type, request_resources, &block)
75
74
  end
76
75
  end
77
76
  end
@@ -11,7 +11,6 @@ module RestApi
11
11
  end
12
12
  end
13
13
 
14
-
15
- class ApiConnectionException < Exception;end;
14
+ class ApiConnectionException < Exception; end;
16
15
  end
17
16
  end
@@ -5,29 +5,29 @@ module RestApi
5
5
  module RequestHandler
6
6
  module Client
7
7
  class << self
8
- def get url, params = {}
8
+ def get(url, params = {})
9
9
  parse_response(RestClient.get(url, :params => params, :accept => :json))
10
10
  end
11
11
 
12
- def post url, params = {}
12
+ def post(url, params = {})
13
13
  parse_response(RestClient.post(url, params, :accept => :json))
14
14
  end
15
15
 
16
- def put url, params = {}
16
+ def put(url, params = {})
17
17
  parse_response(RestClient.put(url, params, :accept => :json))
18
18
  end
19
19
 
20
- def delete url, params = {}
20
+ def delete(url, params = {})
21
21
  parse_response(RestClient.delete(url, :params => params, :accept => :json))
22
22
  end
23
23
 
24
- def parse_response string_response
24
+ def parse_response(string_response)
25
25
  begin
26
26
  return JSON.parse(string_response)
27
27
  rescue Exception => e
28
28
  raise RestApi::Exceptions::ParseResponseException.new(
29
29
  :response_body => string_response,
30
- :error => e )
30
+ :error => e)
31
31
  end
32
32
  end
33
33
 
@@ -9,18 +9,18 @@ module RestApi
9
9
  end
10
10
  end
11
11
 
12
- def self.make_request request_type, request_url, request_params = nil
12
+ def self.make_request(request_type, request_url, request_params = nil)
13
13
  request_params ||= {}
14
14
  begin
15
15
  case request_type
16
16
  when :put
17
- client.put request_url, request_params
17
+ client.put(request_url, request_params)
18
18
  when :post
19
- client.post request_url, request_params
19
+ client.post(request_url, request_params)
20
20
  when :delete
21
- client.delete request_url, request_params
21
+ client.delete(request_url, request_params)
22
22
  when :get
23
- client.get request_url, request_params
23
+ client.get(request_url, request_params)
24
24
  else
25
25
  raise Exception.new("Invalid request method")
26
26
  end
@@ -55,6 +55,7 @@ module RestApi
55
55
 
56
56
  def self.get_params_from_array_arguments arguments
57
57
  params_hash = { }
58
+
58
59
  if arguments[0].is_a? Hash
59
60
  if (arguments[0].has_key?(:resources_params) || arguments[0].has_key?(:request_params))
60
61
  params_hash[:resources_params] = arguments[0][:resources_params]
@@ -69,6 +70,7 @@ module RestApi
69
70
  end
70
71
  params_hash[:resources_params] = Array.new arguments
71
72
  end
73
+
72
74
  params_hash.delete :request_params if params_hash[:request_params].nil?
73
75
  params_hash
74
76
  end
@@ -1,3 +1,3 @@
1
1
  module RestApi
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/rest_api.rb CHANGED
@@ -39,8 +39,4 @@ module RestApi
39
39
  def self.request_parser
40
40
  RestApi::API::RequestParser
41
41
  end
42
-
43
-
44
-
45
-
46
42
  end
data/rest_api.gemspec CHANGED
@@ -5,8 +5,8 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Codus Tecnologia"]
6
6
  gem.email = ["vinicius.oyama@codus.com.br"]
7
7
  gem.platform = Gem::Platform::RUBY
8
- gem.description = %q{An API client for any application!}
9
- gem.summary = %q{An API client for any application!}
8
+ gem.description = %q{An easy to use, no configuration API client!}
9
+ gem.summary = %q{An easy to use, no configuration API client!}
10
10
  gem.homepage = "https://github.com/codus/rest_api"
11
11
 
12
12
  gem.files = `git ls-files`.split($\)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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: 2012-10-24 00:00:00.000000000 Z
12
+ date: 2012-10-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -59,7 +59,7 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.6'
62
- description: An API client for any application!
62
+ description: An easy to use, no configuration API client!
63
63
  email:
64
64
  - vinicius.oyama@codus.com.br
65
65
  executables: []
@@ -112,7 +112,7 @@ rubyforge_project:
112
112
  rubygems_version: 1.8.24
113
113
  signing_key:
114
114
  specification_version: 3
115
- summary: An API client for any application!
115
+ summary: An easy to use, no configuration API client!
116
116
  test_files:
117
117
  - spec/rest_api/api/request_parser_spec.rb
118
118
  - spec/rest_api/api_spec.rb