Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Import gists/files

  • Loading branch information...
commit 95f567c720e31e23678ffa7cac4f0f3e18138222 1 parent 579a378
@rwdaigle authored
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
4 app/controllers/gists_controller.rb
@@ -5,11 +5,11 @@ class GistsController < ApplicationController
before_filter :force_user_login
def index
- @gists = [] #current_user.gh_client.gists
+ @gists = current_user.gists
end
def refresh
- puts QC.enqueue("GistFetcher.fetch", current_user.id)
+ 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
22 app/models/gist_fetcher.rb
@@ -2,11 +2,27 @@ class GistFetcher
class << self
- def fetch(user_id)
+ def fetch_user_gists(user_id)
user = User.find(user_id)
- user.gh_client.gists.each do |gh_gist|
- # Create local gist
+ 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
30 app/models/user.rb
@@ -1,24 +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)
- 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)
- end
- end
+ 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
+ }
- # Don't like this hear, but it's convenient for now
- def gh_client
- @gh_client ||= Octokit::Client.new(:login => gh_username, :oauth_token => gh_oauth_token)
+ 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
@@ -3,15 +3,15 @@
/ %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")}:"
+ = "#{pluralize(gist.files.count, "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
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
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
Please sign in to comment.
Something went wrong with that request. Please try again.