Last active
August 29, 2015 14:15
-
-
Save net/b43237844b8715448b11 to your computer and use it in GitHub Desktop.
Astro-Phys.com API in Ruby
This file contains hidden or 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
require 'open-uri' | |
require 'json' # For parsing the response into a hash | |
# As defined here http://www.astro-phys.com/api | |
call = 'coefficients' # note: '/api/de406/' is not needed | |
parameters = ['bodies=sun'] # e.g. ['bodies=venus', 'type=m'] | |
# Converts parameters to query string | |
unless parameters.empty? | |
parameters = parameters.join("&") | |
parameters.insert(0, '?') | |
end | |
# Opens connection to server | |
query = open("http://www.astro-phys.com/api/de406/#{call}#{parameters unless parameters.empty?}") | |
# Reads the response (note: I belive this only reads one line, however as the response is json this | |
# should be ok) | |
response = query.read | |
query.close # Closes the connection | |
response = JSON.parse(response) # Parses the response | |
puts response['results'] # Random test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment