Class: ActiveResource::Pagination::PagingFormatter

Inherits:
Object
  • Object
show all
Includes:
Formats::JsonFormat
Defined in:
lib/active_resource/pagination.rb

Overview

PagingFormatter expects a JSON response that looks like this (for a resource named “posts”): {

"total_pages": 5,
"current_page": 1,
"posts": [{
  "id": 1
}]

}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ PagingFormatter

Returns a new instance of PagingFormatter.



25
26
27
# File 'lib/active_resource/pagination.rb', line 25

def initialize(name)
  self.collection_name = name
end

Instance Attribute Details

#collection_nameObject

Returns the value of attribute collection_name.



23
24
25
# File 'lib/active_resource/pagination.rb', line 23

def collection_name
  @collection_name
end

Instance Method Details

#decode(json) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_resource/pagination.rb', line 29

def decode(json)
  meta = super(json)
  if meta.is_a?(Hash) && meta['total_pages']
    list = PagedArray.new(meta[collection_name])

    list.total_pages  = meta['total_pages']
    list.current_page = meta['current_page']

    list
  elsif meta.is_a?(Array)
    list = PagedArray.new(meta)

    list.total_pages = 1
    list.current_page = 1

    list
  else
    meta
  end
end