Skip to content

Commit

Permalink
Add support for returning pagination info
Browse files Browse the repository at this point in the history
  • Loading branch information
phylor authored and rmehner committed Jun 13, 2022
1 parent 97f5f28 commit f43dbae
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ Basic usage of a `Ioki::Client`
products = platform_client.products
# returns a list of Ioki::Model::Platform::Product instances

stations = platform_client.stations(products.first, auto_paginate: true)
# stations are a scoped endpoint within products, to the interface requires
stations = platform_client.stations(products.first, paginate: true)
# stations are a scoped endpoint within products, so the interface requires
# either a product or a product id as the first parameter.
# This call will then fetch the index of stations. This example also shows
# auto_pagination, which keeps calling the index until the last page was
# fetched. Bear in mind, that auto_pagination might be extremely expensive.
# This call will then fetch the index of stations. Use `paginate: true` to
# receive pagination information in the response. Otherwise, only the data of
# the first page is returned.
unless stations.meta.last_page
next_page = stations.meta.page + 1
stations = platform_client.stations(products.first, params: {page: next_page}, paginate: true)
end

new_station = Ioki::Model::Platform::Station.new(
location_name: 'Test',
station_type: 'virtual',
lat: stations.first.lat,
lng: stations.first.lng
lat: stations.data.first.lat,
lng: stations.data.first.lng
)

created_station = platform_client.create_station(products.first, new_station)
Expand All @@ -57,6 +61,12 @@ Basic usage of a `Ioki::Client`

platform_client.delete_station(products.first, created_station)
# will delete the formerly created station

stations = platform_client.stations(products.first, auto_paginate: true)
# This example shows auto_pagination, which keeps calling the index until the
# last page was fetched. Bear in mind, that auto_pagination might be extremely
# expensive.
first_station = stations.first
```

See `spec/ioki/examples` for more examples.
Expand Down
10 changes: 9 additions & 1 deletion lib/ioki/apis/endpoints/index.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative '../../model/response'

module Endpoints
class Index
attr_reader :model_class, :base_path, :resource, :path
Expand All @@ -24,9 +26,15 @@ def full_path
end

def call(client, args = [], options = {}, &block)
options = options.dup
paginate = options.delete(:paginate)
auto_paginate = options.delete(:auto_paginate)

if auto_paginate
if paginate
data, parsed_response = send_request(client, args, options)

Ioki::Model::Response.new(data, parsed_response.dig('meta'))
elsif auto_paginate
paginated_requests(client, args, options, &block)
elsif block_given?
send_request(client, args, options).first.each { |item| block.call(item) }
Expand Down
14 changes: 14 additions & 0 deletions lib/ioki/model/meta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Ioki
module Model
class Meta
attr_reader :page, :last_page

def initialize(attributes)
@page = attributes['page']
@last_page = attributes['last_page']
end
end
end
end
16 changes: 16 additions & 0 deletions lib/ioki/model/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require_relative 'meta'

module Ioki
module Model
class Response
attr_reader :data, :meta

def initialize(data, meta)
@data = data
@meta = Ioki::Model::Meta.new(meta)
end
end
end
end

0 comments on commit f43dbae

Please sign in to comment.