Consuming the GitHub API

OAuth

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

Making API Calls

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

Presenting the Data

  • When the data was ready, hide the spinning wheel that was in the section of the page where the data belonged, and access the information at the route specified in the AJAX call:
$(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');
}
  • Found at the “/repositories” route was this:
class RepositoriesController < ApplicationController 
def index
@repositories = Repository.all(current_user)
render json: @repositories
end
end
  • Calling Repository.all accessed the Repository PORO (not an ActiveRecord model), which looked like this:
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
  • And that brings us full circle back to the service and the Faraday call shown above!

Other Features

Improvements

  • Caching: Even with AJAX calls, the site loaded pretty slowly. Since there was one landing page for everything beyond the gist functionality, it required quite a few API calls, as well as the Nokogiri scraping. I need to think more about the balance between performance, and being up to date — the information on someone’s GitHub page changes pretty frequently.
  • Background workers: for the reason mentioned above. I don’t know much about how background workers work, but I would like to read up on it.
  • Move away from the use of OpenStructs to POROs with only the attributes I actually need, that I define.
  • Updated my AJAX calls to use the $.getJSON method. I’m glad I did it the way I did this time around so I have a better understanding of what is happening (and to practice my JavaScript!), but the jQuery route would be cleaner.

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
Adrienne Domingus

Adrienne Domingus

Senior Software Engineer | www.adriennedomingus.com

More from Medium

Ruby 3 on Rails 7: RESTful JSON API

5 Useful Git Commands for Devs

Basics of Cross Site Request Forgery (CSRF), and ways to prevent it in NodeJs and Ruby on Rails

Building a Ruby CLI app