-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added show design a bit more and changed the rest client to curb for …
…a performance of exactly 0.0ms ...
- Loading branch information
1 parent
b1b8ce1
commit 058d2c4
Showing
8 changed files
with
120 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,11 @@ | ||
require 'json' | ||
require 'open-uri' | ||
|
||
# User Controller | ||
class UsersController < ApplicationController | ||
def show | ||
input_user = params[:user] | ||
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 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 | ||
total_bytes += bytes | ||
end | ||
@sorted_languages = user_lang.sort_by { |_lang, bytes| -bytes } | ||
@sorted_languages.each do |lang_pair| | ||
percentage = lang_pair[1] * 100.0 / total_bytes | ||
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 | ||
|
||
private | ||
|
||
def round_languages (nb_lines) | ||
x = Math.log10(nb_lines).floor - 1 | ||
(nb_lines / (10.0**x)).round * 10**x | ||
client = GithubClient.new | ||
@user = client.find_user_info(input_user) | ||
all_repos_info = client.find_repos_info(input_user) | ||
@sorted_languages = all_repos_info[0] | ||
@total_stars = all_repos_info[1] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Class used for GitHub API calls in User Controller | ||
class GithubClient | ||
# Return {user-gh-profile-info} | ||
def find_user_info(input_user) | ||
base_url = "https://api.github.com/users/#{input_user}?access_token=" | ||
return JSON.parse Curl::Easy.perform(base_url + ENV['github_token']) { |curl| curl.headers['User-Agent'] = 'github-languages' }.body_str | ||
rescue => e | ||
puts e | ||
end | ||
|
||
# Returns [ [array-of-languages-and-info] , number-of-total-stars ] | ||
def find_repos_info(input_user) | ||
base_url = "https://api.github.com/users/#{input_user}/repos?access_token=" | ||
user_repos = JSON.parse Curl::Easy.perform(base_url + ENV['github_token']) { |curl| curl.headers['User-Agent'] = 'github-languages' }.body_str | ||
repos_info = [] | ||
user_lang = {} | ||
total_stars = 0 | ||
total_bytes = 0 | ||
user_repos.delete_if { |repo| repo['fork'] == true }.each do |repo| | ||
total_stars += repo['stargazers_count'] | ||
repo_lang = JSON.parse Curl::Easy.perform("#{repo['languages_url']}?access_token=" + ENV['github_token']) { |curl| curl.headers['User-Agent'] = 'github-languages' }.body_str | ||
repo_lang.each do |lang, bytes| | ||
user_lang[lang] ||= 0 | ||
user_lang[lang] += bytes | ||
total_bytes += bytes | ||
end | ||
end | ||
sorted_languages = user_lang.sort_by { |_lang, bytes| -bytes } | ||
sorted_languages.each do |lang_info| | ||
lang_info[1] = dynamic_round(lang_info[1]) # Round number of characters | ||
lang_info << dynamic_round(lang_info[1] / 25) # Lines of code | ||
raw_percentage = (lang_info[1] * 100.0 / total_bytes) | ||
lang_info << (raw_percentage.between?(0, 0.05) ? 0.1 : raw_percentage.round(1)) # Percentage of usage | ||
end | ||
repos_info << sorted_languages | ||
repos_info << total_stars | ||
rescue => e | ||
puts e | ||
end | ||
|
||
private | ||
|
||
def dynamic_round(nb_lines) | ||
case nb_lines | ||
when 0..4 | ||
return 5 | ||
when 5..100 | ||
nb_lines.round(-1) | ||
else | ||
x = Math.log10(nb_lines).floor - 1 | ||
(nb_lines / (10.0**x)).round * 10**x | ||
end | ||
end | ||
end |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters