-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This attempts to fix an issue with the `request.body` not being a StringIO instance, but wrapped depending on the application server in use. For Webrick, the raw request body seems not to be accessible anymore, which is a shame. Switching to Puma helps a bit, but getting the raw body still requires accessing an instance variabale from Rack::Lint...
- Loading branch information
Showing
10 changed files
with
70 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require_relative "lib/sisimai_web/app" | ||
run SisimaiWeb::App |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "sinatra" | ||
require "sisimai" | ||
require "sisimai/fact" | ||
require "oj" | ||
|
||
require_relative "./classifier" | ||
|
||
module SisimaiWeb | ||
class App < Sinatra::Application | ||
set :host, "0.0.0.0" | ||
set :port, 3001 | ||
set :environment, "production" | ||
|
||
post "/" do | ||
result = SisimaiWeb.classify(raw_body) | ||
|
||
status 200 | ||
headers "Content-Type" => "application/json" | ||
body result.to_json | ||
end | ||
|
||
private | ||
|
||
def raw_body | ||
body = request.body | ||
body = case body | ||
when StringIO | ||
body | ||
when Rack::Lint::Wrapper::InputWrapper | ||
body.instance_variable_get(:@input) | ||
else | ||
raise ArgumentError, "don't know how to access raw body" | ||
end | ||
|
||
body.tap(&:rewind).read | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "sisimai" | ||
require "sisimai/fact" | ||
|
||
module SisimaiWeb | ||
def self.classify(message) | ||
fact = Sisimai::Fact.rise(data: message, vacation: true, delivered: true)&.first | ||
return unless fact | ||
|
||
fact.damn.merge("softbounce" => to_softbounce(fact)) | ||
end | ||
|
||
# Sisimai::Fact has deprecated and will remove #softbounce in v5.1.0. | ||
# For backward compatibility we'll keep it though. | ||
def self.to_softbounce(fact) | ||
return 0 if fact.hardbounce | ||
return -1 if %w[delivered feedback vacation].include?(fact.reason) | ||
|
||
1 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters