Skip to content

Commit 4035e6d

Browse files
committed
introducing the 'limit' and 'offset' parameters to the inventory_items_controller's #index, #in_city and #near_city actions; API swagger JSON specs have been updated accordingly
1 parent 1195ea0 commit 4035e6d

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

app/controllers/application_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def not_found_with_max_age(max_age = caching_time)
1919
expires_in max_age, public: true
2020
end
2121

22+
def sanitized_limit_and_offset
23+
offset = params[:offset].to_i.abs
24+
limit = (params[:limit].to_i == 0) ? nil : params[:limit].to_i.abs
25+
[limit, offset]
26+
end
27+
2228
# Renders the results of a block passed in as JSON, but only if the
2329
# given 'staleness' critera are met. A presenter class is determined
2430
# based on the entity passed in, which gets passed back to the passed-in block.

app/controllers/inventory_items_controller.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class InventoryItemsController < ApplicationController
44
# Show a single inventory_item
55
# List all inventory_items
66
# Example:
7-
# ` curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items/1.json'
7+
# ` curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items/1.json?representation=full'
88
def show
99
Rails.logger.debug "Inventory Item with ID #{@item.id} is #{@item.inspect}"
1010

@@ -17,9 +17,10 @@ def show
1717

1818
# List all inventory_items
1919
# Example:
20-
# ` curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items.json'
20+
# ` curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items.json?representation=full&limit=15&offset=30'
2121
def index
22-
all_items = InventoryItem.all
22+
lim, off = sanitized_limit_and_offset
23+
all_items = InventoryItem.all.limit(lim).offset(off)
2324
return json_response([]) unless newest_item = all_items.sort_by(&:updated_at).last
2425
Rails.logger.info "newest_item is #{newest_item.inspect}"
2526
render_if_stale(all_items, last_modified: newest_item.updated_at.utc, etag: newest_item) do |item_presenters|
@@ -70,9 +71,10 @@ def destroy
7071

7172
# Find inventory items filtered by a given city ID
7273
# Example:
73-
# `curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items/in_city/1.json'`
74+
# `curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items/in_city/1.json?representation=full&limit=15&offset=30'`
7475
def in_city
75-
return json_response([]) if (city_items = InventoryItem.where(params.slice(:city_id))).blank?
76+
lim, off = sanitized_limit_and_offset
77+
return json_response([]) if (city_items = InventoryItem.where(params.slice(:city_id)).limit(lim).offset(off)).blank?
7678
newest_item = city_items.sort_by(&:updated_at).last
7779
Rails.logger.info "newest_item is #{newest_item.inspect}"
7880
render_if_stale(city_items, last_modified: newest_item.updated_at.utc, etag: newest_item) do |item_presenters|
@@ -84,12 +86,13 @@ def in_city
8486

8587
# Find inventory items in a given city ID, or near that city within a given number of miles (default: 15 miles)
8688
# Example:
87-
# `curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items/near_city/1.json?within=20'`
89+
# `curl -v -H "Content-type: application/json" 'http://localhost:3000/api/v1/inventory_items/near_city/1.json?within=20&representation=full&limit=15&offset=30'`
8890
def near_city
91+
lim, off = sanitized_limit_and_offset
8992
nearby_city_ids = RemoteCity.find_nearby_city_id(params[:city_id], params.fetch(:within, 15)).map(&:id)
9093
nearby_city_ids << params[:city_id].to_i if params[:city_id].to_i
9194
Rails.logger.info "nearby city IDs (including the requested city itself): #{nearby_city_ids}"
92-
city_items = InventoryItem.where( city_id: nearby_city_ids )
95+
city_items = InventoryItem.where( city_id: nearby_city_ids ).limit(lim).offset(off)
9396
return json_response([]) if city_items.blank?
9497
newest_item = city_items.sort_by(&:updated_at).last
9598
Rails.logger.info "newest_item is #{newest_item.inspect}"

public/api_docs/v1/inventory_items.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
"type": "string",
1616
"description": "the requested representation for the inventory items returned (e.g., 'full'); defaults to a default smaller representation",
1717
"required": false
18+
},
19+
{
20+
"paramType": "query",
21+
"name": "limit",
22+
"type": "integer",
23+
"description": "limit the number of items returned to `limit`; defaults to not enforcing a limit",
24+
"required": false
25+
},
26+
{
27+
"paramType": "query",
28+
"name": "offset",
29+
"type": "integer",
30+
"description": "offset the first item to be returned by `offset`; defaults to 0",
31+
"required": false
1832
}
1933
],
2034
"responseMessages": [
@@ -209,6 +223,20 @@
209223
"type": "string",
210224
"description": "the requested representation for the inventory items returned (e.g., 'full'); defaults to a default smaller representation",
211225
"required": false
226+
},
227+
{
228+
"paramType": "query",
229+
"name": "limit",
230+
"type": "integer",
231+
"description": "limit the number of items returned to `limit`; defaults to not enforcing a limit",
232+
"required": false
233+
},
234+
{
235+
"paramType": "query",
236+
"name": "offset",
237+
"type": "integer",
238+
"description": "offset the first item to be returned by `offset`; defaults to 0",
239+
"required": false
212240
}
213241
],
214242
"responseMessages": [
@@ -248,6 +276,20 @@
248276
"type": "string",
249277
"description": "the requested representation for the inventory items returned (e.g., 'full'); defaults to a default smaller representation",
250278
"required": false
279+
},
280+
{
281+
"paramType": "query",
282+
"name": "limit",
283+
"type": "integer",
284+
"description": "limit the number of items returned to `limit`; defaults to not enforcing a limit",
285+
"required": false
286+
},
287+
{
288+
"paramType": "query",
289+
"name": "offset",
290+
"type": "integer",
291+
"description": "offset the first item to be returned by `offset`; defaults to 0",
292+
"required": false
251293
}
252294
],
253295
"responseMessages": [

0 commit comments

Comments
 (0)