I've built a simple api in Sinatra, with the purpose of setting up a deployment pipeline using Docker. I'm at a stage where I could easily switch Sinatra for something else and there's currently one reason why I might do so.
I once wrote an api using Express and it was trivial to reuse the tests to test a deployment:
# Testing the code
chai.request(app)
.get('/')
# Testing a deployment
chai.request('http://localhost:8080')
.get('/')
Examples from: https://github.com/chaijs/chai-http#integration-testing
Now I am wondering if I can accomplish the same with rack-test and Sinatra. Simply sending a URL, instead of the app, crashes. So is there an easy way to accomplish this? I suppose I could write a testing framework on top of rack-test, but I'm not sure it's worth it, even though I do prefer Ruby over Javascript and Sinatra over Express.
I realised that I should be able to write a rack app that forwards all requests to the environment I want to run deployment tests against. So I went to Google and found a gem that does just that: rack-proxy
Here's how to write a simple rack app that redirects requests to your server:
require 'rack/proxy'
class Foo < Rack::Proxy
def rewrite_env(env)
env["HTTP_HOST"] = "api.example.com"
env
end
end
Then I ran my tests against Foo.new they succeeded. I checked the logs of that environment and I can confirm that my tests were in fact running against that environment.
Foo may not be the best name for such a proxy and you may not want the host name hardcoded, but I'm sure you can figure out how to make this work in your project if you need it.
Related
I am curious as to how best to set up a testing environment for Angular on a server that is running Sinatra. Normally when running a node server, you have a package.json which can require the dev-dependency of Protractor. However a Sinatra server will only have a Gemfile. Are there any best practices or gems that facilitate using Protractor in a Ruby environment?
Protractor is a node application so you must have node install to use it. In your case it's sounds like you will need to have both the Gemfile to maintain the dependencies for the Sinatra app and a package.json to manage dependencies for your automation tests Protractor.
You have lots of other options for end to end tests in ruby if you don't want to or don't have to use Protractor you can use rspec (the syntax for rspec and jasmine is similar) or cucumber as your testing framework and then use capybara (or selenium directly if you prefer) to control a browser and complete end to end tests.
While developing JavaScript apps, I usually create an API app, totally separate from the UI app. For the API, I usually use Sinatra.
I'm developing a JavaScript app that will use a WebSocket service I build. I'd like to use Ruby (em-websocket for now) and ActiveModel for data models. I want to keep this really lightweight, like a Sinatra app is for a RESTful API.
It seems my WebSocket service will simply be a ruby script invoked via "ruby web_socket_service.rb". I'd like to be able to use various gems (like activerecord, capistrano, and nokogiri) with this WebSocket service. What's the most typical way of accomplishing this?
Would I be better off creating a standalone gem to contain my models and the WebSocket service script and then host my WebSocket service from that? Or maybe simply include the gems and models directly in the script via "gem 'name'? Or, is there some special library or framework commonly-used to tackle this?
Look at a Rails app. That's the approach I would take if your WebSocket service starts to grow towards a medium-sized app. I.e. bin, lib, Rakefile, and a Gemfile for your gems and bundler.
For smaller apps you can still use a Gemfile and bundler to manage the included gems. This locks gem versions so you won't have conflicts if you deploy to other servers. And then just put everything into one or two script files, similar to Sinatra.
Creating standalone gems is really only useful for libraries or application parts that are reusable across many applications. This doesn't sound like that sort of thing.
I'm trying to write a Ruby web application and I want to use the functionality offered by Sinatra.
I used this code:
require 'rubygems'
require 'sinatra'
get '/' do
'Hello, world!'
end
I typed localhost:8080/?hello.rb in my browser and I received an error message that the script is unable to find sinatra.
I also tried to run the sinatra app using this command:
ruby hello.rb
and this time it worked!
So I suspect the problem is not in my code, but in the way I'm using G-WAN to serve the Ruby script.
Sinatra expects to be run by a rack-compatible server, which G-Wan is apparently not. When you run script manually it is being run by a WEBrick server that comes bundled with Ruby. Examples of compatible servers are thin, unicorn or apache/nginx with phusion passenger
Ruby (like Java or PHP) probably uses configuration files and/or environment variables to specify where to find packages, modules and libraries.
While G-WAN received similar requests for asm, C, C++, D, Objective-C, Java and C# support, Ruby users did not manifest interest in this area - so far.
If you point to us the relevant Ruby documentation, we will try to add this Ruby feature (just like it was done for the other languages).
I prepare to develop one project, which has no UI. The project just need to interact with database, so is there any example for reference ?
Does your app have ties to a Rails-based app and need to read the Rails configuration files? Or, is it entirely stand-alone and have no Rails interaction?
ActiveRecord is OK for that, but if I don't need Rails compatibility I use Sequel. It's a great ORM that I find to be much more flexible.
If you need Rails compatibility and want to use ActiveRecord, look into using rails runner. From the docs:
runner runs Ruby code in the context of Rails non-interactively. For instance:
$ rails runner "Model.long_running_method"
Rails runner is for command-line apps that don't need the HTTPd server or user-interface of Rails. I use them for things like an app that runs daily to ftp files from a site for analysis. It has to write to the database, so it has access to all the models I've defined, but it never needs to present anything to the user since it is invisible to them.
I know this is pretty exotic, but I want to deploy a Ruby web application (not Rails, but Rack based, so it plugs into most Ruby servers just fine) using JRuby. Google and friends give me a few success stories, but mostly rails related and if not, no details on the deployment are provided. The framework I'm using is Ramaze, if it matters.
Any help would be greatly appreciated.
In my opinion, running a Rack based application with a rackup script is the real Ruby way. And I wanted to apply the same for JRuby too. That is why I've written jetty-rackup http://github.com/geekq/jetty-rackup
We are using it for deploying a Sinatra web application. No Java specific configuration needed. A typical, small config.ru is enough. Embedded jetty web server is used in place of Webrick then.
This is the "just works" gem for me: https://github.com/matadon/mizuno