forked from r-spacex/SpaceX-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
262 lines (225 loc) · 6.65 KB
/
app.rb
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#########################################
# SpaceX API for searching company info,
# vehicle info, launch sites, and
# launch data.
#########################################
require 'bundler/setup'
require 'sinatra'
require 'sinatra/subdomain'
require 'json'
require 'mongo'
# Uses the modular version of Sinatra
class SpacexAPI < Sinatra::Base
register Sinatra::Subdomain
# DB credentials
host = ENV['MONGO_HOST']
user = ENV['MONGO_USER']
password = ENV['MONGO_PASS']
database = ENV['MONGO_DB']
# Creates connection for mongo client
client = Mongo::Client.new("mongodb://#{user}:#{password}@#{host}:63892/#{database}")
# Error for no results
error = { error: 'No Matches Found' }
# Disables rack protection because of false positives
# that were blocking connections to home page
disable :protection
# No longer necessary
# Forces the use of HTTPS for the API
# before do
# redirect request.url.sub('http', 'https') unless request.secure?
# end
# Uses subdomain api.example.com to route traffic
subdomain :api do
##########################################
# Basic Info Endpoints
##########################################
get '/' do
content_type :json
collection = client[:home]
hash = collection.find({}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a[0])
end
get '/info' do
content_type :json
collection = client[:info]
hash = collection.find({}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a[0])
end
get '/vehicles' do
content_type :json
collection = client[:vehicle]
hash = collection.find({}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a)
end
get '/vehicles/falcon1' do
content_type :json
collection = client[:vehicle]
hash = collection.find({id: 'falcon1'}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a[0])
end
get '/vehicles/falcon9' do
content_type :json
collection = client[:vehicle]
hash = collection.find({id: 'falcon9'}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a[0])
end
get '/vehicles/falconheavy' do
content_type :json
collection = client[:vehicle]
hash = collection.find({id: 'falcon_heavy'}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a[0])
end
get '/vehicles/dragon' do
content_type :json
collection = client[:vehicle]
hash = collection.find({id: 'dragon'}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a[0])
end
get '/launchpads' do
content_type :json
collection = client[:launchpad]
hash = collection.find({}, projection: {_id: 0})
JSON.pretty_generate(hash.to_a[0])
end
get '/launches/latest' do
content_type :json
collection = client[:launch]
hash = collection.find({}, projection: {_id: 0}).sort({flight_number: -1}).limit(1)
JSON.pretty_generate(hash.to_a)
end
##########################################
# Launches endpoints
##########################################
# Returns all launches
get '/launches' do
content_type :json
collection = client[:launch]
# Gets launches sorted by year
if params['year']
year = params['year']
hash = collection.find({launch_year: "#{year}"}, projection: {_id: 0}).sort({flight_number: 1})
# Gets launches in a date range
elsif params['from'] and params['to']
start = params['from']
final = params['to']
hash = collection.find({ launch_date_utc: {'$gte': "#{start}T00:00:00Z", '$lte': "#{final}T00:00:00Z"}}, projection: {_id: 0}).sort({flight_number: 1})
# Gets all past launches
else
hash = collection.find({}, projection: {_id: 0}).sort({flight_number: 1})
end
# parse and return results
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
##########################################
# Upcoming launch endpoints
##########################################
get '/launches/upcoming' do
content_type :json
collection = client[:upcoming]
# Gets upcoming launches sorted by year
if params['year']
year = params['year']
hash = collection.find({launch_year: "#{year}"}, projection: {_id: 0}).sort({flight_number: 1})
# Gets upcoming launches in a date range
elsif params['from'] and params['to']
start = params['from']
final = params['to']
hash = collection.find({launch_date_utc: {'$gte': "#{start}T00:00:00Z", '$lte': "#{final}T00:00:00Z"}}, projection: {_id: 0}).sort({flight_number: 1})
# Gets all future launches
else
hash = collection.find({}, projection: {_id: 0}).sort({launch_date_utc: 1})
end
# parse and return results
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
##########################################
# Launches by part serial #'s'
##########################################
# Get all launches with a core serial number
get '/launches/cores/:core' do
content_type :json
core = params['core']
collection = client[:launch]
hash = collection.find({core_serial: "#{core}"}, projection: {_id: 0}).sort({core_serial: 1})
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
# Get info on a specific Dragon capsule
get '/parts/caps/:cap' do
content_type :json
cap = params['cap']
collection = client[:capsule]
hash = collection.find({capsule_serial: "#{cap}"}, projection: {_id: 0}).sort({capsule_serial: 1})
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
# Get all Dragon Capsule information
get '/parts/caps' do
content_type :json
collection = client[:capsule]
hash = collection.find({}, projection: {_id: 0}).sort({capsule_serial: 1})
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
# Get all launches with capsule serial #
get '/launches/caps/:cap' do
content_type :json
cap = params['cap']
collection = client[:launch]
hash = collection.find({cap_serial: "#{cap}"}, projection: {_id: 0}).sort({capsule_serial: 1})
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
# Get all Dragon core information
get '/parts/cores' do
content_type :json
collection = client[:core]
hash = collection.find({}, projection: {_id: 0}).sort({core_serial: 1})
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
# Get core information by serial #
get '/parts/cores/:core' do
content_type :json
core = params['core']
collection = client[:core]
hash = collection.find({core_serial: "#{core}"}, projection: {_id: 0}).sort({core_serial: 1})
array = hash.to_a
if array.empty?
JSON.pretty_generate(error)
else
JSON.pretty_generate(array)
end
end
end
end