Skip to content

Commit

Permalink
added show design a bit more and changed the rest client to curb for …
Browse files Browse the repository at this point in the history
…a performance of exactly 0.0ms ...
  • Loading branch information
alex-benoit committed Oct 15, 2016
1 parent b1b8ce1 commit 058d2c4
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem 'pg'
gem 'figaro'
gem 'jbuilder', '~> 2.0'
gem 'redis'
gem 'rest-client'
gem 'curb'

gem 'sass-rails'
gem 'jquery-rails'
Expand Down
15 changes: 2 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ GEM
byebug (9.0.6)
coderay (1.1.1)
concurrent-ruby (1.0.2)
curb (0.9.3)
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 @@ -66,8 +65,6 @@ 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 @@ -90,7 +87,6 @@ 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 @@ -136,10 +132,6 @@ 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 @@ -170,9 +162,6 @@ 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 @@ -185,6 +174,7 @@ DEPENDENCIES
better_errors
binding_of_caller
bootstrap-sass
curb
figaro
font-awesome-sass
jbuilder (~> 2.0)
Expand All @@ -196,7 +186,6 @@ DEPENDENCIES
puma
rails (= 5.0.0.1)
redis
rest-client
sass-rails
simple_form
spring
Expand Down
10 changes: 8 additions & 2 deletions app/assets/stylesheets/pages/_show.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
// margin-left: auto;
}

img {
width: 150px;
height: auto;
}

table {
width: 100%;
width: calc(100% - 20px);
margin: 0 10px;

.left-align {
text-align: left;
Expand All @@ -17,7 +23,7 @@

h1 {
margin-top: 60px;
margin-bottom: 50px;
margin-bottom: 40px;
position: relative;

&:before, &:after{
Expand Down
51 changes: 5 additions & 46 deletions app/controllers/users_controller.rb
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
54 changes: 54 additions & 0 deletions app/services/github_client.rb
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 removed app/services/github_repo_client.rb
Empty file.
83 changes: 50 additions & 33 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
<div id="show" class="container">
<div class="row text-center">
<h1>
<%= @user["name"] ? @user["name"] : @user["login"] %>'s Profile
</h1>
</div>
<div class="row">
<div class="col-xs-12 col-md-10 col-md-offset-1 text-center">
<h1>
<%= @user["name"] ? @user["name"] : @user["login"] %>'s Languages
</h1>
<div class="row">
<div class="col-xs-12 col-md-6">
<table>
<thead>
<tr>
<th class='left-align'>Language</th>
<th class='right-align'>Characters</th>
<th class='right-align'>Lines (approx)</th>
<th class='right-align'>Percentage</th>
</tr>
</thead>
<tbody>
<% @sorted_languages.each do |lang_trio| %>
<tr>
<td class='left-align'><%= lang_trio[0] %></td>
<td class='right-align'><%= "#{lang_trio[1]} characters" %></td>
<td class='right-align'><%= "~#{lang_trio[1]/25} lines" %></td>
<td class='right-align'><%= "#{lang_trio[2].round(2)}%" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="col-xs-12 col-md-6">
<div id="donut"></div>
</div>
</div>
<%= link_to "Back", root_path, class: "btn btn-default" %>
<div class="col-xs-12 col-md-4">
<h3 class="text-center">Profile</h3>
<%= image_tag "#{@user["avatar_url"]}" %>
<ul>
<li>Name: <%= @user["name"] %></li>
<li>Company: <%= @user["company"] %></li>
<li>Email: <a href="mailto:<%= @user["email"] %>"> <%= @user["email"] %> </a></li>
<li>Website: <a href="<%= @user["blog"] %>"> <%= @user["blog"].chomp('/').reverse.chomp('//:ptth').chomp('//:sptth').chomp('.www').reverse %> </a></li>
<li>Joined: <%= Date.parse(@user["created_at"]).strftime('%B %Y') %></li>
<li>Public Repos: <%= @user["public_repos"] %></li>
<li>Followers: <%= @user["followers"] %></li>
<li>Following: <%= @user["following"] %></li>
<li>Collected Stars: <%= @total_stars %></li>
</ul>
</div>
<div class="col-xs-12 col-md-4">
<h3 class="text-center">Languages</h3>
<table>
<thead>
<tr>
<th class='left-align'>Language</th>
<!-- <th class='right-align'>Characters</th> -->
<th class='right-align'>Lines (approx)</th>
<th class='right-align'>Percentage</th>
</tr>
</thead>
<tbody>
<% @sorted_languages.each do |lang_trio| %>
<tr>
<td class='left-align'><%= lang_trio[0] %></td>
<!-- <td class='right-align'><%# "#{lang_trio[1]} chars" %></td> -->
<td class='right-align'><%= "~#{number_with_delimiter(lang_trio[2])} lines" %></td>
<td class='right-align'><%= "#{lang_trio[3]}%" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="col-xs-12 col-md-4">
<h3 class="text-center">Distribution</h3>
<div id="donut"></div>
</div>
</div>
<div class="row text-center">
<%= link_to "Back", root_path, class: "btn btn-default" %>
</div>
</div>

Expand All @@ -43,7 +60,7 @@
element: 'donut',
data: [
<% @sorted_languages.each do |lang_trio| %>
{ label: '<%= lang_trio[0] %>', value: <%= (lang_trio[2]*10).ceil/10.0 %> },
{ label: '<%= lang_trio[0] %>', value: <%= lang_trio[3] %> },
<% end %>
],
formatter: function (y, data) { return y+'%' },
Expand Down

0 comments on commit 058d2c4

Please sign in to comment.