-
Notifications
You must be signed in to change notification settings - Fork 1
/
notes_sinatra.rb
51 lines (40 loc) · 1.24 KB
/
notes_sinatra.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
# -*- encoding : utf-8 -*-
require 'sinatra'
require 'sinatra/base'
require 'erb'
require 'yajl'
class NotesSinatra < Sinatra::Base
# allow user to send normal get method with _method=put x delete
use Rack::MethodOverride
set :default_locale, 'en'
set :views, File.dirname(__FILE__) + '/app_sinatra/views'
TAXONOMY_URL_REGEXP = /^\/v\d+\/[a-z0-9_\-]+\//
# Set connection based on TAXONOMY
before do
#path is api call
if TAXONOMY_URL_REGEXP =~ request.path
@api_call = true
#split path for getting taxonomy
path = request.path.split("/")
set_taxonomy path[2] if path && path.size > 2 #taxonomy
#by taxonomy param
elsif params[:taxonomy]
set_taxonomy params[:taxonomy]
#by taxonomy session
elsif session[:taxonomy]
set_taxonomy session[:taxonomy]
#set default as sandbox
else
set_taxonomy "sandbox"
end
end
def set_taxonomy taxonomy
@taxonomy = taxonomy
session[:taxonomy] = taxonomy
ApiAdapter::Note.index_name "notes_#{taxonomy}"
rescue Exception => e
render_error 500, e.class.to_s.demodulize, e.message
end
end
#including Sinatra methods
Dir[File.join(File.dirname(__FILE__),"/app_sinatra/**/*.rb")].each {|file| require file }