I finally got this job. The trick was to use the global variable in faraday on_complete - I tried to find the best solution, but it was the best I could do. Once again, I got the header code here . Here's a complete guide on how to get the pagination with it:
First, on my server side, I have a Kaminari stone, and I pass page and per as parameters to the server from the client. (It also uses a search to search)
def search @search = Order.order('id desc').search(params[:q]) @orders = @search.result(distinct: true).page(params[:page]).per(params[:per]) respond_with @orders.as_json(include: :items) end
My client makes a request as follows:
@orders = Order.search(q: { client_id_eq: @current_user.id }, page: params[:page], per: 3)`
Returning to the server, I have this in my ApiController (application controller for api):
protected def self.set_pagination_headers(name, options = {}) after_filter(options) do |controller| results = instance_variable_get("@#{name}") headers["X-Pagination"] = { total_count: results.total_count, offset_value: results.offset_value }.to_json end end
In the orders_controller.rb server, I set the pagination headers for the search method:
class OrdersController < ApiController set_pagination_headers :orders, only: [:search] ... end
Now, to get the headers, we need the Faraday middleware in it on the client.
Now on your client controller you have a global hash variable called $ pagination; mine is as follows:
$pagintation = { total_count: 0, offset_value: 0 }`
Finally, I added the Kaminari gem to my client application to split the array and get these simple page links:
@orders = Kaminari.paginate_array(@orders, total_count: $pagination[:total_count]).page(params[:page]).per(params[:per_page])`
I hope this can help someone else, and if anyone knows a better way to do this, let me know!