Mount grape framework in ramaze framework? - ruby

Someone help me to mount grape framework...
require 'grape'
class Api < Grape::API
format :json
resources :articles do
get do
{hello: 'hello world!'}
end
end
end
Also i wanna to change custom routes...

Related

How can I use ActionMailer previews in a Sinatra app?

I have a Sinatra Ruby app with the ActionMailer gem for sending emails. The email sending functionality works fine, but I can't figure out how to use the preview functionality for development. My mailer mailer.rb is located in lib/companyname/mailers, and my preview mailer_preview.rb is located in spec/companyname/mailers/previews. When I run my app and navigate to http://localhost:26250/rails/mailers I get a 404 "Sinatra doesn't know this ditty" page.
What do I need to do to be able to see the previews in my browser?
mailer.rb
module CompanyName
class Mailer < ActionMailer::Base
def test_email(recipient_email_address)
email = mail(to: recipient_email_address, from: "no-reply#companyname.com", subject: "Testing ActionMailer") do |format|
format.html { "<h1>Testing</h1>" }
end
email.deliver_now
end
end
end
mailer_preview.rb
module CompanyName
class MailerPreview < ActionMailer::Preview
def test_email
Mailer.test_email("test#email.com")
end
end
end
After looking at the code for ActionMailer I couldn't find any non-Rails method of configuration, so it looks like this isn't currently possible*. I ended up setting up a Sinatra endpoint to load the HTML from the MailerPreview class and just display it in the browser:
mailers_controller.rb
get "/emails/:email_name" do
halt 404 unless ENV["ENABLE_MAIL_TEST_ENDPOINT"] == "true"
email_preview = CompanyName::MailerPreview.public_send(params["email_name"])
email_preview.html_part.decoded # Note: I switched to the Mail gem instead of ActionMailer, so the code may not be identical
end
*I am not a Rails expert so this may not be the case, however to the best of my abilities I could not see a solution.

Sinatra method `development?` undefined

The Sinatra docs say that development? will return true when the environment is development, but I receive an error stating that the method development? is undefined.
I tried skipping the shorthand and testing the ENV['RAKE_ENV'] variable itself, but it was just nil.
This is the error I'm getting:
undefined method `development?' for main:Object (NoMethodError)
and this is the code that is triggering the error:
require 'dm-sqlite-adapter' if development?
I am using the modular style app. The line above is an separate file that only manages the model. What's going on?
I struggled with this one, too. Here's what I discovered along the way.
You need to be "inside" a class that inherits from Sinatra::Base (such as Sinatra::Application, which inherits from Base) to be able to use the development? method, which is defined in base.rb.
In a classic Sinatra app you are already coding "inside" a class that inherits from Sinatra::Base. So development? will just work "anywhere".
In modular Sinatra the development? will only work in Sinatra::Base sub-classes, such as:
require 'sinatra/base'
# Placing
# require 'dm-sqlite-adapter' if development?
# here will not work.
class ApplicationController < Sinatra::Base
require 'dm-sqlite-adapter' if development? # But here it works
...
end
# Placing
# require 'dm-sqlite-adapter' if development?`
# AFTER the above class will still not work
class SomethingElse
# nor will `development?` work here, since it is called inside
# a class without Sinatra::Base inheritance
...
end
So basically you can use an ApplicationController class that inherits from Sinatra::Base and inside here check for development?. The same goes for sub-classes that inherits from your ApplicationController class:
class UserController < ApplicationController
require 'dotenv' if development?
...
end
For modular Sinatra, in the (main:Object) code text "outside" Sinatra::Base sub-classes, you need to follow Arup's instructions:
if Sinatra::Base.environment == :development
require 'awesome_print'
require 'dotenv'
Dotenv.load
...
end
Since you are using the modular style, you need to add the module namespace Sinatra::Base before the method.
So you will be able to access Sinatra::Base.development? anywhere in the app.

Sinatra routes not working in modular app?

I've converted a classic Sinatra application to a modular application. Now suddenly my put, patch and delete routes stopped working. get and post work fine. Is there something I need to do to get these to work in a modular app that is different from classic?
%form{action: "/addsomething", method: 'post'}
%input{type: 'hidden', name: '_method', value: 'put'}
I get a 404 now when I attempt to access one of the above mentioned routes.
require 'sinatra/base'
class MyClass < Sinatra::Base
put '/addsomething' do
'HELLO WORLD!'
end
get '/hello' do
'hello world'
end
end
The method_override setting (that allows the _method field to override the HTTP method) is false by default in modular style. You need to enable it with:
enable :method_override
You can still inherit from Sinatra::Application in the modular style to keep the default settings:
require 'sinatra/base'
class MyClass < Sinatra::Application
put '/addsomething' do
'HELLO WORLD!'
end
get '/hello' do
'hello world'
end
end

Testing expectations on doubles in Sinatra app using spec

I want to verify that a method was called on a service I want to inject into a Sinatra application using rspec but I can't find an example of how this is done. Here is my spec...
RSpec.configure do |config|
config.include Rack::Test::Methods
end
def app
App
end
describe 'Login' do
context 'when the user is logged out' do
describe 'POST on /signup' do
it 'invokes signup on the user service with the correct parameters' do
service = double('user_service').as_null_object
service.should_receive(:signup).with(:username => 'RobA2345')
post '/signup'
end
end
end
end
Here the App is a modular Sinatra app. I come from a .NET background and I'd use constructor injection here to solve this problem but I know this isn't the ruby way to do it.
Help, as always, is appreciated.
Assuming that you're expecting to receive the message on a new instance of UserService, there are a couple of ways to do this. If you are using a recent version of rspec, this should work:
it 'invokes signup on the user service with the correct parameters' do
UserService.any_instance.should_receive(:signup).with(:username => 'RobA2345')
post '/signup'
end
Alternatively, this should work in just about any version of rspec:
it 'invokes signup on the user service with the correct parameters' do
service = double('user_service').as_null_object
UserService.stub(:new).and_return(service)
service.should_receive(:signup).with(:username => 'RobA2345')
post '/signup'
end

error while using cancan in ruby: "uninitialized constant CanCan::Rule::Mongoid"

here is my controller:
class AdminController < ApplicationController
before_filter :require_user
authorize_resource :class => false
def index
end
def users_list
end
end
here is my Ability class:
class Ability
include CanCan::Ability
def initialize(user)
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
when trying to access "/admin/users_list" (with an admin user or without) i get the following error:
uninitialized constant CanCan::Rule::Mongoid
any thoughts?
Just recently, CanCan added support for Mongoid and renamed CanDefinition to Rule, so the error you are getting indicates you are using the latest CanCan code from the git repo.
Try out CanCan version 1.4 from rubygems and see if that solves the problem. There may be some bug fixes needed before 1.5 is released to rubygems.
UPDATE:
This bug was fixed in CanCan version 1.5.0.beta1.

Resources