Module: V1::ApiController::Metadata

Included in:
V1::ApiController
Defined in:
app/controllers/v1/api_controller/metadata.rb

Overview

@apiDefine Pagination Pagination

@apiSuccess {Object} meta 
@apiSuccess {Integer} meta.current_page Current page in pagination
@apiSuccess {Integer} meta.total_pages Total number of pages
@apiSuccess {Integer} meta.total_count Total number of elements

Instance Method Summary collapse

Instance Method Details

#meta_attributes(collection, extra_meta = {}) ⇒ Hash

Generate metadata information for request.


20
21
22
23
24
25
# File 'app/controllers/v1/api_controller/metadata.rb', line 20

def meta_attributes(collection, extra_meta = {})
  meta = {}
  meta = meta.merge(meta_pagination(collection)) if collection.class.name.include?('ActiveRecord')
  # Merge the metadata result with the extra metadata
  meta.merge(extra_meta)
end

#meta_pagination(collection) ⇒ Hash

Generate metadata pagination for request.


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/v1/api_controller/metadata.rb', line 31

def meta_pagination(collection)
  next_page = previous_page = nil
  # Generate the link to the next page
  next_page = compile_page(URI(request.original_url), collection.next_page) if collection.total_pages > collection.current_page
  # Generate the link to the previous page
  previous_page = compile_page(URI(request.original_url), collection.prev_page) if collection.current_page > 1 && collection.current_page < collection.total_pages
  # Current page of the response
  # Total pages of the query
  # Total records of the query
  meta = {
    current_page: collection.current_page,
    total_pages: collection.total_pages,
    total_count: collection.total_count

  }
  # Next number page
  meta[:next_page] = collection.next_page if collection.next_page
  # Previous number page
  meta[:prev_page] = collection.prev_page if collection.prev_page
  # Link to the next page
  meta[:href_previous_page] = previous_page if previous_page
  # Link to the previous page
  meta[:href_next_page] = next_page if next_page
  meta
end