Permalink
Browse files

Fetch file contents using individual Gist API call

  • Loading branch information...
1 parent 56014a9 commit 6d2d5944d18ec46cacede78d4da9b8341a1d17c8 @rwdaigle committed Oct 3, 2012
Showing with 37 additions and 39 deletions.
  1. +1 −1 app/controllers/gists_controller.rb
  2. +1 −15 app/models/gist.rb
  3. +17 −10 app/models/gist_fetcher.rb
  4. +18 −13 app/models/gist_file.rb
@@ -9,7 +9,7 @@ def index
end
def refresh
- QC.enqueue("GistFetcher.fetch_user_gists", current_user.id)
+ QC.enqueue("GistFetcher.fetch", current_user.id)
redirect_to gists_path
end
end
View
@@ -8,14 +8,6 @@ class Gist < ActiveRecord::Base
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']
@@ -31,14 +23,8 @@ def import_gist(gh_gist)
existing_gist.update_attributes(attributes)
existing_gist
else
- Gist.create(attributes)
+ 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
@@ -2,21 +2,28 @@ class GistFetcher
class << self
- def fetch_user_gists(user_id)
+ def fetch(user_id)
user = User.find(user_id)
- gh_client(user).gists.each do |gh_gist|
- # TODO add Scrolls logging
+ gh = gh_client(user)
+ fetch_gists(gh, user)
+ fetch_files(gh, user)
+ end
+
+ protected
+
+ # Make sure have all gist stubs imported
+ def fetch_gists(gh, user)
+ gh.gists.each do |gh_gist|
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
+ # Fetch individual gists from API to get file contents
+ def fetch_files(gh, user)
+ user.gists.pluck(:gh_id).each do |gh_gist_id|
+ GistFile.import(gh.gist(gh_gist_id))
+ end
+ end
private
View
@@ -6,19 +6,24 @@ class GistFile < ActiveRecord::Base
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)
+ def import(gh_gist)
+
+ gist = Gist.where(gh_id: gh_gist['id']).first
+
+ gh_gist.files.each do |filename, 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

0 comments on commit 6d2d594

Please sign in to comment.