Skip to content

Commit c463652

Browse files
committed
Merge pull request github#1 from github/rendering_data
Add samples for new dev guide content
2 parents 60e1e22 + e54f062 commit c463652

File tree

17 files changed

+479
-3
lines changed

17 files changed

+479
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
access_token
2+
.DS_Store

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
platform-samples
22
================
33

4-
A public place for all platform sample projects.
4+
This is a public place for all sample projects related to the GitHub Platform.
55

6-
Will be open-source and public when it's good and ready. The goal is to have
7-
people clone the repo, and get started easily.
6+
## Hierarchy
7+
8+
The directories are organized to correlate with guides found on developer.github.com.
9+
But here it is, broken down:
10+
11+
* _api_: here's a bunch of sample code relating to the API. Subdirectories in this
12+
category are broken up by language. Do you have a language sample you'd like added?
13+
Make a pull request and we'll consider it.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "http://rubygems.org"
2+
3+
gem "json", "1.7.7"
4+
gem 'sinatra', '~> 1.3.5'
5+
gem 'rest-client', '~> 1.6.3'
6+
gem 'sinatra_auth_github', '~> 0.13.3'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
addressable (2.3.3)
5+
faraday (0.8.6)
6+
multipart-post (~> 1.1)
7+
faraday_middleware (0.9.0)
8+
faraday (>= 0.7.4, < 0.9)
9+
hashie (1.2.0)
10+
json (1.7.7)
11+
mime-types (1.21)
12+
multi_json (1.6.1)
13+
multipart-post (1.2.0)
14+
netrc (0.7.7)
15+
octokit (1.23.0)
16+
addressable (~> 2.2)
17+
faraday (~> 0.8)
18+
faraday_middleware (~> 0.9)
19+
hashie (~> 1.2)
20+
multi_json (~> 1.3)
21+
netrc (~> 0.7.7)
22+
rack (1.5.2)
23+
rack-protection (1.3.2)
24+
rack
25+
rest-client (1.6.7)
26+
mime-types (>= 1.16)
27+
sinatra (1.3.5)
28+
rack (~> 1.4)
29+
rack-protection (~> 1.3)
30+
tilt (~> 1.3, >= 1.3.3)
31+
sinatra_auth_github (0.13.3)
32+
sinatra (~> 1.0)
33+
warden-github (~> 0.13.1)
34+
tilt (1.3.3)
35+
warden (1.2.1)
36+
rack (>= 1.0)
37+
warden-github (0.13.2)
38+
octokit (>= 1.22.0)
39+
warden (> 1.0)
40+
41+
PLATFORMS
42+
ruby
43+
44+
DEPENDENCIES
45+
json (= 1.7.7)
46+
rest-client (~> 1.6.3)
47+
sinatra (~> 1.3.5)
48+
sinatra_auth_github (~> 0.13.3)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
basics-of-authentication
2+
================
3+
4+
This is the sample projct built by following the "[Basics of Authentication][basics of auth]"
5+
guide on developer.github.com.
6+
7+
It consists of two different servers: one built correctly, and one built less optimally.
8+
9+
To run these projects, make sure you have [Bundler][bundler] installed; then type
10+
`bundle install` on the command line.
11+
12+
For the "less optimal" server, type `ruby server.rb` on the command line.
13+
This will run the server at `localhost:4567`.
14+
15+
For the correct server, enter `rackup -p 4567` on the command line.
16+
17+
[basics of auth]: http://developer.github.com/guides/basics-of-authentication/
18+
[bundler]: http://gembundler.com/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'sinatra/auth/github'
2+
require 'rest_client'
3+
4+
module Example
5+
class MyBasicApp < Sinatra::Base
6+
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
7+
# Instead, set and test environment variables, like below
8+
# if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
9+
# CLIENT_ID = ENV['GITHUB_CLIENT_ID']
10+
# CLIENT_SECRET = ENV['GITHUB_CLIENT_SECRET']
11+
# end
12+
13+
CLIENT_ID = ENV['GH_BASIC_CLIENT_ID']
14+
CLIENT_SECRET = ENV['GH_BASIC_SECRET_ID']
15+
16+
enable :sessions
17+
18+
set :github_options, {
19+
:scopes => "user",
20+
:secret => CLIENT_SECRET,
21+
:client_id => CLIENT_ID,
22+
:callback_url => "/callback"
23+
}
24+
25+
register Sinatra::Auth::Github
26+
27+
get '/' do
28+
if !authenticated?
29+
authenticate!
30+
else
31+
access_token = github_user["token"]
32+
auth_result = RestClient.get("https://api.github.com/user", {:params => {:access_token => access_token, :accept => :json},
33+
:accept => :json})
34+
35+
auth_result = JSON.parse(auth_result)
36+
37+
erb :advanced, :locals => {:login => auth_result["login"],
38+
:hire_status => auth_result["hireable"] ? "hireable" : "not hireable"}
39+
end
40+
end
41+
42+
get '/callback' do
43+
if authenticated?
44+
redirect "/"
45+
else
46+
authenticate!
47+
end
48+
end
49+
end
50+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ENV['RACK_ENV'] ||= 'development'
2+
require "rubygems"
3+
require "bundler/setup"
4+
5+
require File.expand_path(File.join(File.dirname(__FILE__), 'advanced_server'))
6+
7+
run Example::MyBasicApp
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'sinatra'
2+
require 'rest_client'
3+
require 'json'
4+
5+
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
6+
# Instead, set and test environment variables, like below
7+
# if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
8+
# CLIENT_ID = ENV['GITHUB_CLIENT_ID']
9+
# CLIENT_SECRET = ENV['GITHUB_CLIENT_SECRET']
10+
# end
11+
CLIENT_ID = ENV['GH_BASIC_CLIENT_ID']
12+
CLIENT_SECRET = ENV['GH_BASIC_SECRET_ID']
13+
14+
get '/' do
15+
erb :index, :locals => {:client_id => CLIENT_ID}
16+
end
17+
18+
get '/callback' do
19+
# get temporary GitHub code...
20+
session_code = request.env['rack.request.query_hash']["code"]
21+
# ... and POST it back to GitHub
22+
result = RestClient.post("https://github.com/login/oauth/access_token",
23+
{:client_id => CLIENT_ID,
24+
:client_secret => CLIENT_SECRET,
25+
:code => session_code},
26+
:accept => :json)
27+
access_token = JSON.parse(result)["access_token"]
28+
29+
auth_result = RestClient.get("https://api.github.com/user", {:params => {:access_token => access_token, :accept => :json},
30+
:accept => :json})
31+
32+
auth_result = JSON.parse(auth_result)
33+
34+
erb :basic, :locals => {:login => auth_result["login"],
35+
:hire_status => auth_result["hireable"] ? "hireable" : "not hireable"
36+
}
37+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<html>
4+
<head>
5+
6+
</head>
7+
<body>
8+
<p>Well, well, well, <%= login %>! It looks like you're <em>still</em> <%= hire_status %>!</p>
9+
</body>
10+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<html>
4+
<head>
5+
6+
</head>
7+
<body>
8+
<p>Hello, <%= login %>! It looks like you're <%= hire_status %>.</p>
9+
</body>
10+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<html>
4+
<head>
5+
6+
</head>
7+
<body>
8+
<p>Well, hello there!</p>
9+
<p>We're going to now talk to the GitHub API. Ready? <a href="https://github.com/login/oauth/authorize?client_id=<%= client_id %>">Click here</a> to begin!</a></p>
10+
<p>If that link doesn't work, remember to provide your own <a href="http://developer.github.com/v3/oauth/#web-application-flow">Client ID</a>!</p>
11+
</body>
12+
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "http://rubygems.org"
2+
3+
gem "json", "1.7.7"
4+
gem 'sinatra', '~> 1.3.5'
5+
gem 'sinatra_auth_github', '~> 0.13.3'
6+
gem 'octokit', '~> 1.23.0'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
addressable (2.3.3)
5+
faraday (0.8.6)
6+
multipart-post (~> 1.1)
7+
faraday_middleware (0.9.0)
8+
faraday (>= 0.7.4, < 0.9)
9+
hashie (1.2.0)
10+
json (1.7.7)
11+
multi_json (1.6.1)
12+
multipart-post (1.2.0)
13+
netrc (0.7.7)
14+
octokit (1.23.0)
15+
addressable (~> 2.2)
16+
faraday (~> 0.8)
17+
faraday_middleware (~> 0.9)
18+
hashie (~> 1.2)
19+
multi_json (~> 1.3)
20+
netrc (~> 0.7.7)
21+
rack (1.5.2)
22+
rack-protection (1.4.0)
23+
rack
24+
sinatra (1.3.5)
25+
rack (~> 1.4)
26+
rack-protection (~> 1.3)
27+
tilt (~> 1.3, >= 1.3.3)
28+
sinatra_auth_github (0.13.3)
29+
sinatra (~> 1.0)
30+
warden-github (~> 0.13.1)
31+
tilt (1.3.4)
32+
warden (1.2.1)
33+
rack (>= 1.0)
34+
warden-github (0.13.2)
35+
octokit (>= 1.22.0)
36+
warden (> 1.0)
37+
38+
PLATFORMS
39+
ruby
40+
41+
DEPENDENCIES
42+
json (= 1.7.7)
43+
octokit (~> 1.23.0)
44+
sinatra (~> 1.3.5)
45+
sinatra_auth_github (~> 0.13.3)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
rendering-data-as-graphs
2+
================
3+
4+
This is the sample projct built by following the "[Rendering Data as Graphs][rendering data]"
5+
guide on developer.github.com.
6+
7+
To run these projects, make sure you have [Bundler][bundler] installed; then type
8+
`bundle install` on the command line.
9+
10+
Then, enter `rackup -p 4567` on the command line.
11+
12+
[rendering data]: http://developer.github.com/guides/rendering-data-as-graphs/
13+
[bundler]: http://gembundler.com/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ENV['RACK_ENV'] ||= 'development'
2+
require "rubygems"
3+
require "bundler/setup"
4+
5+
require File.expand_path(File.join(File.dirname(__FILE__), 'server'))
6+
7+
run Example::MyGraphApp
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require 'sinatra/auth/github'
2+
require 'octokit'
3+
4+
module Example
5+
class MyGraphApp < Sinatra::Base
6+
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
7+
# Instead, set and test environment variables, like below
8+
# if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
9+
# CLIENT_ID = ENV['GITHUB_CLIENT_ID']
10+
# CLIENT_SECRET = ENV['GITHUB_CLIENT_SECRET']
11+
# end
12+
13+
CLIENT_ID = ENV['GH_GRAPH_CLIENT_ID']
14+
CLIENT_SECRET = ENV['GH_GRAPH_SECRET_ID']
15+
16+
enable :sessions
17+
18+
set :github_options, {
19+
:scopes => "repo",
20+
:secret => CLIENT_SECRET,
21+
:client_id => CLIENT_ID,
22+
:callback_url => "/"
23+
}
24+
25+
register Sinatra::Auth::Github
26+
27+
get '/' do
28+
if !authenticated?
29+
authenticate!
30+
else
31+
octokit_client = Octokit::Client.new(:login => github_user.login, :oauth_token => github_user.token)
32+
repos = octokit_client.repositories
33+
language_obj = {}
34+
repos.each do |repo|
35+
# sometimes language can be nil
36+
if repo.language
37+
if !language_obj[repo.language]
38+
language_obj[repo.language] = 1
39+
else
40+
language_obj[repo.language] += 1
41+
end
42+
end
43+
end
44+
45+
languages = []
46+
language_obj.each do |lang, count|
47+
languages.push :language => lang, :count => count
48+
end
49+
50+
language_byte_count = []
51+
repos.each do |repo|
52+
repo_name = repo.name
53+
repo_langs = octokit_client.languages("#{github_user.login}/#{repo_name}")
54+
repo_langs.each do |lang, count|
55+
if !language_obj[lang]
56+
language_obj[lang] = count
57+
else
58+
language_obj[lang] += count
59+
end
60+
end
61+
end
62+
63+
language_obj.each do |lang, count|
64+
language_byte_count.push :name => "#{lang} (#{count})", :count => count
65+
end
66+
67+
# some mandatory formatting for d3
68+
language_bytes = [ :name => "language_bytes", :elements => language_byte_count]
69+
70+
erb :lang_freq, :locals => { :languages => languages.to_json, :language_byte_count => language_bytes.to_json}
71+
end
72+
end
73+
end
74+
end

0 commit comments

Comments
 (0)