Skip to content

Instantly share code, notes, and snippets.

@net
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save net/b43237844b8715448b11 to your computer and use it in GitHub Desktop.
Save net/b43237844b8715448b11 to your computer and use it in GitHub Desktop.
Astro-Phys.com API in Ruby
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