forked from seven1m/bible_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
115 lines (107 loc) · 3.48 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
require 'bundler'
Bundler.require
require 'sinatra/reloader'
require 'json'
DB = Sequel.connect(ENV['BIBLE_API_DB'], charset: 'utf8')
set :protection, except: [:json_csrf]
def get_verse_id(ref, translation_id, last = false)
record = DB[
'select id from verses ' \
"where book_id = :book and chapter = :chapter #{ref[:verse] ? 'and verse = :verse' : ''} " \
'and translation_id = :translation_id ' \
" #{last ? 'order by id desc' : ''} limit 1",
ref.update(translation_id: translation_id)
].first
record ? record[:id] : nil
end
def get_verses(ranges, translation_id)
all = []
ranges.each do |(ref_from, ref_to)|
start_id = get_verse_id(ref_from, translation_id)
stop_id = get_verse_id(ref_to, translation_id, :last)
return nil unless start_id && stop_id
all += DB['select * from verses where id between ? and ?', start_id, stop_id].to_a
end
all
end
# pulled from sinatra-jsonp and modified to return a UTF-8 charset
module Sinatra
module Jsonp
def jsonp(*args)
if args.size > 0
data = MultiJson.dump args[0], :pretty => settings.respond_to?(:json_pretty) && settings.json_pretty
if args.size > 1
callback = args[1].to_s
else
['callback','jscallback','jsonp','jsoncallback'].each do |x|
callback = params.delete(x) unless callback
end
end
if callback
callback.tr!('^a-zA-Z0-9_$\.', '')
content_type 'text/javascript', charset: 'utf-8'
response = "#{callback}(#{data})"
else
content_type 'application/json', charset: 'utf-8'
response = data
end
response
end
end
end
helpers Jsonp
end
get '/' do
@translations = DB['select id, identifier, language, name from translations order by language, name']
books = DB["select translation_id, book from verses where book_id = 'JHN' group by translation_id"]
@books = books.each_with_object({}) do |book, hash|
hash[book[:translation_id]] = book[:book]
end
@host = (request.env['SCRIPT_URI'] || request.env['REQUEST_URI']).split('?').first
erb :index
end
get '/:ref' do
content_type 'application/json', charset: 'utf-8'
ref_string = params[:ref].tr('+', ' ')
translation = DB['select * from translations where identifier = ?', params[:translation] || 'WEB'].first
vn = params[:verse_numbers]
unless translation
status 404
response = { error: 'translation not found' }
return jsonp(response)
end
ref = BibleRef::Reference.new(ref_string, language: translation[:language_code])
if (ranges = ref.ranges)
if (verses = get_verses(ranges, translation[:id]))
verses.map! do |v|
{
book_id: v[:book_id],
book_name: v[:book],
chapter: v[:chapter],
verse: v[:verse],
text: v[:text]
}
end
verse_text = if vn == 'true'
verses.map { |v| '(' + v[:verse].to_s + ') ' + v[:text] }.join
else
verses.map { |v| v[:text] }.join
end
response = {
reference: ref.normalize,
verses: verses,
text: verse_text,
translation_id: translation[:identifier],
translation_name: translation[:name],
translation_note: translation[:license]
}
else
status 404
response = { error: 'not found' }
end
else
status 404
response = { error: 'not found' }
end
jsonp response
end