Permalink
Browse files

Add support for fetching starred gists

  • Loading branch information...
1 parent cfe17d9 commit da36b088cf48ee7a19af2ba682e0cb887f6502a8 @rwdaigle committed Nov 21, 2012
Showing with 51 additions and 12 deletions.
  1. +8 −8 app/models/gist.rb
  2. +20 −1 app/models/gist_fetcher.rb
  3. +16 −0 db/migrate/20121121002434_add_starred_gist_support.rb
  4. +7 −3 db/schema.rb
View
16 app/models/gist.rb
@@ -2,8 +2,8 @@ class Gist < ActiveRecord::Base
include Tire::Model::Search
- attr_accessible :gh_id, :user_id, :description, :url, :git_pull_url, :git_push_url, :public,
- :comment_count, :gh_created_at, :gh_updated_at
+ attr_accessible :gh_id, :user_id, :owner_gh_id, :owner_gh_username, :owner_gh_avatar_url, :description, :url, :git_pull_url, :git_push_url, :public,
+ :comment_count, :gh_created_at, :gh_updated_at, :starred
belongs_to :user
has_many :files, :class_name => 'GistFile', :dependent => :delete_all
@@ -24,17 +24,17 @@ class Gist < ActiveRecord::Base
class << self
- def import(gh_gist)
+ def import(user_id, gh_gist, overrides = {})
- user = User.where(gh_id: gh_gist.user.id).first
+ user = User.find(user_id)
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_id: gh_id, user_id: user.id, owner_gh_id: gh_gist.user['id'], owner_gh_username: gh_gist.user.login, owner_gh_avatar_url: gh_gist.user.avatar_url,
+ 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
- }
+ }.merge(overrides)
if(existing_gist = where(gh_id: gh_id).first)
log({ns: self, fn: __method__, measure: true, at: 'gist-imported'}, user, existing_gist)
View
21 app/models/gist_fetcher.rb
@@ -8,18 +8,37 @@ def fetch
log({ns: self, fn: __method__}, since: since) do
User.last_fetched_before(since).active_auth.pluck(:id).each do |user_id|
QC.enqueue("GistFetcher.fetch_gists", user_id)
+ QC.enqueue("GistFetcher.fetch_starred_gists", user_id)
end
end
end
+ # TODO: lot of dup w/ fetch_starred_gists
def fetch_gists(user_id)
user = User.find(user_id)
gh_client(user) do |gh|
log({ns: self, fn: __method__, measure: true}, user) do
gh.gists(nil, since: (user.last_gh_fetch ? user.last_gh_fetch.iso8601.to_s : nil)).each do |gh_gist|
- Gist.import(gh_gist)
+ Gist.import(user_id, gh_gist)
+ QC.enqueue("GistFetcher.fetch_gist_files", user_id, gh_gist.id)
+ end
+ end
+ QC.enqueue("User.refresh_index", user_id)
+ end
+
+ user.fetched! # If gist imports fail, this could cause gaps in updated gists...
+ end
+
+ def fetch_starred_gists(user_id)
+
+ user = User.find(user_id)
+
+ gh_client(user) do |gh|
+ log({ns: self, fn: __method__, measure: true}, user) do
+ gh.starred_gists(since: (user.last_gh_fetch ? user.last_gh_fetch.iso8601.to_s : nil)).each do |gh_gist|
+ Gist.import(user_id, gh_gist, starred: true)
QC.enqueue("GistFetcher.fetch_gist_files", user_id, gh_gist.id)
end
end
View
16 db/migrate/20121121002434_add_starred_gist_support.rb
@@ -0,0 +1,16 @@
+class AddStarredGistSupport < ActiveRecord::Migration
+
+ def up
+ change_table :gists do |t|
+ t.string :owner_gh_id, :owner_gh_username, :owner_gh_avatar_url
+ t.boolean :starred
+ end
+ end
+
+ def down
+ remove_column :gists, :owner_gh_id
+ remove_column :gists, :owner_gh_username
+ remove_column :gists, :owner_gh_avatar_url
+ remove_column :gists, :starred
+ end
+end
View
10 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121107020238) do
+ActiveRecord::Schema.define(:version => 20121121002434) do
create_table "gist_files", :force => true do |t|
t.integer "gist_id"
@@ -39,8 +39,12 @@
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
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "owner_gh_id"
+ t.string "owner_gh_username"
+ t.string "owner_gh_avatar_url"
+ t.boolean "starred"
end
add_index "gists", ["gh_id"], :name => "index_gists_on_gh_id"

0 comments on commit da36b08

Please sign in to comment.