Created
December 11, 2013 14:20
Revisions
-
SabretWoW created this gist
Dec 11, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ # 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"