Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fetch Gists from the API
require 'net/http'
require 'json'
# a simple wrapper to do an HTTP GET
def fetch_uri(uri)
# GETs the URI and returns a Ruby hash.
def fetch_json(uri)
result = fetch_uri(uri)
# Gets the next uri via pagination in headers
def next_uri(uri)
result = fetch_uri(uri)
links = result.to_hash['link']
# stupid parsing. We'd use something real here in
# production
link_array = links.first.split(",")
link_array.collect! do |string|
string =~ /\<(.*)\>; rel="(.*)"/
[$2, $1]
# omg hax! I don't want to deal with templates, so
# let's punt on that. You'd use a real URI templates
# parser in real code.
def parse_uri_template(uri)
# Let's begin!
json = fetch_json('')
base_uri = parse_uri_template(json["gists_url"])
next_uri = next_uri(base_uri)
json = fetch_json(next_uri)
first_entry = json[0]
puts "DATA:"
puts "ID:\t#{first_entry["id"]}"
puts "URL:\t#{first_entry["url"]}"
puts "USER:\t#{first_entry["user"]["login"]}"

This comment has been minimized.

Copy link

@glebm glebm commented Mar 31, 2013

had to modify for https to work


This comment has been minimized.

Copy link
Owner Author

@steveklabnik steveklabnik commented Apr 1, 2013

oh? This worked for me, unmodified. weird.


This comment has been minimized.

Copy link

@glebm glebm commented Apr 1, 2013

I'm getting 400 The plain HTTP request was sent to HTTPS port with this code on ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.2.1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment