Skip to content

Commit

Permalink
Log all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
rwdaigle committed Oct 5, 2012
1 parent 0a0a9f7 commit cf5d3e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
15 changes: 12 additions & 3 deletions app/models/gist.rb
Expand Up @@ -34,10 +34,13 @@ def import(gh_gist)
}

if(existing_gist = where(gh_id: gh_id).first)
log({ns: self, fn: __method__}, user, existing_gist)
existing_gist.update_attributes(attributes)
existing_gist
else
create(attributes)
new_gist = create(attributes)
log({ns: self, fn: __method__}, user, new_gist)
new_gist
end
end

Expand All @@ -52,11 +55,17 @@ def search(q)
end

def reindex
find_each { |gist| gist.update_index }
tire.index.refresh
log({ns: self, fn: __method__}) do
find_each { |gist| gist.update_index }
tire.index.refresh
end
end
end

def to_log
{ gist_id: id, gist_gh_id: gh_id, gist_description: description }
end

# Required for Tire/Elasticsearch
def to_indexed_json
indexed_attributes.to_json
Expand Down
26 changes: 17 additions & 9 deletions app/models/gist_fetcher.rb
Expand Up @@ -4,30 +4,38 @@ class << self

def fetch(user_id)
user = User.find(user_id)
gh = gh_client(user)
fetch_gists(gh, user)
fetch_files(gh, user)
update_search_indices(user)
log({ns: self, fn: __method__}, user) do
gh = gh_client(user)
fetch_gists(gh, user)
fetch_files(gh, user)
update_search_indices(user)
end
end

protected

# Make sure have all gist stubs imported
def fetch_gists(gh, user)
gh.gists.each do |gh_gist|
Gist.import(gh_gist)
log({ns: self, fn: __method__}, user) do
gh.gists.each do |gh_gist|
Gist.import(gh_gist)
end
end
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))
log({ns: self, fn: __method__}, user) do
user.gists.pluck(:gh_id).each do |gh_gist_id|
GistFile.import(gh.gist(gh_gist_id))
end
end
end

def update_search_indices(user)
user.gists.each { |gist| gist.update_index }
log({ns: self, fn: __method__}, user) do
user.gists.each { |gist| gist.update_index }
end
end

private
Expand Down
9 changes: 8 additions & 1 deletion app/models/gist_file.rb
Expand Up @@ -19,15 +19,22 @@ def import(gh_gist)
}

if(existing_file = where(gist_id: gist.id, filename: gh_file.filename).first)
log({ns: self, fn: __method__}, gist, existing_file)
existing_file.update_attributes(attributes)
existing_file
else
create(attributes)
new_file = create(attributes)
log({ns: self, fn: __method__}, gist, new_file)
new_file
end
end
end
end

def to_log
{ file_id: id, file_filename: filename }
end

def indexed_attributes
{
filename: filename,
Expand Down
9 changes: 8 additions & 1 deletion app/models/user.rb
Expand Up @@ -14,11 +14,18 @@ def authenticate(auth)
}

if(existing_user = User.where(gh_id: auth.uid).first)
log({ns: self, fn: __method__}, existing_user)
existing_user.update_attributes(attributes)
existing_user
else
User.create(attributes)
new_user = User.create(attributes)
log({ns: self, fn: __method__}, new_user)
new_user
end
end
end

def to_log
{ user: gh_username, user_id: id, user_email: gh_email }
end
end

0 comments on commit cf5d3e9

Please sign in to comment.