-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpact_api.rb
827 lines (578 loc) · 16.9 KB
/
mpact_api.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
require 'sinatra'
require 'sinatra/activerecord'
require 'json'
require './environments'
# require './initializers'
class String
def to_bool
return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
return false if self == false || self.blank? || self =~ (/^(false|f|no|n|0)$/i)
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
end
class Fixnum
def to_bool
return true if self == 1
return false if self == 0
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
end
class TrueClass
def to_i; 1; end
def to_bool; self; end
end
class FalseClass
def to_i; 0; end
def to_bool; self; end
end
class NilClass
def to_bool; false; end
end
# use Rack::MethodOverride
set :method_override, true
set :public_folder, 'public'
class Guide < ActiveRecord::Base
has_many :entries
end
class Note < ActiveRecord::Base
belongs_to :entry
end
class Request < ActiveRecord::Base
belongs_to :entry
end
class Entry < ActiveRecord::Base
belongs_to :guide
has_many :requests
has_many :notes
end
class Op < ActiveRecord::Base
end
before do
error 401 unless params[:apikey] == "1138"
end
get '/' do
# 'mpact api'
erb :readme
end
get '/guides' do
content_type 'application/json'
Guide.limit(5).to_json
end
# get '/guides/edit' do
# erb :edit_guide
# end
helpers do
def guides_all
@guides_all ||= Guide.order('key ASC') || halt(404)
end
def guide_entries_all
@guide_entries_all ||= Entry.where('"entries"."guideKey" = ?', params[:key]).order('entrytype ASC', 'name ASC').select('id,"guideKey",name,image,bio,data,entrytype,location,include') || halt(404)
end
def guide_entries
@guide_entries ||= Entry.order('entrytype ASC, name ASC').where('"entries"."include" = ? AND "entries"."guideKey" = ?', true, params[:key]) || halt(404)
end
def entry_requests
@entry_requests ||= Entry.select('entries.id, "entries"."guideKey", entries.name, entries.image, entries.bio, entries.entrytype, entries.location, array_to_string(array_agg(requests.request), \'|\') as reqs').joins(:requests).where('"entries"."guideKey" = ?', params[:key]).group('entries.id')
end
configure :development do
def entry_requests
@entry_requests ||= Entry.select("entries.id, 'entries'.'guideKey', entries.name, entries.image, entries.bio, entries.location, entries.entrytype, group_concat(requests.request, \"|\") as reqs").joins(:requests).where('"entries"."guideKey" = ?', params[:key]).group('entries.id')
end
end
def entry_notes
@entry_notes ||= Note.order('created_at ASC, updated_at ASC').where('"notes"."author" = ? AND "notes"."entry_id" = ?', params[:author], params[:entry]) || halt(404)
end
def mission_ops
@mission_ops ||= Op.order('category ASC, what ASC') || halt(404)
end
def mission_ops_edit
@mission_ops_edit ||= Op.order('id ASC') || halt(404)
end
end
get '/v2/guide/:key/entrieswithreqs' do
redirect '/guide/' + params[:key] + '/entrieswithreqs?apikey=' + params[:apikey]
end
get '/guide/:key/entrieswithreqs' do
content_type 'application/json'
key = params[:key]
if key == "refuge"
sorted = entry_requests.sort_by &:id
else
sorted = entry_requests.sort_by &:name
end
sorted.to_json
end
# v2 GetEntriesForGuide
get '/v2/guide/:key/entries' do
content_type 'application/json'
key = params[:key]
puts params[:debug]
debug = params[:debug].to_bool
puts debug ? "debug is true" : "debug is false"
if key == "refuge"
sorted = guide_entries.sort_by &:id
else
# if debug == true
# puts "return all"
# sorted = guide_entries_all.select('id,"guideKey",name,image,entrytype')
# else
# puts "return subset"
sorted = guide_entries.select('id,"guideKey",name,image,entrytype')
# end
end
sorted.to_json
end
get '/guide/:key/entries' do
content_type 'application/json'
key = params[:key]
puts params[:debug]
debug = params[:debug].to_bool
puts debug ? "debug is true" : "debug is false"
if key == "refuge"
sorted = guide_entries.sort_by &:id
else
if debug == true
puts "return all"
sorted = guide_entries_all.select('id,"guideKey",name,image,entrytype,data')
else
puts "return subset"
sorted = guide_entries.select('id,"guideKey",name,image,entrytype,data')
end
end
sorted.to_json
end
get '/guide/:key/entries/report' do
if params[:key] == "gmc"
erb :gmcreport
else
"#{params[:key]} report"
end
end
# get a single entry by index
get '/guide/:key/entry/:idx/requests' do
content_type 'application/json'
theEntry = Entry.find_by_id(params[:idx])
requests = theEntry.requests || []
requests.to_json
end
# get a single entry for "today" functionality (per guide)
get '/guide/:key/entries/today' do
content_type 'application/json'
start_date = Date.new(2014, 7, 21)
today = Date.today
diff = (today-start_date).to_i
idx = diff % guide_entries.length
#production
guide_entries[idx-1].to_json
end
get '/guide/:key/entries/today/random' do
content_type 'application/json'
l = guide_entries.length
r = Random.new.rand(1..l)
# puts "random number: " + r
#production
guide_entries[r-1].to_json
end
# notes
get '/entry/:entry/notes' do
content_type 'application/json'
#params entry, author
entry_notes.to_json
end
post '/entry/:entry/addnote' do
entry_idx = params[:entry]
note_text = params[:text]
author = params[:author]
if entry_idx.nil? || author.nil? || note_text.nil? || note_text.empty?
halt(400)
else
lastNote = Note.last
nextid = lastNote.nil? ? 1 : lastNote.id + 1
puts nextid
note = Note.create(id: nextid, text: note_text, author: author, entry_id: entry_idx)
end
end
post '/note/:id' do
id = params[:id]
note = Note.find(id)
return status 404 if note.nil?
note.delete
status 202
end
#end notes processing
#mission ops
# get '/ops/add' do
# erb :add_op_form
# end
# get '/ops/edit' do
# erb :edit_op_form
# end
# get '/ops/report' do
# erb :ops_report
# end
# post '/ops/add' do
# puts "add " + params[:description]
# op = Op.create(
# category: params[:category].to_i,
# what: params[:what],
# when: params[:when],
# where: params[:where],
# description: params[:description])
# redirect '/ops/add?apikey=1138&added=' + op.id.to_s + '&cat=' + op.category.to_s
# end
# post '/ops/edit' do
# puts "edit " + params[:description]
# id = params[:id]
# Op.update(
# id, {
# :category => params[:category].to_i,
# :what => params[:what],
# :when => params[:when],
# :where => params[:where],
# :description => params[:description]
# })
# redirect '/ops/edit?apikey=1138&edited=' + id.to_s
# end
get '/ops/:cat' do
content_type 'application/json'
mission_ops.where('ops.category = ?', params[:cat].to_s).to_json
end
get '/ops' do
content_type 'application/json'
mission_ops.to_json
end
#end mission ops
# get '/guide/:key/addentry' do
# erb :add_form
# end
get '/entry/:idx' do
content_type 'application/json'
entry = Entry.find_by_id(params[:idx])
return entry.to_json(:include => :requests)
end
# post '/guide/:key/entry' do
# entry = nil
# name = params[:name]
# image = params[:image]
# # filename = params[:datafile] if !params[:datafile].nil?
# # content = params[:dfcontent]
# nextid = Entry.last.id + 1
# puts nextid.to_s
# if !name.nil?
# # puts "do stuff"
# entry = Entry.create(id: nextid, guideKey: params[:key], name: name, image: image, entrytype: params[:entrytype])
# if !image.nil?
# entry.image = image
# end
# redirect '/guide/' + params[:key] + '/addentry?apikey=1138&added=' + entry.id.to_s + '&et=' + entry.entrytype.to_s
# else
# redirect '/guide/' + params[:key] + '/addentry?apikey=1138&error=Error adding new entry.'
# end
# end
# get '/guide/:key/editentries' do
# erb :db_form
# end
# post '/guide/:key/editentry' do
# id = params[:entry]
# Entry.update(id, { :image => params[:image], :name => params[:name], :entrytype => params[:entrytype], :bio => params[:bio], :location => params[:location], :include => params[:include] })
# reqs_all = params.select { |key,value| key.to_s.match(/^request\d+/) }
# puts reqs_all
# theEntry = Entry.find_by_id(id)
# theEntry.requests.delete_all
# reqs_all.each_with_index { |item, idx|
# req = item[1]
# req.strip
# if req.length > 0
# theEntry.requests.create(request: req)
# end
# # item[1].strip
# # if item[1].length > 0
# # theEntry.requests.create(request: item)
# # end
# }
# redirect '/guide/' + params[:key] + '/editentries?apikey=1138&edited=' + id.to_s
# end
# post '/deleteguide/:id' do
# id = params[:id]
# guide = Guide.find(id)
# return status 404 if guide.nil?
# guide.delete
# status 202
# redirect '/guides/edit?apikey=1138&deleted=' + id.to_s
# end
# edit guide
# post '/editguide' do
# id = params[:guide]
# Guide.update(id, { :image => params[:image], :title => params[:title], :textLabel => params[:textLabel]})
# redirect '/guides/edit?apikey=1138&edited=' + id.to_s
# end
# post '/addguide' do
# newGuide = Guide.create(
# key: params[:guide_new],
# image: params[:image],
# title: params[:title],
# textLabel: params[:textLabel])
# redirect '/guides/edit?apikey=1138&added=' + newGuide.id.to_s
# end
# post '/guide/:key/deleteentry/:id' do
# id = params[:id]
# entry = Entry.find(id)
# return status 404 if entry.nil?
# entry.delete
# status 202
# redirect '/guide/' + params[:key] + '/editentries?apikey=1138&deleted=' + id.to_s
# end
# delete 'guide/entry/:id' do
# puts params[:_method]
# entry = Entry.find(params[:id])
# return status 404 if entry.nil?
# entry.delete
# status 202
# "Entry #{id} deleted."
# end
# post '/copyrequests/:from/:to' do
# fromEntryReqs = Request.where('"requests"."entry_id" = ?', params[:from])
# toEntry = Entry.find_by_id(params[:to])
# fromEntryReqs.each do |req|
# lastReq = Request.last
# nextid = lastReq.nil? ? 1 : lastReq.id + 1
# # req = Request.create(id: nextid, )
# toEntry.requests.create(request: req.request)
# end
# redirect '/guide/' + toEntry.guideKey + '/editentries?apikey=1138&edited=' + params[:to].to_s
# end
# post '/guide/:key/deleterequest/:id' do
# id = params[:id]
# request = Request.find(id)
# return status 404 if request.nil?
# request.delete
# status 202
# redirect '/guide/' + params[:key] + '/editentries?apikey=1138&deletedreq=' + id.to_s
# end
##############################################################################################################
# API version 3
get '/v3/guides' do
content_type 'application/json'
Guide.limit(5).to_json
end
get '/v3/guides/edit' do
erb :edit_guide
end
# Get guide at index
get '/v3/guide/:id' do
content_type 'application/json'
guide = Guide.find_by_id(params[:id])
return status 404 if guide.nil?
guide.to_json
end
# Edit guide
post '/v3/guide/:id' do
id = params[:id]
Guide.update(id, { :image => params[:image], :title => params[:title], :textLabel => params[:textLabel]})
redirect '/v3/guides/edit?apikey=1138&edited=' + id.to_s
end
# create guide
post '/v3/guide' do
newGuide = Guide.create(
key: params[:guide_new],
image: params[:image],
title: params[:title],
textLabel: params[:textLabel])
redirect '/v3/guides/edit?apikey=1138&added=' + newGuide.id.to_s
end
#end guide resources
#guide entries
get '/v3/guide/:key/entries/edit' do
erb :db_form
end
# post '/v3/guide/:key/entry' do
# entry = nil
# name = params[:name]
# image = params[:image]
# nextid = Entry.last.id + 1
# puts nextid.to_s
# if !name.nil?
# entry = Entry.create(id: nextid, guideKey: params[:key], name: name, image: image, entrytype: params[:entrytype])
# if !image.nil?
# entry.image = image
# end
# redirect '/v3/guide/' + params[:key] + '/entries/edit?apikey=1138&added=' + entry.id.to_s + '&et=' + entry.entrytype.to_s
# else
# redirect '/v3/guide/' + params[:key] + '/entries/edit?apikey=1138&error=Error adding new entry.'
# end
# end
# post '/v3/guide/:key/entry/:entryid' do
# entryid = params[:entryid]
# Entry.update(entryid, { :image => params[:image], :name => params[:name], :entrytype => params[:entrytype], :bio => params[:bio], :location => params[:location], :include => params[:include] })
# reqs_all = params.select { |key,value| key.to_s.match(/^request\d+/) }
# puts reqs_all
# theEntry = Entry.find_by_id(id)
# theEntry.requests.delete_all
# reqs_all.each_with_index { |item, idx|
# req = item[1]
# req.strip
# if req.length > 0
# theEntry.requests.create(request: req)
# end
# }
# redirect '/v3/guide/' + params[:key] + '/entries?apikey=1138&edited=' + id.to_s
# end
# v3 GetEntriesForGuide
get '/v3/guide/:key/entries' do
content_type 'application/json'
key = params[:key]
if key == "refuge"
sorted = guide_entries.sort_by &:id
else
sorted = guide_entries.select('id,"guideKey",name,image,entrytype,bio,location')
end
sorted.to_json(:include => :requests)
end
# get a single entry for "today" functionality (per guide)
get '/v3/guide/:key/entries/today' do
content_type 'application/json'
start_date = Date.new(2014, 7, 21)
today = Date.today
diff = (today-start_date).to_i
idx = diff % guide_entries.length
#production
guide_entries[idx-1].to_json
end
#end guide entries
# entries/requests
# GET entry by Index
get '/v3/entry/:idx' do
content_type 'application/json'
entry = Entry.find_by_id(params[:idx])
return entry.to_json(:include => :requests)
end
post '/v3/entry' do
entry = nil
guide = params[:guidekey]
name = params[:name]
image = params[:image]
nextid = Entry.last.id + 1
puts nextid.to_s
if !name.nil?
entry = Entry.create(id: nextid, guideKey: guide, name: name, image: image, entrytype: params[:entrytype])
if !image.nil?
entry.image = image
end
redirect '/v3/guide/' + guide + '/entries/edit?apikey=1138&added=' + entry.id.to_s + '&et=' + entry.entrytype.to_s
else
redirect '/v3/guide/' + guide + '/entries/edit?apikey=1138&error=Error adding new entry.'
end
end
# Edit Entry by index
post '/v3/entry/:id' do
id = params[:id]
Entry.update(id, { :image => params[:image], :name => params[:name], :entrytype => params[:entrytype], :bio => params[:bio], :location => params[:location], :include => params[:include] })
reqs_all = params.select { |key,value| key.to_s.match(/^request\d+/) }
puts reqs_all
theEntry = Entry.find_by_id(id)
theEntry.requests.delete_all
reqs_all.each_with_index { |item, idx|
req = item[1]
req.strip
if req.length > 0
theEntry.requests.create(request: req)
end
}
redirect '/v3/guide/' + params[:guidekey] + '/entries/edit?apikey=1138&edited=' + id.to_s
end
# GET requests for Entry
get '/v3/entry/:id/requests' do
content_type 'application/json'
theEntry = Entry.find_by_id(params[:id])
requests = theEntry.requests || []
requests.to_json
end
# DELETE entry
delete '/v3/entry/:id' do
puts "*******************************************"
puts params[:_method]
puts "*******************************************"
id = params[:id]
entry = Entry.find(id)
return status 404 if entry.nil?
entry.delete
status 200
"Entry #{id} deleted."
end
# DELETE request
delete '/v3/request/:id' do
id = params[:id]
request = Request.find(id)
return status 404 if request.nil?
request.delete
status 200
# redirect '/v3/guide/' + params[:guidekey] + '/entries/edit?apikey=1138&deletedreq=' + id.to_s
end
# post '/copyrequests/:from/:to' do
post '/v3/requests/copy' do
fromEntryReqs = Request.where('"requests"."entry_id" = ?', params[:from])
toEntry = Entry.find_by_id(params[:to])
fromEntryReqs.each do |req|
lastReq = Request.last
nextid = lastReq.nil? ? 1 : lastReq.id + 1
# req = Request.create(id: nextid, )
toEntry.requests.create(request: req.request)
end
status 200
# redirect '/v3/guide/' + toEntry.guideKey + '/entries/edit?apikey=1138&edited=' + params[:to].to_s
end
#mission ops
get '/v3/ops/edit' do
erb :edit_op_form
end
get '/v3/ops/report' do
erb :ops_report
end
post '/v3/op' do
puts "add " + params[:description]
op = Op.create(
category: params[:category].to_i,
what: params[:what],
when: params[:when],
where: params[:where],
description: params[:description])
redirect '/v3/ops/edit?apikey=1138&added=' + op.id.to_s + '&cat=' + op.category.to_s
end
get '/v3/op/:id' do
content_type 'application/json'
op = Op.find_by_id(params[:id])
return status 404 if op.nil?
op.to_json
end
post '/v3/op/:id' do
puts "edit " + params[:description]
id = params[:id]
Op.update(
id, {
:category => params[:category].to_i,
:what => params[:what],
:when => params[:when],
:where => params[:where],
:description => params[:description]
})
redirect '/v3/ops/edit?apikey=1138&edited=' + id.to_s
end
get '/v3/ops' do
content_type 'application/json'
cat = params[:category]
if cat.nil? || cat.empty?
mission_ops.to_json
else
mission_ops.where('ops.category = ?', cat.to_s).to_json
end
end
#end mission ops
#################
# Static (image) handler
def send_static_file(path, &missing_file_block)
file_path = File.join(File.dirname(__FILE__), 'public', path)
File.exist?(file_path) ? send_file(file_path) : missing_file_block.call
end
get '/images/:filename' do
send_static_file(request.path + '.png') {404}
end