diff --git a/Gemfile b/Gemfile index 8fa280a..f94922e 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ gem 'pg' gem 'figaro' gem 'jbuilder', '~> 2.0' gem 'redis' +gem 'rest-client' gem 'sass-rails' gem 'jquery-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 797ba03..396d77b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -184,6 +196,7 @@ DEPENDENCIES puma rails (= 5.0.0.1) redis + rest-client sass-rails simple_form spring diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 24d15e5..5da4d7b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 @@ -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