Class: CurrentAstronauts::Astronauts
- Inherits:
-
Object
- Object
- CurrentAstronauts::Astronauts
- Defined in:
- lib/current_astronauts.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
-
#fetch ⇒ Object
Let’s go get some data.
-
#initialize ⇒ Astronauts
constructor
A new instance of Astronauts.
-
#num ⇒ Object
How many astronauts in space?.
-
#people ⇒ Object
List of astronauts and their craft, as an array of hashes.
-
#print ⇒ Object
Print a formatted list of astronauts and their craft.
-
#success? ⇒ Boolean
Fetch succeeded and data message is ‘success’.
Constructor Details
#initialize ⇒ Astronauts
Returns a new instance of Astronauts.
9 10 11 12 13 14 15 16 |
# File 'lib/current_astronauts.rb', line 9 def initialize if ENV['OPEN_NOTIFY_URL'] @url = ENV['OPEN_NOTIFY_URL'] else @url = 'http://api.open-notify.org/astros.json' end @data = nil end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/current_astronauts.rb', line 7 def data @data end |
Instance Method Details
#fetch ⇒ Object
Let’s go get some data
19 20 21 22 23 24 25 |
# File 'lib/current_astronauts.rb', line 19 def fetch response = HTTParty.get(@url) if response.success? @data = response.parsed_response end response.code end |
#num ⇒ Object
How many astronauts in space?
37 38 39 |
# File 'lib/current_astronauts.rb', line 37 def num success? ? @data['number'] : nil end |
#people ⇒ Object
List of astronauts and their craft, as an array of hashes
42 43 44 |
# File 'lib/current_astronauts.rb', line 42 def people success? ? @data['people'] : nil end |
#print ⇒ Object
Print a formatted list of astronauts and their craft
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/current_astronauts.rb', line 47 def print unless success? return nil end nlen = "Name".length clen = "Craft".length @data['people'].each do |p| nlen = p['name'].length > nlen ? p['name'].length : nlen clen = p['craft'].length > clen ? p['craft'].length : clen end print_header(nlen, clen) @data['people'].each do |p| print_line(nlen, p['name'], clen, p['craft']) end end |
#success? ⇒ Boolean
Fetch succeeded and data message is ‘success’
28 29 30 31 32 33 34 |
# File 'lib/current_astronauts.rb', line 28 def success? stat = false if @data and @data['message'] == 'success' stat = true end return stat end |