Skip to content

Instantly share code, notes, and snippets.

@SabretWoW
Created December 11, 2013 14:20
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save SabretWoW/7911188 to your computer and use it in GitHub Desktop.
Save SabretWoW/7911188 to your computer and use it in GitHub Desktop.
Ruby script that uses open-uri to fetch the contents of a JSON endpoint, uses the JSON gem to parse the string into a Ruby array & prints some of the records. This is the foundation for all web API requests, so feel free to use it in the future.
# http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
require 'open-uri'
# https://github.com/flori/json
require 'json'
# http://stackoverflow.com/questions/9008847/what-is-difference-between-p-and-pp
require 'pp'
# Construct the URL we'll be calling
request_uri = 'http://localhost:3000/users.json'
request_query = ''
url = "#{request_uri}#{request_query}"
# Actually fetch the contents of the remote URL as a String.
buffer = open(url).read
# Sample JSON response from fake API endpoint (a simple app running on my machine), but you can easily build it. Credit goes to Michael Hartl for the stellar "Build Twitter in Rails" tutorial:
#
# http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
#
# Output after running it through http://jsonviewer.net to make it more readable.
# ['0'] (
# | id = 2
# | name = "Example User"
# | email = "example@railstutorial.org"
# | created_at = "2013-12-11T06:09:06.866Z"
# | updated_at = "2013-12-11T06:09:06.866Z"
# # ... and more
# | )
# ['1'] (
# | id = 4
# | name = "Example User 2"
# | email = "example@railstutorial.org"
# | created_at = "2013-12-11T06:09:06.866Z"
# | updated_at = "2013-12-11T06:09:06.866Z"
# # ... and more
# | )
# Convert the String response into a plain old Ruby array. It is faster and saves you time compared to the standard Ruby libraries too.
result = JSON.parse(buffer)
# An example of how to take a random sample of elements from an array. Pass the number of elements you want into .sample() method. It's probably a better idea for the server to limit the results before sending, but you can use basic Ruby skills to trim & modify the data however you'd like.
result = result.sample(5)
# Loop through each of the elements in the 'result' Array & print some of their attributes.
result.each do |user|
puts "#{user['id']}\t#{user['name']}\t#{user['email']}"
puts "Registered: #{user['created_at']}\n\n"
end
# Expected output in this format:
# Uncomment the next line to output the data inside the 5 elements returned from the JSON call. Google 'Ruby pp gem'.
# pp result
# 14 Kari Lynch example-12@railstutorial.org
# Registered: 2013-12-11T06:09:08.231Z
# 20 Dr. Josh Bergstrom example-18@railstutorial.org
# Registered: 2013-12-11T06:09:08.788Z
# 24 Haley Walter example-22@railstutorial.org
# Registered: 2013-12-11T06:09:09.157Z
# 17 Ozella Barton example-15@railstutorial.org
# Registered: 2013-12-11T06:09:08.511Z
# 13 Mandy Reynolds example-11@railstutorial.org
# Registered: 2013-12-11T06:09:08.138Z
puts "Finished!\n\n"
@Epigene
Copy link

Epigene commented Apr 20, 2015

Useful snippet! A little point though - JSON.parse(buffer) returns a Ruby kev-value store commonly known as a hash, not an array. It had me a bit confused.

@samarthsikotara
Copy link

Amazing..! Thank you for sharing.

@davidboothe
Copy link

@Epigene, JSON.parse(buffer) returns an array of hashes.

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