This repository was archived by the owner on Nov 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample.rb
More file actions
45 lines (36 loc) · 1.22 KB
/
example.rb
File metadata and controls
45 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Example < Sinatra::Base
get '/' do
erb :index
end
# Use some simple pagination code to pull page links from header
# and use then to navigate classes. Setting per_page to 1 in this
# example so any user with > 1 courses will see pagination links.
get '/pagination' do
page = params[:page] || 1
per_page = params[:per_page] || 2
result = CANVAS.get("accounts/#{ACCOUNT_ID}/users") do |req|
req.params['per_page'] = per_page
req.params['page'] = page
end
@users = result.body
@pages = parse_page_links(result)
erb :pagination
end
protected
def parse_page_links(resp)
link_header = resp.headers['link'].split(',')
# Matches the name of each link: "next," "prev," "first," or "last."
name_regex = /rel="([a-z]+)/
# Matches the full link, e.g. "/api/v1/accounts/1/users?page=1&per_page=15"
link_regex = /^<([^>]+)/
# Matches only the querystring in the link, e.g. "?page=1&per_page=15"
params_regex = /(\?[^>]+)/
# Reduce the link header into a hash.
link_header.inject({}) do |links, link|
key = link.match(name_regex)[1].to_sym
val = link.match(params_regex)[1]
links[key] = val
links
end
end
end