syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
How to get started with Github API?

I use this post to get students started with the Github REST API.

Google is a good place to start with the Github REST API:

https://www.google.com/search?q=github+REST+API

Next, I like to point students to this doc-link:

https://developer.github.com/v3/repos/#list-user-repositories

The above API call has a simple syntax:

GET /users/:username/repos

I can call it with my browser by clicking on the URL below:

https://api.github.com/users/danbikle/repos

My browser will send a GET request and then deal with the response from api.github.com

Then, my browser will display a syntax called JSON.

JSON syntax is easy to read.

If I see this:

[1,2,2,3,4]

I am looking at at JSON array.

This is a JSON object:

{"hello": "world", "life": "Is Good"}

When I look at the syntax from

https://api.github.com/users/danbikle/repos

I see an array of objects.

Also I see that some of the objects are nested within parent objects.

After I understand JSON, the next step is to connect my software to the API and then parse the JSON.

I can do this using Ruby.

For this mini-project, I install Ubuntu 14.04 on my laptop.

Then I install some packages inside Ubuntu using the shell commands listed below:

apt-get install autoconf bison build-essential libssl-dev libyaml-dev \
libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3       \
libgdbm-dev libsqlite3-dev gitk postgresql postgresql-server-dev-all  \
libpq-dev emacs wget curl chromium-browser openssh-server

apt-get update apt-get upgrade shutdown -r now login


Then I install a recent version of Ruby:
cd ~
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
vi .bashrc
bash
ls -la ~/.rbenv/plugins/ruby-build/share/ruby-build/
rbenv install 2.2.2
rbenv global  2.2.2


Next, I create a folder for my API script:
mkdir ~/github_api_ruby


Then I start populating it:
cd ~/github_api_ruby
rbenv local 2.2.2
gem install bundler
gem install rest-client
gem install json
echo "gem  'rest-client'" > Gemfile
echo "gem  'json'"       >> Gemfile
bundle install


Next I write a simple script to parse JSON served to me via the API:
# ~/github_api_ruby/gar.rb

# This script should connect to the Github API and parse JSON from it.
require 'rest-client'
require 'json'

ghrsp       = RestClient.get "https://api.github.com/users/danbikle/repos"
parsed_json = JSON.parse(ghrsp.to_str)

# I should convert parsed_json into a Ruby array:
myarray = parsed_json.map{|repo| repo['full_name']}

myarray.each{|repo| p repo}

# I should look at a different user and sort his repos by star-count:

ghrsp       = RestClient.get "https://api.github.com/users/ab/repos"
parsed_json = JSON.parse(ghrsp.to_str)

# I should convert parsed_json into a Ruby array:
myarray = parsed_json.map{|repo| 
      {'star_count' => Integer(repo['stargazers_count']),
       'full_name' => repo['full_name']}}
my_sortedarray = myarray.sort_by{|hsh| hsh['star_count']}

my_sortedarray.each{|repo| p repo}

# done


Screen dump:
dan@ann15:~/github_api_ruby $ 
dan@ann15:~/github_api_ruby $ 
dan@ann15:~/github_api_ruby $ ruby gar.rb
"danbikle/aispy"
"danbikle/ang10"
"danbikle/danstar"
"danbikle/fx4us"
"danbikle/h2o-dev"
"danbikle/h2o-droplets"
"danbikle/hackerdojo-shout"
"danbikle/hdLeak"
"danbikle/heroku-buildpack-ruby"
"danbikle/heroku_angular_phonecat"
"danbikle/heuristics"
"danbikle/home_dan_ssqlite"
"danbikle/ibjts"
"danbikle/jae"
"danbikle/MADlib_preso"
"danbikle/ml101"
"danbikle/node-ibapi-addon"
"danbikle/pd101"
"danbikle/rbm10"
"danbikle/sk101"
"danbikle/sparkling-water"
"danbikle/split_adjuster"
"danbikle/spy4us"
"danbikle/spy4usBig"
"danbikle/spy611scripts"
"danbikle/stk_mkt_madlib_logregr"
"danbikle/trade-manager"
{"star_count"=>0, "full_name"=>"ab/2048"}
{"star_count"=>0, "full_name"=>"ab/BartRunnerAndroid"}
{"star_count"=>0, "full_name"=>"ab/bashttpd"}
{"star_count"=>0, "full_name"=>"ab/bcfg2"}
{"star_count"=>0, "full_name"=>"ab/ciphers"}
{"star_count"=>0, "full_name"=>"ab/cookie_extractor"}
{"star_count"=>0, "full_name"=>"ab/easy-rsa"}
{"star_count"=>0, "full_name"=>"ab/ec2instances.info"}
{"star_count"=>0, "full_name"=>"ab/feedprefixer"}
{"star_count"=>0, "full_name"=>"ab/friendly_mime"}
{"star_count"=>0, "full_name"=>"ab/howsmyssl"}
{"star_count"=>0, "full_name"=>"ab/http-cookie"}
{"star_count"=>1, "full_name"=>"ab/aws-sdk-ruby"}
{"star_count"=>1, "full_name"=>"ab/azilink"}
{"star_count"=>1, "full_name"=>"ab/bcfg2-old"}
{"star_count"=>1, "full_name"=>"ab/blag"}
{"star_count"=>1, "full_name"=>"ab/closetag.vim"}
{"star_count"=>1, "full_name"=>"ab/elb"}
{"star_count"=>1, "full_name"=>"ab/eventmachine"}
{"star_count"=>1, "full_name"=>"ab/gapps"}
{"star_count"=>1, "full_name"=>"ab/github-default-git"}
{"star_count"=>1, "full_name"=>"ab/IAMCli"}
{"star_count"=>2, "full_name"=>"ab/aws-owl"}
{"star_count"=>2, "full_name"=>"ab/gitbook"}
{"star_count"=>3, "full_name"=>"ab/Auto-Brightness-Switch"}
{"star_count"=>3, "full_name"=>"ab/crackedin"}
{"star_count"=>4, "full_name"=>"ab/dotfiles"}
{"star_count"=>4, "full_name"=>"ab/ec2-ip"}
{"star_count"=>5, "full_name"=>"ab/bin"}
{"star_count"=>7, "full_name"=>"ab/AWS-Audit"}
dan@ann15:~/github_api_ruby $ 
dan@ann15:~/github_api_ruby $ 
dan@ann15:~/github_api_ruby $ 


So, the rest-client Ruby-Gem is a good way to get programmatic control over JSON served by the Github API.


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me