Consuming the GitHub API

class User < ActiveRecord::Base   def self.create_with_omniauth(auth) 
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.token = auth["credentials"]["token"]
user.nickname = auth["info"]["nickname"]
user.image = auth["extra"]["raw_info"]["avatar_url"]
end
end
end
class GithubService 
attr_reader :current_user
def initialize(current_user)
@current_user = current_user
@_connection = Faraday.new("https://api.github.com")
connection.headers['Authorization'] = "token #{current_user.token}"
end
def repos
get("/users/#{current_user.nickname}/repos")
end
private
def get(path)
JSON.parse(connection.get(path).body)
end
def connection
@_connection
end
end
$(document).ready(function(){ 
renderRepos();
});
var renderRepos = function() {
$.ajax({
url: '/repositories',
success: function(data) {
var items = [];
$.each(data, function(key, repository) {
items.push( "<p>" + repository.table.name + "</p>" );
})
showHide('#all_repos', '#repo-wheel', items);
}
});
}
var showHide = function(divId, wheelId, items) {
$(divId).html(items.join(""));
$(wheelId).addClass('hidden');
}
class RepositoriesController < ApplicationController 
def index
@repositories = Repository.all(current_user)
render json: @repositories
end
end
class Repository < OpenStruct 
def self.service(current_user)
GithubService.new(current_user)
end
def self.all(current_user)
repos_hash = service(current_user).repos
repos_hash.map do |repo_hash|
self.new(repo_hash)
end
end
end

--

--

Senior Software Engineer | www.adriennedomingus.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store