Class: NasaApod::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nasa_apod/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
# File 'lib/nasa_apod/client.rb', line 28

def initialize(options={})
  @api_key = options[:api_key] || "DEMO_KEY"
  self.date = options[:date]
  self.concept_tags = options[:concept_tags]
  self.hd = options[:hd]
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/nasa_apod/client.rb', line 6

def api_key
  @api_key
end

#concept_tagsObject

Returns the value of attribute concept_tags.



6
7
8
# File 'lib/nasa_apod/client.rb', line 6

def concept_tags
  @concept_tags
end

#dateObject

Returns the value of attribute date.



6
7
8
# File 'lib/nasa_apod/client.rb', line 6

def date
  @date
end

#hdObject

Returns the value of attribute hd.



6
7
8
# File 'lib/nasa_apod/client.rb', line 6

def hd
  @hd
end

Instance Method Details

#attributesObject



60
61
62
63
64
# File 'lib/nasa_apod/client.rb', line 60

def attributes
  instance_variables.each_with_object(Hash.new) do  |var,hash| 
    hash[var.to_s.delete("@")] = instance_variable_get(var)
  end
end

#random_post(options = {}) ⇒ Object Also known as: wormhole



53
54
55
56
# File 'lib/nasa_apod/client.rb', line 53

def random_post(options={})
  date = rand(Date.parse("1995-06-16")..Date.today)
  search(date: date)
end

#search(options = {}) ⇒ NasaApod::SearchResults

Returns APOD info for specified day.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :api_key (String)

    Optional. Uses DEMO_KEY as default.

  • :date (String)

    Optional. Returns the APOD results for the given date. Date should be formatted as YYYY-MM-DD. Defaults as today.

  • :concept_tags (Boolean)

    Optional. Returns an array of concept tags if available. Defaults to False.

Returns:

See Also:



45
46
47
48
49
50
51
# File 'lib/nasa_apod/client.rb', line 45

def search(options={})
  self.date = options[:date] || date
  @concept_tags = options[:concept_tags] || concept_tags
  @hd = options[:hd] || hd
  response = HTTParty.get(DEFAULT_URL, { query: attributes })
  handle_response(response)
end