-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsentiment_api.rb
More file actions
62 lines (47 loc) · 1.26 KB
/
sentiment_api.rb
File metadata and controls
62 lines (47 loc) · 1.26 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
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'sinatra'
require "#{File.dirname(__FILE__)}/analyzer"
class SentimentApi < Sinatra::Base
disable :logging
disable :raise_errors
disable :show_exceptions
configure :production do
disable :dump_errors
end
set :server, 'thin'
@@the_logic = Analyzer.new
get '/v1/word/:word.json' do
res = @@the_logic.word(params[:word])
content_type 'application/json'
body res.to_json
status 200
end
post '/v1/word/:word.json' do
res = @@the_logic.add_word(params[:word],params[:value])
content_type 'application/json'
body res.to_json
status 200
end
get '/v1/sentence/:sentence.json' do
res = @@the_logic.sentence(params[:sentence])
content_type 'application/json'
body res.to_json
status 200
end
not_found do
""
end
error InvalidParameters do
error_code = 422
content_type 'application/json'
error error_code, {:error => {:reason => env['sinatra.error'].to_s, :code => error_code}}.to_json
end
error do
error_code = 500
content_type 'application/json'
error error_code, {:error => {:reason => env['sinatra.error'].to_s, :code => error_code}}.to_json
end
end
SentimentApi.run! :port => ARGV[0]