rest_api 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -3
- data/lib/rest_api/api/request_parser.rb +15 -14
- data/lib/rest_api/version.rb +1 -1
- data/spec/rest_api/api/request_parser_spec.rb +21 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -266,13 +266,17 @@ And you have a RESTful url like this:
|
|
266
266
|
|
267
267
|
*http://www.myapiurl.com/categories/2/categories*
|
268
268
|
|
269
|
-
That allows you to get/post/put/delete the child categories of category 2.
|
269
|
+
That allows you to get/post/put/delete the child categories of category 2. And if you try to do:
|
270
270
|
|
271
271
|
```ruby
|
272
272
|
RestApi.request.get_categories_in_categories(2)
|
273
273
|
```
|
274
274
|
|
275
|
-
|
275
|
+
It will be OK and will make a GET to:
|
276
|
+
|
277
|
+
*http://www.myapiurl.com/categories/2/categories/*
|
278
|
+
|
279
|
+
But if you try:
|
276
280
|
|
277
281
|
```ruby
|
278
282
|
RestApi.request.get_categories_in_categories(:resources_params => {:categories => 2})
|
@@ -282,7 +286,7 @@ It will make a GET to:
|
|
282
286
|
|
283
287
|
*http://www.myapiurl.com/categories/2/categories/2*
|
284
288
|
|
285
|
-
Because the resources params arguments are linked to
|
289
|
+
Because the resources params arguments are linked to the resource name defined in the method call.
|
286
290
|
|
287
291
|
You can turn this aroud by defining a request method and mapping the resources like this:
|
288
292
|
|
@@ -44,31 +44,32 @@ module RestApi
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def insert_resources_params_in_tokens_array(tokens_array, resources_params)
|
47
|
-
|
48
|
-
|
47
|
+
|
48
|
+
# copy so the original remains intact (because tokens_array is a pointer)
|
49
|
+
tokens_array_copy = Array.new tokens_array
|
50
|
+
params_insert_positions = Array.new
|
49
51
|
|
50
52
|
if resources_params.is_a?(Hash)
|
51
|
-
|
53
|
+
tokens_array_copy.each_with_index do |token, index|
|
54
|
+
params_insert_positions << {
|
55
|
+
:param => resources_params[token.to_sym],
|
56
|
+
:position => (index + 1)
|
57
|
+
} if resources_params.has_key? token.to_sym
|
58
|
+
end
|
52
59
|
else
|
53
|
-
|
54
|
-
|
60
|
+
resources_params.each_with_index do |param, index|
|
61
|
+
params_insert_positions << {
|
62
|
+
:param => param,
|
63
|
+
:position => (index + 1)
|
64
|
+
} if (index + 1) <= tokens_array.length
|
55
65
|
end
|
56
66
|
end
|
57
67
|
|
58
|
-
params_insert_positions = Array.new
|
59
|
-
tokens_array_copy.each_with_index do |token, index|
|
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
|
64
|
-
end
|
65
|
-
|
66
68
|
params_insert_positions.inject(0) do |insert_offset,insert_position_hash|
|
67
69
|
insert_position = insert_position_hash[:position] + insert_offset
|
68
70
|
tokens_array_copy.insert(insert_position, insert_position_hash[:param].to_s)
|
69
71
|
insert_offset + 1
|
70
72
|
end
|
71
|
-
|
72
73
|
tokens_array_copy
|
73
74
|
end
|
74
75
|
|
data/lib/rest_api/version.rb
CHANGED
@@ -174,6 +174,27 @@ describe "RestApi::API - url_parser" do
|
|
174
174
|
tokens_array = ["usuarios", "eventos", "carros", "festas"]
|
175
175
|
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, [1,2,4]).should be == ["usuarios", "1", "eventos", "2", "carros", "4", "festas"]
|
176
176
|
end
|
177
|
+
|
178
|
+
it "should accept params as an array - resources with same name" do
|
179
|
+
tokens_array = ["usuarios", "usuarios"]
|
180
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, [1]).should be == ["usuarios", "1", "usuarios"]
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
it "should accept params as an array - ignore extra params" do
|
185
|
+
tokens_array = ["usuarios", "usuarios"]
|
186
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, [1,5,6,7]).should be == ["usuarios", "1", "usuarios", "5"]
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should accept params as an array - resources with same name case 2" do
|
190
|
+
tokens_array = ["usuarios", "usuarios", "usuarios"]
|
191
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, [1]).should be == ["usuarios", "1", "usuarios", "usuarios"]
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should accept params as an array - resources with same name case 3" do
|
195
|
+
tokens_array = ["usuarios", "usuarios", "usuarios"]
|
196
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, [1,6]).should be == ["usuarios", "1", "usuarios", "6", "usuarios"]
|
197
|
+
end
|
177
198
|
end
|
178
199
|
|
179
200
|
describe "pluralize_resource" do
|