Skip to content

Commit

Permalink
added basic error handeling for repos that are banned by gh for tos
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-benoit committed Oct 14, 2016
1 parent 9222729 commit 7f0be9d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem 'pg'
gem 'figaro'
gem 'jbuilder', '~> 2.0'
gem 'redis'
gem 'rest-client'

gem 'sass-rails'
gem 'jquery-rails'
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ GEM
coderay (1.1.1)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
domain_name (0.5.20160615)
unf (>= 0.0.5, < 1.0.0)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.14)
Expand All @@ -64,6 +66,8 @@ GEM
sass (>= 3.2)
globalid (0.3.7)
activesupport (>= 4.1.0)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.7.0)
jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1)
Expand All @@ -86,6 +90,7 @@ GEM
mini_portile2 (2.1.0)
minitest (5.9.1)
multi_json (1.12.1)
netrc (0.11.0)
nio4r (1.2.1)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
Expand Down Expand Up @@ -131,6 +136,10 @@ GEM
rb-inotify (0.9.7)
ffi (>= 0.5.0)
redis (3.3.1)
rest-client (2.0.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
sass (3.4.22)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
Expand Down Expand Up @@ -161,6 +170,9 @@ GEM
thread_safe (~> 0.1)
uglifier (3.0.2)
execjs (>= 0.3.0, < 3)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
websocket-driver (0.6.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
Expand All @@ -184,6 +196,7 @@ DEPENDENCIES
puma
rails (= 5.0.0.1)
redis
rest-client
sass-rails
simple_form
spring
Expand Down
26 changes: 22 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
class UsersController < ApplicationController
def show
input_user = params[:user]
access_token = "?access_token=" + ENV['github_token']
@user = JSON.parse(open("https://api.github.com/users/#{input_user}" + access_token).read)
user_repos = JSON.parse(open("https://api.github.com/users/#{input_user}/repos" + access_token).read)
access_token = '?access_token=' + ENV['github_token']

@user = JSON.parse RestClient::Request.execute(
method: :get,
url: "https://api.github.com/users/#{input_user}#{access_token}"
)

user_repos = JSON.parse RestClient::Request.execute(
method: :get,
url: "https://api.github.com/users/#{input_user}/repos#{access_token}"
)

user_lang = {}
total_bytes = 0
user_repos.delete_if { |repo| repo['fork'] == true } .each do |repo|
repo_lang = JSON.parse(open(repo['languages_url'] + access_token).read)
repo_lang = JSON.parse RestClient::Request.execute(
method: :get,
url: "#{repo['languages_url']}#{access_token}"
)
repo_lang.each do |lang, bytes|
user_lang[lang] ||= 0
user_lang[lang] += bytes
Expand All @@ -23,5 +35,11 @@ def show
lang_pair << percentage
end
end
rescue URI::InvalidURIError
{ 'error' => { 'message' => 'url error' } }
rescue RestClient::ResourceNotFound
{ 'error' => { 'message' => 'rest ressource error' } }
rescue RestClient::Forbidden
{ 'error' => { 'message' => 'forbidden' } }
end
end

0 comments on commit 7f0be9d

Please sign in to comment.