Class: NasaApi::NasaInit

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

Direct Known Subclasses

Mars, Neo, Planetary, Tech

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NasaInit

Returns a new instance of NasaInit.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nasa_api.rb', line 20

def initialize(options = {})
  @api_key = options[:api_key] || 'DEMO_KEY'

  # When testing with RSpec, uncomment the below line with your actual API key as default
  # Specs will use the default API key, e.g. the string to the right of ||
  # by default this is 'DEMO_KEY', and will cause specs to fail as DEMO_KEY is rate-limited quickly
  #
  # @api_key = options[:api_key] || 'ACTUAL_API_KEY'

  options[:api_key] = @api_key
  @options = options
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



18
19
20
# File 'lib/nasa_api.rb', line 18

def api_key
  @api_key
end

#dateObject

Returns the value of attribute date.



18
19
20
# File 'lib/nasa_api.rb', line 18

def date
  @date
end

#high_definitionObject

Returns the value of attribute high_definition.



18
19
20
# File 'lib/nasa_api.rb', line 18

def high_definition
  @high_definition
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/nasa_api.rb', line 18

def options
  @options
end

Instance Method Details

#params_dates(params = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nasa_api.rb', line 46

def params_dates(params = {})
  # If date provided, parse it
  # If start/end date provided, parse them
  # If {:random = true} in params, use a random date 
  # otherwise use no date, most Nasa API's will then default to Today's date
  if params[:date]
    params[:date] = parse_date(params[:date])
    return params
  end

  if params[:start_date]
    params[:start_date] = parse_date(params[:start_date])
    if params[:end_date]
      params[:end_date] = parse_date(params[:end_date])
    end
    return params
  end 

  if params[:random]
    params[:date] = rand(Date.parse('2000-01-01')..Date.today)
    params.delete(:random)
  end
  params
end

#parse_date(date) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nasa_api.rb', line 33

def parse_date(date)
  # Allow Ruby Date/Time objects to be used
  # by changing them to the Nasa API's expected YYYY-MM-DD format
  case date
  when Time
    date.strftime("%Y-%m-%d")
  when Date
    date.to_s
  when String
    date
  end
end