class NasaApod::Client
Attributes
api_key[RW]
date[R]
hd[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/nasa_apod/client.rb, line 12 def initialize(options = {}) self.api_key = options[:api_key] || 'DEMO_KEY' self.date = options[:date] self.hd = options[:hd] end
Public Instance Methods
date=(date)
click to toggle source
# File lib/nasa_apod/client.rb, line 8 def date=(date) @date = parse_date(date) end
random_post(options = {})
click to toggle source
# File lib/nasa_apod/client.rb, line 35 def random_post(options = {}) date = rand(Date.parse('1995-06-16')..Date.today) options[:date] = date search(options) end
Also aliased as: wormhole
search(options = {})
click to toggle source
Returns APOD info for specified day.
@see api.nasa.gov/api.html#apod @rate_limited Yes api.nasa.gov/api.html#authentication @image_permissions apod.nasa.gov/apod/lib/about_apod.html#srapply @authentication optional NASA api key api.nasa.gov/index.html#apply-for-an-api-key @option options [String] :api_key Optional. Uses DEMO_KEY as default. @option options [String] :date Optional. Returns the APOD results for the given date. Date should be formatted as YYYY-MM-DD. Defaults as today. @return [NasaApod::SearchResults] Return APOD post for a specified date.
# File lib/nasa_apod/client.rb, line 28 def search(options = {}) self.date = options[:date] || date self.hd = options[:hd] || hd response = HTTParty.get(DEFAULT_URL, query: attributes) handle_response(response) end
Private Instance Methods
attributes()
click to toggle source
# File lib/nasa_apod/client.rb, line 45 def attributes instance_variables.each_with_object({}) do |var, hash| hash[var.to_s.delete('@')] = instance_variable_get(var) end end
handle_response(response)
click to toggle source
# File lib/nasa_apod/client.rb, line 51 def handle_response(response) if response['error'].nil? NasaApod::SearchResults.new(response) else NasaApod::Error.new(response) end end
parse_date(date)
click to toggle source
# File lib/nasa_apod/client.rb, line 59 def parse_date(date) case date when Time date.strftime('%Y-%m-%d') when Date date.to_s when String date else Date.today.to_s end end