Skip to content

Commit ca95fe8

Browse files
committed
add app.
1 parent 58318ec commit ca95fe8

27 files changed

+5183
-21
lines changed

.bundle/config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
BUNDLE_PATH: vendor
3+
BUNDLE_DISABLE_SHARED_GEMS: '1'

.gitignore

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,2 @@
1-
*.gem
2-
*.rbc
3-
.bundle
4-
.config
5-
coverage
6-
InstalledFiles
7-
lib/bundler/man
8-
pkg
9-
rdoc
10-
spec/reports
11-
test/tmp
12-
test/version_tmp
13-
tmp
14-
15-
# YARD artifacts
16-
.yardoc
17-
_yardoc
18-
doc/
1+
config/*.yml
2+
vendor/ruby

Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source :rubygems
2+
3+
gem 'faraday'
4+
gem 'faraday_middleware'
5+
gem 'multi_json'
6+
gem 'sinatra'
7+
8+
group :development do
9+
gem 'pry'
10+
end

Gemfile.lock

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
coderay (1.0.6)
5+
faraday (0.8.1)
6+
multipart-post (~> 1.1)
7+
faraday_middleware (0.8.7)
8+
faraday (>= 0.7.4, < 0.9)
9+
method_source (0.7.1)
10+
multi_json (1.3.6)
11+
multipart-post (1.1.5)
12+
pry (0.9.9.6)
13+
coderay (~> 1.0.5)
14+
method_source (~> 0.7.1)
15+
slop (>= 2.4.4, < 3)
16+
rack (1.4.1)
17+
rack-protection (1.2.0)
18+
rack
19+
sinatra (1.3.2)
20+
rack (~> 1.3, >= 1.3.6)
21+
rack-protection (~> 1.2)
22+
tilt (~> 1.3, >= 1.3.3)
23+
slop (2.4.4)
24+
tilt (1.3.3)
25+
26+
PLATFORMS
27+
ruby
28+
29+
DEPENDENCIES
30+
faraday
31+
faraday_middleware
32+
multi_json
33+
pry
34+
sinatra

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
instructurecon2012
2-
==================
1+
#instructurecon2012
32

4-
Example code using the Canvas API.
3+
Example code using the Canvas API.
4+
5+
To get started:
6+
* `bundle install --local --path vendor/ruby`
7+
* `bundle exec rackup`
8+
9+
The site will be running on `localhost:9292`
10+
11+
##TODO
12+
* Add examples beyond pagination.

config.ru

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require './config/environment.rb'
2+
require 'example'
3+
4+
run Example

config/canvas.yml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
domain: http://canvas.instructure.com
2+
access_token: # your canvas access token here.
3+
account_id: # the id of your canvas account

config/environment.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
$: << File.expand_path(File.join(File.dirname(__FILE__), '..'))
2+
3+
require 'bundler'
4+
require 'find'
5+
require 'json'
6+
require 'yaml'
7+
8+
Bundler.require
9+
10+
Find.find('lib').each do |path|
11+
require path if path.match(/\.rb$/)
12+
end
13+
14+
config = YAML::load_file('config/canvas.yml')
15+
CANVAS = Canvas::Client.new(config)
16+
ACCOUNT_ID = config['account_id']

example.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Example < Sinatra::Base
2+
get '/' do
3+
erb :index
4+
end
5+
6+
# Use some simple pagination code to pull page links from header
7+
# and use then to navigate classes. Setting per_page to 1 in this
8+
# example so any user with > 1 courses will see pagination links.
9+
get '/pagination' do
10+
page = params[:page] || 1
11+
per_page = params[:per_page] || 2
12+
13+
result = CANVAS.get("accounts/#{ACCOUNT_ID}/users") do |req|
14+
req.params['per_page'] = per_page
15+
req.params['page'] = page
16+
end
17+
18+
@users = result.body
19+
@pages = parse_page_links(result)
20+
erb :pagination
21+
end
22+
23+
protected
24+
def parse_page_links(resp)
25+
link_header = resp.headers['link'].split(',')
26+
27+
# Matches the name of each link: "next," "prev," "first," or "last."
28+
name_regex = /rel="([a-z]+)/
29+
30+
# Matches the full link, e.g. "/api/v1/accounts/1/users?page=1&per_page=15"
31+
link_regex = /^<([^>]+)/
32+
33+
# Matches only the querystring in the link, e.g. "?page=1&per_page=15"
34+
params_regex = /(\?[^>]+)/
35+
36+
# Reduce the link header into a hash.
37+
link_header.inject({}) do |links, link|
38+
key = link.match(name_regex)[1].to_sym
39+
val = link.match(params_regex)[1]
40+
links[key] = val
41+
42+
links
43+
end
44+
end
45+
end

lib/canvas/client.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Canvas
2+
class Client
3+
attr_reader :conn
4+
5+
# Create a new Faraday instance for connecting to Canvas.
6+
#
7+
# opts - A hash of options used to init the client.
8+
# domain - The url Canvas it located at. Should be something like
9+
# "http://canvas.local".
10+
# access_token - An access token for a user with permissions to make
11+
# the example calls.
12+
#
13+
# Examples
14+
#
15+
# client = Canvas::Client.new(:url => 'http://canvas.local',
16+
# :key => 'SOOxF61Qdyb...')
17+
def initialize(opts = {})
18+
opts = opts.inject({}) { |n, o| n[o[0].to_sym] = o[1]; n }
19+
20+
@conn = Faraday.new(:url => "#{opts[:domain]}/api/v1/") do |conn|
21+
conn.request :oauth2, opts[:access_token]
22+
conn.request :json
23+
conn.response :json, :content_type => /\bjson$/
24+
conn.adapter Faraday.default_adapter
25+
end
26+
end
27+
28+
# If an unknown method is called, pass it to the Faraday client.
29+
def method_missing(method, *args, &block)
30+
args.first.sub!(%r{^/api/v1/}, '')
31+
@conn.send(method, *args, &block)
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)