Class: Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/paginator.rb

Overview

this class fetches and paginates the issues from github.com

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ Paginator

Returns a new instance of Paginator.



7
8
9
10
11
12
# File 'lib/paginator.rb', line 7

def initialize(credentials)
  @credentials = credentials
  @page = 1
  @cache = {}
  @next = {}
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



5
6
7
# File 'lib/paginator.rb', line 5

def page
  @page
end

Instance Method Details

#decObject



42
43
44
# File 'lib/paginator.rb', line 42

def dec
  @page = [@page - 1, 1].max
end

#format_issue(issue) ⇒ Object



34
35
36
# File 'lib/paginator.rb', line 34

def format_issue(issue)
  "#{issue['number']} --- #{issue['title']}"
end

#formatted_issuesObject



38
39
40
# File 'lib/paginator.rb', line 38

def formatted_issues
  issues.map { |i| format_issue(i) }
end

#incObject



46
47
48
# File 'lib/paginator.rb', line 46

def inc
  @page += 1
end

#issuesObject



14
15
16
17
18
19
20
# File 'lib/paginator.rb', line 14

def issues
  @cache[@page] ||= begin
    issues = fetch_issues
    @next[@page] = issues.count.positive?
    issues
  end
end

#next?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/paginator.rb', line 26

def next?
  issues && @next[@page]
end

#no_issues?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/paginator.rb', line 22

def no_issues?
  @page == 1 && issues.empty?
end

#prev?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/paginator.rb', line 30

def prev?
  @page > 1
end