Skip to content

Commit

Permalink
convert to modular Sinatra app
Browse files Browse the repository at this point in the history
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
dmke committed Apr 17, 2024
1 parent d921d00 commit 15eeb1b
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ COPY . .

EXPOSE 3000

CMD ["bundle", "exec", "ruby", "/app/app.rb"]
CMD ["bundle", "exec", "rackup", "--host=0.0.0.0", "--port=3000", "--env=production"]
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ git_source(:github) { |name| "https://github.com/#{name}.git" }

gem "sinatra"
gem "sisimai", "~> 5.0.2"
gem "webrick"
gem "puma"
gem "rackup"

group :test do
Expand Down
7 changes: 5 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ GEM
byebug (11.1.3)
coderay (1.1.3)
diff-lcs (1.5.1)
method_source (1.0.0)
method_source (1.1.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
nio4r (2.7.1)
oj (3.16.3)
bigdecimal (>= 3.0)
pry (0.14.2)
Expand All @@ -17,6 +18,8 @@ GEM
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
puma (6.4.2)
nio4r (~> 2.0)
rack (3.0.10)
rack-protection (4.0.0)
base64 (>= 0.1.0)
Expand Down Expand Up @@ -58,12 +61,12 @@ PLATFORMS

DEPENDENCIES
pry-byebug
puma
rack-test
rackup
rspec
sinatra
sisimai (~> 5.0.2)
webrick

BUNDLED WITH
2.5.6
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Download the code, install the dependencies and start the server.
$ git clone https://github.com/digineo/sisimai-web
$ cd sisimai-web
$ bundle install
$ bundle exec ruby app.rb
$ bundle exec rackup --host 127.0.0.1 --port 3000
```

## Installation with Docker
Expand Down
20 changes: 0 additions & 20 deletions app.rb

This file was deleted.

2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative "lib/sisimai_web/app"
run SisimaiWeb::App
18 changes: 0 additions & 18 deletions lib/classifier.rb

This file was deleted.

38 changes: 38 additions & 0 deletions lib/sisimai_web/app.rb
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
20 changes: 20 additions & 0 deletions lib/sisimai_web/classifier.rb
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
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
require "rack/test"

ENV["RACK_ENV"] = "test"
require_relative "../app.rb"
require_relative "../lib/sisimai_web/app.rb"

module TestHelper
include Rack::Test::Methods

def app
Sinatra::Application
SisimaiWeb::App
end

def read_fixture(name)
Expand Down

0 comments on commit 15eeb1b

Please sign in to comment.