Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rwdaigle/gisted
base: 8a01e9d8ea
...
head fork: raulb/gisted
compare: 56014a9325
  • 4 commits
  • 22 files changed
  • 0 commit comments
  • 1 contributor
View
3  Gemfile
@@ -7,7 +7,10 @@ gem 'pg'
gem 'omniauth'
gem 'omniauth-github'
+gem 'rack-worker'
+gem 'dalli'
gem 'octokit'
+gem 'queue_classic'
gem 'jquery-rails'
gem 'haml-rails'
View
10 Gemfile.lock
@@ -32,6 +32,7 @@ GEM
arel (3.0.2)
builder (3.0.3)
daemons (1.1.9)
+ dalli (2.2.1)
erubis (2.7.0)
eventmachine (1.0.0)
execjs (1.4.0)
@@ -87,6 +88,9 @@ GEM
omniauth (~> 1.0)
pg (0.14.1)
polyglot (0.3.3)
+ queue_classic (2.0.1)
+ pg (~> 0.14.0)
+ scrolls (~> 0.2.1)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
@@ -94,6 +98,8 @@ GEM
rack
rack-test (0.6.2)
rack (>= 1.0)
+ rack-worker (0.0.1.rc4)
+ json
rails (3.2.8)
actionmailer (= 3.2.8)
actionpack (= 3.2.8)
@@ -117,6 +123,7 @@ GEM
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
+ scrolls (0.2.3)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -139,12 +146,15 @@ PLATFORMS
ruby
DEPENDENCIES
+ dalli
haml-rails
jquery-rails
octokit
omniauth
omniauth-github
pg
+ queue_classic
+ rack-worker
rails (= 3.2.8)
sass-rails (~> 3.2.3)
thin
View
3  Procfile
@@ -1 +1,2 @@
-web: bundle exec thin start -p $PORT
+web: bundle exec thin start -p $PORT
+worker: bundle exec rake qc:work
View
4 Rakefile
@@ -3,5 +3,7 @@
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
+require "queue_classic"
+require "queue_classic/tasks"
-Gisted::Application.load_tasks
+Gisted::Application.load_tasks
View
6 app/controllers/application_controller.rb
@@ -22,6 +22,10 @@ def force_user_login
end
def current_user
- @current_user ||= session[:user_id] ? User.find(session[:user_id]) : nil
+ begin
+ @current_user ||= session[:user_id] ? User.find(session[:user_id]) : nil
+ rescue Exception => e
+ nil
+ end
end
end
View
11 app/controllers/gists_controller.rb
@@ -1,14 +1,15 @@
+require "queue_classic"
+
class GistsController < ApplicationController
before_filter :force_user_login
def index
- @gists = gh_client.gists
+ @gists = current_user.gists.includes(:files)
end
- protected
-
- def gh_client
- @gh_client ||= Octokit::Client.new(:login => current_user.gh_username, :oauth_token => current_user.gh_oauth_token)
+ def refresh
+ QC.enqueue("GistFetcher.fetch_user_gists", current_user.id)
+ redirect_to gists_path
end
end
View
2  app/controllers/sessions_controller.rb
@@ -10,7 +10,7 @@ def logout
end
def create
- user = User.authorize(request.env['omniauth.auth'])
+ user = User.authenticate(request.env['omniauth.auth'])
log_in_user(user.id)
redirect_to gists_path
end
View
44 app/models/gist.rb
@@ -0,0 +1,44 @@
+class Gist < ActiveRecord::Base
+
+ attr_accessible :gh_id, :user_id, :description, :url, :git_pull_url, :git_push_url, :public, :comment_count, :gh_created_at, :gh_updated_at
+
+ belongs_to :user
+ has_many :files, :class_name => 'GistFile', :dependent => :delete_all
+
+ class << self
+
+ def import(gh_gist)
+ gist = import_gist(gh_gist)
+ gist.import_files(gh_gist)
+ gist
+ end
+
+ protected
+
+ def import_gist(gh_gist)
+
+ user = User.where(gh_id: gh_gist.user.id).first
+ gh_id = gh_gist['id']
+
+ attributes = {
+ gh_id: gh_id, user_id: user.id, description: gh_gist.description,
+ url: gh_gist.html_url, git_push_url: gh_gist.git_push_url, git_pull_url: gh_gist.git_pull_url,
+ public: gh_gist.public, comment_count: gh_gist.comments,
+ gh_created_at: gh_gist.created_at, gh_updated_at: gh_gist.updated_at
+ }
+
+ if(existing_gist = where(gh_id: gh_id).first)
+ existing_gist.update_attributes(attributes)
+ existing_gist
+ else
+ Gist.create(attributes)
+ end
+ end
+ end
+
+ def import_files(gh_gist)
+ gh_gist.files.each do |filename, gh_file|
+ GistFile.import(id, gh_file)
+ end
+ end
+end
View
28 app/models/gist_fetcher.rb
@@ -0,0 +1,28 @@
+class GistFetcher
+
+ class << self
+
+ def fetch_user_gists(user_id)
+ user = User.find(user_id)
+ gh_client(user).gists.each do |gh_gist|
+ # TODO add Scrolls logging
+ Gist.import(gh_gist)
+ end
+ end
+
+ # Fetch individual gists from API (needed only for additional info?)
+ # def fetch_gist(user_id, gist_gh_id)
+ # user = User.find(user_id)
+ # gh_gist = user.gh_client.gist(gist_gh_id)
+ # # TODO: Persist gist
+ # # Gist.transfer_from_gh(user_id, gh_gist)
+ # end
+
+ private
+
+ def gh_client(user)
+ Octokit::Client.new(:login => user.gh_username, :oauth_token => user.gh_oauth_token, :auto_traversal => true)
+ end
+
+ end
+end
View
25 app/models/gist_file.rb
@@ -0,0 +1,25 @@
+class GistFile < ActiveRecord::Base
+
+ attr_accessible :gist_id, :filename, :raw_url, :language, :file_type, :content, :size_bytes
+
+ belongs_to :gist
+
+ class << self
+
+ def import(gist_id, gh_file)
+
+ attributes = {
+ gist_id: gist_id, filename: gh_file.filename, raw_url: gh_file.raw_url,
+ language: gh_file.language, file_type: gh_file.type, size_bytes: gh_file['size'],
+ content: gh_file.content
+ }
+
+ if(existing_file = where(gist_id: gist_id, filename: gh_file.filename).first)
+ existing_file.update_attributes(attributes)
+ existing_file
+ else
+ create(attributes)
+ end
+ end
+ end
+end
View
25 app/models/user.rb
@@ -1,19 +1,24 @@
class User < ActiveRecord::Base
- attr_accessible :gh_avatar_url, :gh_oauth_token, :gh_url, :gh_username
+ attr_accessible :gh_id, :gh_email, :gh_name, :gh_avatar_url, :gh_oauth_token, :gh_url, :gh_username
+
+ has_many :gists, :dependent => :destroy
class << self
- def authorize(auth)
- token = auth.credentials.token
- user = User.find_by_gh_oauth_token(token)
- user = create_from_gh_oauth(token, auth) unless user
- return user
- end
+ def authenticate(auth)
+
+ attributes = {
+ gh_id: auth.uid, gh_oauth_token: auth.credentials.token, gh_username: auth.info.nickname, gh_name: auth.info.name,
+ gh_email: auth.info.email, gh_avatar_url: auth.info.image, gh_url: auth.info.urls.GitHub
+ }
- def create_from_gh_oauth(token, auth)
- gh_username, gh_avatar_url, gh_url = auth.info.nickname, auth.info.image, auth.info.GitHub
- User.create(gh_oauth_token: token, gh_username: gh_username, gh_avatar_url: gh_avatar_url, gh_url: gh_url)
+ if(existing_user = User.where(gh_id: auth.uid).first)
+ existing_user.update_attributes(attributes)
+ existing_user
+ else
+ User.create(attributes)
+ end
end
end
end
View
12 app/views/gists/index.html.haml
@@ -1,17 +1,17 @@
-Your gists:
+= "Your gists (#{link_to("refresh", refresh_gists_path, :method => :post)}):".html_safe
- for gist in @gists do
/ %p
/ = gist.inspect
%p
- = link_to gist.description, gist.html_url
- = "(#{gist.public ? "Public" : "Private"})"
+ = link_to gist.description, gist.url
+ = "(#{gist.public? ? "Public" : "Private"})"
%br/
- = "Created on #{gist.created_at.to_datetime.to_formatted_s(:short)}, last updated on #{gist.updated_at.to_datetime.to_formatted_s(:short)}"
+ = "Created on #{gist.gh_created_at.to_datetime.to_formatted_s(:short)}, last updated on #{gist.gh_updated_at.to_datetime.to_formatted_s(:short)}"
%ul
%li
= "#{pluralize(gist.files.size, "file")}:"
%ul
- - gist.files.each do |name, file|
+ - gist.files.each do |file|
%li
= link_to(file.filename, file.raw_url)
- = "- #{file.language} (#{file.type}), #{file['size'] / 1028}kb"
+ = "- #{file.language} (#{file.file_type}), #{file.size_bytes / 1028}kb"
View
3  app/views/homes/index.html.haml
@@ -1,4 +1,5 @@
%h4 Hi there
+%p= link_to "My gists", gists_path
%p= link_to "Login", login_path
-%p= link_to "Logout", logout_path
+%p= link_to "Logout", logout_path
View
2  config/application.rb
@@ -62,5 +62,7 @@ class Application < Rails::Application
config.middleware.use OmniAuth::Builder do
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'], :scope => "gist"
end
+
+ # config.middleware.use Rack::Worker # errors... :(
end
end
View
10 config/environments/production.rb
@@ -9,7 +9,8 @@
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
- config.serve_static_assets = false
+ config.serve_static_assets = true
+ config.static_cache_control = "public, max-age=2592000"
# Compress JavaScripts and CSS
config.assets.compress = true
@@ -41,6 +42,13 @@
# Use a different cache store in production
# config.cache_store = :mem_cache_store
+ config.cache_store = :dalli_store
+
+ config.action_dispatch.rack_cache = {
+ :metastore => Dalli::Client.new,
+ :entitystore => Dalli::Client.new,
+ :allow_reload => false
+ }
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
View
2  config/initializers/rack_worker.rb
@@ -0,0 +1,2 @@
+Rack::Worker.cache = Dalli::Client.new(nil, {:expires_in => 300})
+Rack::Worker.queue = QC
View
9 config/routes.rb
@@ -1,12 +1,17 @@
Gisted::Application.routes.draw do
-
root :to => 'homes#index'
+
+ # OAuth/session management
match '/login', to: 'sessions#login', :as => :login
match '/logout', to: 'sessions#logout', :as => :logout
match '/auth/github/callback', to: 'sessions#create', :via => [:post, :get], :as => :github_auth_callback
- resources :gists, :only => :index
+ resources :gists do
+ collection do
+ post 'refresh'
+ end
+ end
# The priority is based upon order of creation:
# first created -> highest priority.
View
8 db/migrate/20120928184857_create_users.rb
@@ -1,11 +1,13 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
- t.string :gh_username
+ t.integer :gh_id, :unique => true
+ t.string :gh_username, :gh_email, :gh_name
t.string :gh_oauth_token, :unique => true
- t.string :gh_avatar_url
- t.string :gh_url
+ t.string :gh_avatar_url, :gh_url
t.timestamps
end
+
+ add_index :users, :gh_id
end
end
View
13 db/migrate/20120928201432_add_queue_classic.rb
@@ -0,0 +1,13 @@
+require 'queue_classic'
+
+class AddQueueClassic < ActiveRecord::Migration
+
+ def self.up
+ QC::Setup.create
+ end
+
+ def self.down
+ QC::Setup.drop
+ end
+
+end
View
18 db/migrate/20121002204303_create_gists.rb
@@ -0,0 +1,18 @@
+class CreateGists < ActiveRecord::Migration
+ def change
+ create_table :gists do |t|
+ t.string :gh_id, :unique => true
+ t.integer :user_id
+ t.text :description
+ t.string :url, :git_pull_url, :git_push_url
+ t.boolean :public
+ t.integer :comment_count
+ t.timestamp :gh_created_at
+ t.timestamp :gh_updated_at
+ t.timestamps
+ end
+
+ add_index :gists, :gh_id
+ add_index :gists, :user_id
+ end
+end
View
14 db/migrate/20121002204505_create_gist_files.rb
@@ -0,0 +1,14 @@
+class CreateGistFiles < ActiveRecord::Migration
+ def change
+ create_table :gist_files do |t|
+ t.integer :gist_id
+ t.string :filename, :raw_url, :language, :file_type
+ t.text :content
+ t.integer :size_bytes
+ t.timestamps
+ end
+
+ add_index :gist_files, :gist_id
+ add_index :gist_files, :filename
+ end
+end
View
49 db/schema.rb
@@ -11,10 +11,55 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120928184857) do
+ActiveRecord::Schema.define(:version => 20121002204505) do
+
+ create_table "gist_files", :force => true do |t|
+ t.integer "gist_id"
+ t.string "filename"
+ t.string "raw_url"
+ t.string "language"
+ t.string "file_type"
+ t.text "content"
+ t.integer "size_bytes"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "gist_files", ["filename"], :name => "index_gist_files_on_filename"
+ add_index "gist_files", ["gist_id"], :name => "index_gist_files_on_gist_id"
+
+ create_table "gists", :force => true do |t|
+ t.string "gh_id"
+ t.integer "user_id"
+ t.text "description"
+ t.string "url"
+ t.string "git_pull_url"
+ t.string "git_push_url"
+ t.boolean "public"
+ t.integer "comment_count"
+ t.datetime "gh_created_at"
+ t.datetime "gh_updated_at"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "gists", ["gh_id"], :name => "index_gists_on_gh_id"
+ add_index "gists", ["user_id"], :name => "index_gists_on_user_id"
+
+ create_table "queue_classic_jobs", :force => true do |t|
+ t.string "q_name"
+ t.string "method"
+ t.text "args"
+ t.datetime "locked_at"
+ end
+
+ add_index "queue_classic_jobs", ["q_name", "id"], :name => "idx_qc_on_name_only_unlocked"
create_table "users", :force => true do |t|
+ t.integer "gh_id"
t.string "gh_username"
+ t.string "gh_email"
+ t.string "gh_name"
t.string "gh_oauth_token"
t.string "gh_avatar_url"
t.string "gh_url"
@@ -22,4 +67,6 @@
t.datetime "updated_at", :null => false
end
+ add_index "users", ["gh_id"], :name => "index_users_on_gh_id"
+
end

No commit comments for this range

Something went wrong with that request. Please try again.