This question is (very) similar to this post, but not a duplicate IMHO, because:
- my setting is completely reproducible ( docker )
- I use
gemto install everything explictly - I have a "non-rails-related" script to "prove" gem is "operational"
- the mentioned question is "ancient" in internet time (nearly 8 years old)
Here is the completely reproducible docker image of a REST API rails server:
FROM ruby
RUN apt-get update
RUN apt-get install vim -y
RUN gem install rails
RUN rails new translator --api --skip-action-mailer --skip-active-record
WORKDIR translator
RUN gem install syntax_tree
COPY routes.rb config
COPY translator_controller.rb app/controllers
COPY test.rb test.rb
COPY primes.rb primes.rb
EXPOSE 3000
I build and run my docker image normally:
$ docker build --tag host.front.rb --file Dockerfile .
$ docker run -p 8012:3000 -d -t --name front.rb host.front.rb
$ docker exec -it front.rb bash
First I make sure the syntax_tree gem is installed properly:
$ cat test.rb # <----- independent test file
require "json"
require "syntax_tree"
file = File.open('primes.rb')
content = file.read
file.close
program = SyntaxTree.parse(content)
puts JSON.dump(program)
$ ruby test.rb | head -c 41
{"type":"program","location":[1,0,19,265] # <---- good !
Then I verify I can reach my dockerized server:
# on docker
$ rails server -b 0.0.0.0
=> Booting Puma
=> Rails 7.1.3.2 application starting in development
< ... omitted for brevity ... >
* Listening on http://0.0.0.0:3000
# my local machine
$ curl -X POST -F "[email protected]" http://127.0.0.1:8012/translator1
>> 247
$ stat --format=%n:%s primes.rb
primes.rb:247 # <----- good !
When I try my second endpoint (which requires the syntax_tree) it fails:
$ curl -X POST -F "[email protected]" http://127.0.0.1:8012/translator2
{"status":500,...,"exception":"LoadError: cannot load such file -- syntax_tree
Here is my translator_controller.rb:
require "json"
class TranslatorController < ApplicationController
def post_handler_simple
# get the sent file entity
source = params["source"]
# read ruby source code
file = File.open(source.tempfile)
content = file.read
file.close
# return content length -> works fine !
render plain: ">> #{content.length()}\n"
end
def post_handler
# get the sent file entity
source = params["source"]
# read ruby source code
file = File.open(source.tempfile)
content = file.read
file.close
# this import doesn't work ...
require "syntax_tree"
# parse it
program = SyntaxTree.parse(content)
# return AST (json format)
render plain: JSON.dump(program)
end
end
And here is my routes.rb for completeness
Rails.application.routes.draw do
post "/translator1", to: "translator#post_handler_simple"
post "/translator2", to: "translator#post_handler"
end
By default
Railsknows nothing about gems you have installed locally (viagem install ...).You have no problem in
test.rbbecause it's a pure non-RailsRubyfile - and locally installed gem was found byruby_gems.If you want
syntax_treeto be available in controller (or require it from there) - define this dependency in projectGemfile.