Permalink
Browse files

Initial commit

  • Loading branch information...
leereilly committed May 18, 2011
0 parents commit 372f50bef0cab04414fce52a22d76f232dd3b800
173 app.rb
@@ -0,0 +1,173 @@
+$:.unshift File.join(File.dirname(__FILE__),'lib')
+
+require 'rubygems'
+require 'net/http'
+require 'sinatra'
+require 'json'
+require 'erb'
+require 'uri'
+
+require 'User'
+require 'Repo'
+
+DataMapper::Logger.new(STDOUT, :debug)
+
+disable :show_exceptions
+set :environment, :production
+
+configure :development do
+ DataMapper.setup(:default, 'sqlite:////Users/lreilly/Projects/github-scores.com/db/dbdb')
+end
+
+configure :production do
+ DataMapper.setup(:default, 'sqlite:////Users/lreilly/Projects/github-scores.com/db/db.db')
+end
+
+error do
+ @title = "404"
+ @text = "Sorry, but this cat is in another castle!"
+ @display_small_search = false
+ erb :not_found
+end
+
+get '/' do
+ begin
+ if params[:github_url]
+ @title = 'High Scores'
+ @github_url = sanitize_input params[:github_url]
+ @user = get_user_from_github_url(@github_url)
+ @repo = get_repo_from_github_url(@github_url)
+ @high_scores = get_high_scores(@user, @repo)
+ @display_small_search = true
+ redirect "/#{@user}/#{@repo}/high_scores/"
+ else
+ @title = 'High Scores'
+ @text = 'Please enter a Github repository URL'
+ @display_small_search = false
+ erb :index
+ end
+ rescue
+ @title = "404"
+ @text = "Sorry, but this cat is in another castle!"
+ @display_small_search = false
+ erb :not_found
+ end
+end
+
+get '/recent/?' do
+ @repos = Repo.all(:limit => 5, :order => [ :updated_at.desc ])
+ @display_small_search = true
+ erb :recent
+end
+
+
+get '/credits/?' do
+ erb :credits
+end
+
+get '/help/?' do
+ @display_small_search = true
+ erb :help
+end
+
+get '/about/?' do
+ @display_small_search = true
+ erb :about
+end
+
+get '/:user/:repo/?' do
+ @user = User::create_from_username(params[:user])
+ @repo = Repo::create_from_username_and_repo(params[:user], params[:repo])
+ @display_small_search = true
+ erb :repo
+end
+
+get '/:user/?' do
+ @user = User::create_from_username(params[:user])
+ @display_small_search = true
+ erb :user
+end
+
+not_found do
+ @title = "404"
+ @text = "Sorry, but this cat is in another castle!"
+ erb :not_found
+end
+
+def sanitize_input(url)
+ url = url.downcase
+
+ # Special rules for Github URLs starting with 'github.com'
+ if url[0..9] == 'github.com'
+ url = 'https://www.github.com' + url[9..url.size]
+
+ # Special rules for Github URLs starting with 'www.github.com'
+ elsif url[0..13] == 'www.github.com'
+ url = 'https://www.github.com' + url[13..url.size]
+ end
+
+ # Special rules for Github URLs ending in 'git'
+ if url[-4,4] == '.git'
+ url = url[0..-5]
+ end
+
+ url = url.gsub("http://", "https://")
+ url = url.gsub("git@github.com:", "https://www.github.com/")
+ url = url.gsub("git://", "https://www.")
+
+ # If someone just passes in user/repo e.g. leereilly/leereilly.net
+ tokens = url.split('/')
+ if tokens.size == 2
+ url = "https://www.github.com/#{tokens[0]}/#{tokens[1]}"
+ end
+
+ return url
+end
+
+def get_user_from_github_url(sanitized_github_url)
+ return sanitized_github_url.split('/')[3]
+end
+
+def get_repo_from_github_url(sanitized_github_url)
+ return sanitized_github_url.split('/')[4]
+end
+
+def get_high_scores(user, repo)
+ begin
+ # Kludge - three API calls
+ stored_user = User::create_from_username(user)
+ puts "Storing user: #{stored_user}"
+ stored_repo = Repo::create_from_username_and_repo(user, repo)
+ puts "Storing repo: #{stored_repo}"
+
+ contributors_url = "http://github.com/api/v2/json/repos/show/#{user}/#{repo}/contributors"
+
+ contributors_feed = Net::HTTP.get_response(URI.parse(contributors_url))
+ contributors = contributors_feed.body
+ contributors_result = JSON.parse(contributors)
+ repository_contributors = contributors_result['contributors']
+ contributors_array = Array.new
+ repository_contributors.each do |repository_contributor|
+ user_hash = Hash.new
+ user_hash[:login] = repository_contributor['login']
+ user_hash[:email] = repository_contributor['email']
+ user_hash[:gravatar_id] = repository_contributor['gravatar_id']
+ user_hash[:location] = repository_contributor['location']
+ user_hash[:contributions] = repository_contributor['contributions'].to_i
+ contributors_array << user_hash
+ end
+ return contributors_array
+ rescue
+ raise "Sorry, this GitHub repository doesn't seem to exist or is private"
+ end
+end
+
+
+get '/:user/:repo/high_scores/?' do
+ @title = "New"
+ @user = params[:user]
+ @repo = params[:repo]
+ @high_scores = get_high_scores(@user, @repo)
+ @display_small_search = true
+ erb :high_scores
+end
@@ -0,0 +1,10 @@
+require 'rubygems'
+require 'sinatra.rb'
+
+# Sinatra defines #set at the top level as a way to set application configuration
+set :views, File.join(File.dirname(__FILE__), 'app','views')
+set :run, false
+set :env, (ENV['RACK_ENV'] ? ENV['RACK_ENV'].to_sym : :development)
+
+require 'app/main'
+run Sinatra.application
BIN db/db.db
Binary file not shown.
@@ -0,0 +1,48 @@
+require 'rubygems'
+require 'data_mapper'
+require 'net/http'
+require 'json'
+require 'uri'
+
+DataMapper::Logger.new($stdout, :debug)
+DataMapper.setup(:default, 'sqlite:////Users/lreilly/Projects/github-scores.com/db/db.db')
+
+class Contributor
+ include DataMapper::Resource
+
+ API_VERSION = 'v2'
+ BASE_URL = 'http://github.com/api/' + API_VERSION + '/json/user/show/'
+
+ property :id, Serial
+ property :login, String
+ property :gravatar_id, String
+ property :contributions, String
+
+ def self.create_from_user_and_repo(user, repo)
+ stored_user = User::create_from_username(user)
+ stored_repo = Repo::create_from_username_and_repo(user, repo)
+
+ contributors_url = "http://github.com/api/v2/json/repos/show/#{user}/#{repo}/contributors"
+ contributors_feed = Net::HTTP.get_response(URI.parse(contributors_url))
+ contributors = contributors_feed.body
+ contributors_result = JSON.parse(contributors)
+ repository_contributors = contributors_result['contributors']
+ contributors_array = Array.new
+
+ repository_contributors.each do |repository_contributor|
+ contributor = Contributor.new
+ contributor.login = repository_contributor['login']
+ contributor.gravatar_id = repository_contributor['gravatar_id']
+ contributor.contributions = repository_contributor['contributions']
+ contributor.save
+ end
+ end
+
+ def self.get_json_response(url)
+ Net::HTTP.get_response(URI.parse(url))
+ end
+end
+
+DataMapper::auto_upgrade!
+contributors = Contributor::create_from_user_and_repo('leereilly', 'leereilly.net')
+
@@ -0,0 +1,84 @@
+require 'rubygems'
+require 'data_mapper'
+require 'net/http'
+require 'json'
+require 'uri'
+
+DataMapper::Logger.new($stdout, :debug)
+DataMapper.setup(:default, 'sqlite:////Users/lreilly/Projects/github-scores.com/db/db.db')
+
+class Repo
+ include DataMapper::Resource
+
+ API_VERSION = 'v2'
+ BASE_URL = 'http://github.com/api/' + API_VERSION + '/json/repos/show/'
+
+ property :id, Serial
+ property :owner, String
+ property :url, String
+ property :homepage, String
+ property :name, String
+ property :description, String
+ property :parent, String
+ property :has_issues, String
+ property :source, String
+ property :watchers, String
+ property :has_downloads, String
+ property :fork, String
+ property :forks, String
+ property :has_wiki, String
+ property :pushed_at, String
+ property :open_issues, String
+ property :updated_at, DateTime
+
+ def self.create_from_username_and_repo(username, repo)
+ repo_data_url = Repo.get_repo_data_url(username, repo)
+
+ if found_repo = Repo.first(:owner => username, :name => repo)
+ if Time.now - Time.parse(found_repo.updated_at.to_s) <= 60*60*24
+ puts "Repo created less than 24 hours ago. Returning DB record"
+ return found_repo
+ else
+ puts "Updating current repo"
+ repo = found_repo
+ end
+ else
+ puts "User not found; using web services"
+ repo = Repo.new
+ end
+
+ repo_data_response = get_json_response(repo_data_url)
+ repo_data = JSON.parse(repo_data_response.body)
+ repo_data = repo_data['repository']
+
+ repo.owner = repo_data['owner']
+ repo.name = repo_data['name']
+ repo.url = repo_data['url']
+ repo.homepage = repo_data['homepage']
+ repo.description = repo_data['description']
+ repo.parent = repo_data['parent']
+ repo.has_issues = repo_data['has_issues']
+ repo.source = repo_data['source']
+ repo.watchers = repo_data['watchers']
+ repo.has_downloads = repo_data['has_downloads']
+ repo.fork = repo_data['fork']
+ repo.forks = repo_data['forks']
+ repo.has_wiki = repo_data['has_wiki']
+ repo.pushed_at = repo_data['pushed_at']
+ repo.open_issues = repo_data['open_issues']
+ repo.updated_at = Time.now
+ repo.save!
+ return repo
+ end
+
+ def self.get_json_response(url)
+ Net::HTTP.get_response(URI.parse(url))
+ end
+
+ def self.get_repo_data_url(username, repo)
+ return BASE_URL + username + '/' + repo
+ end
+end
+
+DataMapper.auto_upgrade!
+
Oops, something went wrong.

0 comments on commit 372f50b

Please sign in to comment.