Forked from bryanbraun/Octokit_Sinatra_Example.rb
Last active
August 29, 2015 14:12
-
-
Save melindavoo/0e6c970c745c7425d534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 # => "[email protected]" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment