Created
December 11, 2013 14:20
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 = "[email protected]" | |
# | 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 = "[email protected]" | |
# | 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 [email protected] | |
# Registered: 2013-12-11T06:09:08.231Z | |
# 20 Dr. Josh Bergstrom [email protected] | |
# Registered: 2013-12-11T06:09:08.788Z | |
# 24 Haley Walter [email protected] | |
# Registered: 2013-12-11T06:09:09.157Z | |
# 17 Ozella Barton [email protected] | |
# Registered: 2013-12-11T06:09:08.511Z | |
# 13 Mandy Reynolds [email protected] | |
# Registered: 2013-12-11T06:09:08.138Z | |
puts "Finished!\n\n" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.