Class: GithubUser

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/github_user.rb

Instance Method Summary collapse

Instance Method Details

#async_syncObject


61
62
63
# File 'app/models/github_user.rb', line 61

def async_sync
  GithubUpdateUserWorker.perform_async(self.)
end

#avatar_url(size = 60) ⇒ Object


33
34
35
# File 'app/models/github_user.rb', line 33

def avatar_url(size = 60)
  "https://avatars.githubusercontent.com/u/#{github_id}?size=#{size}"
end

#descriptionObject


53
54
55
# File 'app/models/github_user.rb', line 53

def description
  nil
end

#download_from_githubObject


72
73
74
75
76
# File 'app/models/github_user.rb', line 72

def download_from_github
  update_attributes(github_client.user(github_id).to_hash.slice(:login, :name, :company, :blog, :location))
rescue Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
  nil
end

#download_orgsObject


78
79
80
81
82
83
84
85
# File 'app/models/github_user.rb', line 78

def download_orgs
  github_client.orgs().each do |org|
    GithubCreateOrgWorker.perform_async(org.)
  end
  true
rescue Octokit::Unauthorized, Octokit::UnprocessableEntity, Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
  nil
end

#download_reposObject


87
88
89
90
91
92
93
94
95
# File 'app/models/github_user.rb', line 87

def download_repos
  AuthToken.client.search_repos("user:#{login}").items.each do |repo|
    GithubRepository.create_from_hash repo.to_hash
  end

  true
rescue Octokit::Unauthorized, Octokit::UnprocessableEntity, Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
  nil
end

#github_clientObject


57
58
59
# File 'app/models/github_user.rb', line 57

def github_client
  AuthToken.client
end

#github_urlObject


41
42
43
# File 'app/models/github_user.rb', line 41

def github_url
  "https://github.com/#{login}"
end

#open_source_contributionsObject


19
20
21
# File 'app/models/github_user.rb', line 19

def open_source_contributions
  github_contributions.joins(:github_repository).where("github_repositories.fork = ? AND github_repositories.private = ?", false, false)
end

#org?Boolean


37
38
39
# File 'app/models/github_user.rb', line 37

def org?
  false
end

#syncObject


65
66
67
68
69
70
# File 'app/models/github_user.rb', line 65

def sync
  download_from_github
  download_orgs
  download_repos
  update_attributes(last_synced_at: Time.now)
end

#to_paramObject


49
50
51
# File 'app/models/github_user.rb', line 49

def to_param
  
end

#to_sObject


45
46
47
# File 'app/models/github_user.rb', line 45

def to_s
  name.presence || 
end

#top_favourite_project_idsObject


27
28
29
30
31
# File 'app/models/github_user.rb', line 27

def top_favourite_project_ids
  Rails.cache.fetch "user:#{self.id}:top_favourite_project_ids:v2", :expires_in => 1.week do
    favourite_projects.limit(10).pluck(:id)
  end
end

#top_favourite_projectsObject


23
24
25
# File 'app/models/github_user.rb', line 23

def top_favourite_projects
  Project.where(id: top_favourite_project_ids).maintained.order("position(','||projects.id::text||',' in '#{top_favourite_project_ids.join(',')}')")
end