forked from learningtapestry/lrmi-tagger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
82 lines (65 loc) · 2.3 KB
/
Copy pathapp.rb
File metadata and controls
82 lines (65 loc) · 2.3 KB
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
require 'sinatra/base'
require 'sinatra'
require 'json'
require './lib/learning_registry_client'
require 'erb'
class MyApp < Sinatra::Base
enable :sessions
set :session_secret, 'app00key00change'
@@lr_client = nil
configure do
set :app_file, __FILE__
if File.exists?('./config/learning_registry.yml')
@@lr_client = LearningRegistryClient.new
else
raise 'File does not exist: config/learning_registry.yml. Please copy config/learning_registry-sample.yml to config/learning_registry.yml and add the configuration values.'
end
end
configure :development do
enable :logging, :dump_errors, :raise_errors
end
configure :qa do
enable :logging, :dump_errors, :raise_errors
end
configure :production do
set :raise_errors, false #false will show nicer error page
set :show_exceptions, false #true will ignore raise_errors and display backtrace in browser
end
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
## Helper function to determine if parameter exists and is not empty from form POST
def param_exists?(param_name)
params[param_name] and not params[param_name].empty? ? true : false
end
## Helper function to determine if parameter exists and is not empty from form POST
def select_param?(param_name, value)
puts session[param_name]
session[param_name] and session[param_name].include? value ? ' SELECTED ' : ''
end
end
get '/' do
session.clear if params[:clear] == 'true'
erb :index
end
post '/' do
erb :index
end
post '/rdfa' do
erb :rdfa
end
post '/learning_registry' do
lr_msg = erb :_lrenvelope_template
result = @@lr_client.publish lr_msg
lr_message = 'Unsuccessful publish into the Learning Registry. Please contact your system administrator or email <a href="mailto:learningregistry@lrmitagger.org">learningregistry@lrmitagger.org</a>.'
if result.code == '200' then
body = JSON.parse(result.body)
if body['OK'] then
doc_id = body['document_results'][0]['doc_ID']
lr_message = "Published successfully into the Learning Registry. <a href='#{@@lr_client.obtain_url(doc_id)}' target='_new'>Click here to view the record.</a>"
end
end
erb :learning_registry, :locals => { :lr_message => lr_message }
end
end