Skip to content

Commit 99753a8

Browse files
committed
Syntax highlighting for Rendering Data as Graphs guide.
1 parent 9470ca6 commit 99753a8

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

content/guides/rendering-data-as-graphs.md

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,45 @@ First, [register a new application][new oauth application] on GitHub. Set the ma
2323
URLs to `http://localhost:4567/`. As [before][basics-of-authentication], we're going to handle authentication for the API by
2424
implementing a Rack middleware using [sinatra-auth-github][sinatra auth github]:
2525

26-
require 'sinatra/auth/github'
27-
28-
module Example
29-
class MyGraphApp < Sinatra::Base
30-
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
31-
# Instead, set and test environment variables, like below
32-
# if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
33-
# CLIENT_ID = ENV['GITHUB_CLIENT_ID']
34-
# CLIENT_SECRET = ENV['GITHUB_CLIENT_SECRET']
35-
# end
36-
37-
CLIENT_ID = ENV['GH_GRAPH_CLIENT_ID']
38-
CLIENT_SECRET = ENV['GH_GRAPH_SECRET_ID']
39-
40-
enable :sessions
41-
42-
set :github_options, {
43-
:scopes => "repo",
44-
:secret => CLIENT_SECRET,
45-
:client_id => CLIENT_ID,
46-
:callback_url => "/"
47-
}
48-
49-
register Sinatra::Auth::Github
50-
51-
get '/' do
52-
if !authenticated?
53-
authenticate!
54-
else
55-
access_token = github_user["token"]
56-
end
57-
end
58-
end
59-
end
26+
#!ruby
27+
require 'sinatra/auth/github'
28+
29+
module Example
30+
class MyGraphApp < Sinatra::Base
31+
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
32+
# Instead, set and test environment variables, like below
33+
# if ENV['GITHUB_CLIENT_ID'] && ENV['GITHUB_CLIENT_SECRET']
34+
# CLIENT_ID = ENV['GITHUB_CLIENT_ID']
35+
# CLIENT_SECRET = ENV['GITHUB_CLIENT_SECRET']
36+
# end
37+
38+
CLIENT_ID = ENV['GH_GRAPH_CLIENT_ID']
39+
CLIENT_SECRET = ENV['GH_GRAPH_SECRET_ID']
40+
41+
enable :sessions
42+
43+
set :github_options, {
44+
:scopes => "repo",
45+
:secret => CLIENT_SECRET,
46+
:client_id => CLIENT_ID,
47+
:callback_url => "/"
48+
}
49+
50+
register Sinatra::Auth::Github
51+
52+
get '/' do
53+
if !authenticated?
54+
authenticate!
55+
else
56+
access_token = github_user["token"]
57+
end
58+
end
59+
end
60+
end
6061

6162
Set up a similar _config.ru_ file as in the previous example:
6263

64+
#!ruby
6365
ENV['RACK_ENV'] ||= 'development'
6466
require "rubygems"
6567
require "bundler/setup"
@@ -78,6 +80,7 @@ so you know it'll work.
7880
Authentication with the API via Octokit is easy. Just pass your login
7981
and token to the `Octokit::Client` constructor:
8082

83+
#!ruby
8184
if !authenticated?
8285
authenticate!
8386
else
@@ -89,11 +92,13 @@ to see the different programming languages they use, and count which ones are us
8992
most often. To do that, we'll first need a list of our repositories from the API.
9093
With Octokit, that looks like this:
9194

95+
#!ruby
9296
repos = client.repositories
9397

9498
Next, we'll iterate over each repository, and count the language that GitHub
9599
associates with it:
96100

101+
#!ruby
97102
language_obj = {}
98103
repos.each do |repo|
99104
# sometimes language can be nil
@@ -126,6 +131,7 @@ check out ["D3 for Mortals"][D3 mortals].
126131
D3 is a JavaScript library, and likes working with data as arrays. So, let's convert our Ruby hash into
127132
a JSON array for use by JavaScript in the browser.
128133

134+
#!ruby
129135
languages = []
130136
language_obj.each do |lang, count|
131137
languages.push :language => lang, :count => count
@@ -141,6 +147,7 @@ Now, _lang_freq.erb_ is going to need some JavaScript to support rendering a bar
141147
For now, you can just use the code provided here, and refer to the resources linked above
142148
if you want to learn more about how D3 works:
143149

150+
#!html
144151
<!DOCTYPE html>
145152
<meta charset="utf-8">
146153
<html>
@@ -239,6 +246,7 @@ should be a great way to visualize the sizes of our coding languages used, rathe
239246
than simply the count. We'll need to construct an array of objects that looks
240247
something like this:
241248

249+
#!javascript
242250
[ { "name": "language1", "size": 100},
243251
{ "name": "language2", "size": 23}
244252
...
@@ -247,13 +255,15 @@ something like this:
247255
Since we already have a list of repositories above, let's inspect each one, and
248256
call [the language listing API method][language API]:
249257

258+
#!ruby
250259
repos.each do |repo|
251260
repo_name = repo.name
252261
repo_langs = octokit_client.languages("#{github_user.login}/#{repo_name}")
253262
end
254263

255264
From there, we'll cumulatively add each language found to a "master list":
256265

266+
#!ruby
257267
repo_langs.each do |lang, count|
258268
if !language_obj[lang]
259269
language_obj[lang] = count
@@ -264,6 +274,7 @@ From there, we'll cumulatively add each language found to a "master list":
264274

265275
After that, we'll format the contents into a structure that D3 understands:
266276

277+
#!ruby
267278
language_obj.each do |lang, count|
268279
language_byte_count.push :name => "#{lang} (#{count})", :count => count
269280
end
@@ -275,12 +286,14 @@ After that, we'll format the contents into a structure that D3 understands:
275286

276287
To wrap up, we pass this JSON information over to the same ERB template:
277288

289+
#!ruby
278290
erb :lang_freq, :locals => { :languages => languages.to_json, :language_byte_count => language_bytes.to_json}
279291

280292

281293
Like before, here's a bunch of JavaScript that you can drop
282294
directly into your template:
283295

296+
#!html
284297
<div id="byte_freq"></div>
285298
<script>
286299
var language_bytes = <%= language_byte_count %>

0 commit comments

Comments
 (0)