Skip to content

Instantly share code, notes, and snippets.

@bryanbraun
Last active December 21, 2017 13:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bryanbraun/8624205 to your computer and use it in GitHub Desktop.
Save bryanbraun/8624205 to your computer and use it in GitHub Desktop.
This file is an example Sinatra app.rb file, showing github API Authentication and usage examples of Octokit.rb. For more context on setting up Sinatra to work with the Github API, see http://developer.github.com/guides/basics-of-authentication/.
# app.rb
require "sinatra"
require "sinatra/activerecord"
require 'rest-client'
require 'json'
require 'octokit'
require 'pp' # for pretty print debugging
# Needed for preserving github auth tokens across sessions.
enable :sessions
# Set up the database
set :database, "sqlite3:///blog.db"
# This is our database object, Post
#
# I can query it with the Active Record Querying Interface (http://guides.rubyonrails.org/active_record_querying.html)
#
# Examples:
# Post.count #=> Counts the number of records in the posts table
# Post.All #=> Returns all posts. Equivalent to SELECT * from posts;
#
class Post < ActiveRecord::Base
end
######################################
# Github Auth
#
# Based on this tutorial:
# http://developer.github.com/guides/basics-of-authentication/
######################################
CLIENT_ID = ENV['GH_BASIC_CLIENT_ID']
CLIENT_SECRET = ENV['GH_BASIC_SECRET_ID']
def authenticated?
session[:access_token]
end
def authenticate!
erb :"login", :locals => {:client_id => CLIENT_ID}
end
######################################
# Routing Calls
######################################
# Define pages (GET requests with template responses) at specific URLs
# Home page
get "/" do
gh_data = get_github_data()
# Pass in the CLIENT_ID for the login button on the home page.
erb :"index", :locals => {:client_id => CLIENT_ID, :gh_data => gh_data}
end
# Callback URL for Github Authentication. This gets a github oauth token
# for use in acquiring API data. It's a bit manual and could be replaced with
# https://github.com/atmos/sinatra_auth_github, but it works well for now.
get '/callback' do
# Get temporary GitHub code...
session_code = request.env['rack.request.query_hash']['code']
# ... and POST it back to GitHub
result = RestClient.post('https://github.com/login/oauth/access_token',
{:client_id => CLIENT_ID,
:client_secret => CLIENT_SECRET,
:code => session_code},
:accept => :json)
# example result:
# { "access_token":"xxasdfasdf234234123dvadsfasdfas",
# "token_type":"bearer",
# "scope":"user:email"
# }
# Make the access token available across sessions.
session[:access_token] = JSON.parse(result)['access_token']
# As soon as someone authenticates, we kick them to the home page.
redirect '/'
end
######################################
# Other Methods
######################################
# Demonstrate examples of how to get Data using Octokit.rb
# Example API data is from this example account: https://github.com/octocat
def get_github_data()
if !authenticated?
authenticate!
else
client = Octokit::Client.new :access_token => session[:access_token]
# Create a hash for collecting our example data.
data = Hash.new
# Get various types of data using Octokit.rb
# User Data:
# User data is available via client.user. As long as you have be granted access
# to the "user" scope, you can access any values given in this example API
# response: http://developer.github.com/v3/users/#response
data[:login] = client.user.login # => "octocat"
data[:email] = client.user.email # => "octocat@github.com"
data[:location] = client.user.location # => "San Francisco"
# Repository Data:
# Repository data is available via client.repository (for a specific repo)
# or client.repositories for the full list of repos. As long as you have been
# granted access to the "repo" scope, you can access any values given in this
# example API response: http://developer.github.com/v3/repos/#response-1
#
# Get data from a specific repository, if that repository exists.
if client.repository?("octocat/Hello-World")
data[:repo_id] = client.repository("octocat/Hello-World").id
data[:repo_forks] = client.repository("octocat/Hello-World").forks_count
data[:repo_stars] = client.repository("octocat/Hello-World").stargazers_count
data[:repo_watchers] = client.repository("octocat/Hello-World").watchers_count
data[:repo_full_name] = client.repository("octocat/Hello-World").full_name
data[:repo_description] = client.repository("octocat/Hello-World").description
# Note: You can see all repo methods by printing client.repository("octocat/Hello-World").methods
end
# Instantiate an array for storing repo names.
data[:repo_names] = Array.new
# Loop through all repositories and collect repo names.
client.repositories.each do |repo|
data[:repo_name] << repo.name
end
return data
end
end
@pengwynn
Copy link

You're on the right track. For the rest, you would go through the other methods on Client.

# repo data
client.repository('bryanbraun/writer').stargazers_count # or other fields

# issue data
client.issue('bryanbraun/petitions-newslink', 8).title # or other fields

You can browse the source to see which methods are available on the Octokit client, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment