Class: Api::Paging::TimePaginator

Inherits:
Object
  • Object
show all
Defined in:
lib/api/paging/time_paginator.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TimePaginator

Returns a new instance of TimePaginator.


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/api/paging/time_paginator.rb', line 6

def initialize(opts={})
  @query_base = opts[:query_base]
  @query_time_field = opts[:query_time_field]
  @data_time_field = opts[:data_time_field]
  @current_time = opts[:current_time]
  @limit = opts[:limit]
  @is_descending = opts[:is_descending]
  direction = if @is_descending
                "<"
              else
                ">"
              end
  @time_query_string = "#{@query_time_field} #{direction} ?"
  @sort_string = if @is_descending
                   "#{@query_time_field} DESC"
                 else
                   "#{@query_time_field} ASC"
                 end
end

Instance Method Details

#filter_parameters(parameters) ⇒ Object


69
70
71
72
# File 'lib/api/paging/time_paginator.rb', line 69

def filter_parameters(parameters)
  parameters.delete(:before)
  parameters.delete(:after)
end

#next_page(for_url = true) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/api/paging/time_paginator.rb', line 37

def next_page(for_url=true)
  page_data
  return nil unless next_time

  return next_page_as_query_parameter if for_url

  TimePaginator.new(
    query_base:       @query_base,
    query_time_field: @query_time_field,
    query_data_field: @data_time_field,
    current_time:     next_time,
    is_descending:    @is_descending,
    limit:            @limit
  )
end

#page_dataObject


26
27
28
29
30
31
32
33
34
35
# File 'lib/api/paging/time_paginator.rb', line 26

def page_data
  return @data if @data

  @data = @query_base.where([@time_query_string, @current_time.iso8601(3)]).limit(@limit).order(@sort_string)
  time_data = @data.map {|d| d[@data_time_field] }.sort
  @min_time = time_data.first
  @max_time = time_data.last + 0.001.seconds if time_data.last

  @data
end

#previous_page(for_url = true) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/api/paging/time_paginator.rb', line 53

def previous_page(for_url=true)
  page_data
  return nil unless previous_time

  return previous_page_as_query_parameter if for_url

  TimePaginator.new(
    query_base:       @query_base,
    query_time_field: @query_time_field,
    query_data_field: @data_time_field,
    current_time:     previous_time,
    is_descending:    !@is_descending,
    limit:            @limit
  )
end