-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathopen_notify.rb
More file actions
38 lines (29 loc) · 834 Bytes
/
open_notify.rb
File metadata and controls
38 lines (29 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
require 'json'
require 'faraday'
# Fetch data from the OpenNotify API service at http://api.open-notify.org/
module OpenNotify
BASE_DIR = __dir__
# To allow this to work without internet access, the read data method just
# loads and parses the data
#
# Change this to 'yes' if you want to use the live data.
USE_LIVE_DATA = 'no'
def iss_now
fetch_data(api: 'iss-now')
end
def astros
fetch_data(api: 'astros')
end
def fetch_data(api:)
if USE_LIVE_DATA == 'yes'
conn = Faraday.new('http://api.open-notify.org/') do |f|
f.response :json
end
return conn.get("#{api}.json").body
end
filepath = File.join(BASE_DIR, 'data', "#{api}.json")
JSON.parse(File.read(filepath))
end
module_function :iss_now, :astros, :fetch_data
end