mail_room with Ruby on Rails - ruby-on-rails

I can't seem to get mail_room working in a Rails app.
When testing the logger delivery method, it works correctly with this
config/mail_room.yml
---
:mailboxes:
-
:email: "some#gmail.com"
:password: "password"
:name: "inbox"
:delivery_method: logger
:log_path: "email.log"
but the postback delivery method doesn't seem to work at all
config/mail_room.yml
---
:mailboxes:
-
:email: "some#gmail.com"
:password: "password"
:name: "inbox"
:delivery_method: postback
:delivery_url: "http://global-or-local-ip/inbox"
:delivery_token: "abcdefg"
config/routes.rb
post 'inbox', :to => 'users#inbox', :as => :users_inbox
app/controllers/users_controller.rb
class UsersController < ApplicationController
def inbox
puts "Check your inbox..."
end
end
Not sure if it's because of mail_room or something missing from the Rails app. I've tried different verbs in the routes. Using Rails 4.0.2 and tried both 0.1.0 and github source for the gem.

After some research, we found that Faraday 0.8.8 was having some issues with ruby 2.0 as we were using it in mail_room. Released a new gem, and made notes in the README that users should install >= 0.8.9 of Faraday.

Related

Non specific MVC framework ruby gems and example code to Rails framework

I have been learning Ruby on Rails, but I still have issues when it comes to Ruby gems with examples that are irb based and not Rails or Sinatra framework based. I am trying to implement the Block.io Bitcoin API functionality. But the code I find is Ruby only, so I am not sure where to create a config file for the API Key and also whether I need to create a controller to make this work in the views for Rails.
The gem and examples are on: https://github.com/BlockIo/gem-block-io
I installed this gem via bundle install on Rails
gem install block_io -v=1.0.6
The Ruby example show the following:
>> require 'block_io'
>> BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2
In Rails which config file would I enter the above api_key and pin?
In the example they show the code to get your address as follows:
BlockIo.get_my_address
Do I need to create a function in a controller such as:
def address
#my_address = BlockIo.get_my_addresses
end
and in the view use:
<%= #my_address %>
I need some guidance with regards to the above, any comment or assistance will be greatly appreciated.
require 'block_io' can go into Gemfile like gem 'block_io'. Rails/bundler will require it automaticaly for you as long as the gem name is also the file name you want to require from this gem.
BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2 can be put into an initilizer like config/initializers/block_io.rb. This way set_options is called only once when Rails starts a server or console or runner.
Put it like this into the file config/initializers/block_io.rb
BlockIo.set_options :api_key=> ENV['BLOCK_IO_API_KEY'], :pin => ENV['BLOCK_IO_PIN'], :version => 2
With the environment variables in use you don't commit any secret into your repo.
Now you should be able to call BlockIo.get_my_address within any action.

Middleware not always called in capybara feature test (with rack-test)

I have a gem I'm working on that uses a railtie to add a middleware. Very simple stuff, followed the rails guides section almost exactly. Works fine in development/staging/production.
The middleware initializes a hash-like object in the env at a particular key.
But in my capybara tests, this key is only sometimes initialized. I added a debugger to the middleware and found that it isn't called every time I use the visit method.
What's more is that in this particular spec file, there are 4 examples, and each example calls visit once. But when I run the spec file, the middleware is sometimes called 3 times and sometimes called 2 times. Obviously the middleware stack should be called for every request.
tl;dr: sometimes calling visit in my capybara feature specs (with rack-test driver) does not result in my middleware stack being called.
Help?
ruby 2.0.0-p353
rails 4.0.2
capybara 2.2.1
rack-test 0.6.2
EDIT: This is some of the relevant code here: how the middleware is added and what it does. MyGem::Middleware#call is only sometimes called when using Capybara's visit method.
# railtie.rb
module MyGem
class Railtie < Rails::Railtie
initializer "my_gem.configure_rails_initialization" do |app|
app.middleware.use MyGem::Middleware
end
end
end
# middleware.rb
module MyGem
class Middleware
def initialize(app, options={})
#app = app
# options unused
end
def call(env)
# using a special internal version of the Rack::Session::Cookie class
session = MyGem::Rack::Session::Cookie.new(
#app,
:coder => MyGem::Rack::Session::Cookie::Base64::Marshal.new,
:key => ENV_SESSION_KEY,
:path => '/',
:domain => domain(env),
:expire_after => 6.weeks.to_i, # seconds till this expires
:secret => 'my_gem_secret_14f1c4ad25a6be00fe53f5fd2d746167',
)
# use Rack::Session:Cookie method
session.context(env, #app)
end
end
end
Figured it out!
I was also adding a Warden hook that expected the env key to be added after signing users in and out, and if the Warden::Manager middleware was added prior to my gem's middleware, then it would error out when running my hook that expected that env key to be set.
Solution was to do this in my railtie:
app.middleware.insert_before Warden::Manager, MyGem::Middleware

Capybara/Cucumber : RoutingError for nested resources in a namespace

I'm using Capybara/Cucumber on Rails 3.2 and I'm facing a weird routing error.
I have the following routes defined :
#routes.rb
namespace :super_user do
...
resources :events do
resources :invites
end
end
...
resources :invites
and the following Cucumber feature :
#in_progress #current
Scenario: I can invite a USER by email
Given the following event exists:
| Name |
| The Event |
And I go to the event page for "The Event"
And I follow "Invite new user"
And I fill in "invite_email" with "user#domain.com"
...
The event page (EventsController#show) contains a link to the invites#new action :
#app/views/super_user/events/show.html.erb
...
<%= content_for :button_bar do %>
<%= link_to( 'Invite new user', new_super_user_event_invite_path(#event) ) %>
<% end %>
Everything is working properly when I test the /super_user/events/1 action manually, but whenever I run cucumber I get :
And I follow "Invite new user" # features/step_definitions/web_steps.rb:45
uninitialized constant SuperUser::InvitesController (ActionController::RoutingError)
(eval):2:in `click_link'
./features/step_definitions/web_steps.rb:46:in `/^(?:|I )follow "([^"]*)"$/'
features/create_casino_super_user.feature:24:in `And I follow "Invite new user"'
Why does routing behave differently when using Cucumber/Capybara ? How could I fix this feature ?
Relevant parts of bundle list :
* cucumber (1.0.6)
* cucumber-rails (1.0.2)
* capybara (1.0.1)
* capybara-webkit (0.6.1 dfa0624)
* rails (3.2.1)
EDIT
Side note : the InvitesController class is not in the SuperUser module but as I said previously it works when testing manually.
I'm on Rails 3 (not 3.2), coming from 2.3 and just jumping to the new routing DSL. I came across a very similar issue where our resource-in-a-namespace routes work when hit directly but not from inside Cucumber/Capybara.
In the end, I pulled the default routes from Rails 2.3 and made them active only inside cucumber, which seems to work:
# Cucumber doesn't understand the Rails 3 default route, above, so use the old way to make that work
# TODO remove this when we can/must, and hope that Cucumber is smarter by then
if File.basename($0) == "cucumber"
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
end
Not sure that's an option for you (map.connect is part of the old API, which I think disappears in 3.1), but I wanted to put it on the internet somewhere for those who come looking.

Rails3 - oauth-plugin problem

I'm trying to use oauth-plugin on a Rails application I'm developing, but I keep running into problems.
To make sure I'm not making any mistake, I started an application from scratch (using Rails 3.0.3). Here are the steps I followed:
Create da new rails application (rails.test)
Edited its Gemfile to include:
gem "oauth-plugin", ">=0.4.0.pre1"
gem "oauth", "0.4.4"
Generated oauth-consumer, by running script/rails g oauth_consumer
Edited oauth_consumers.rb to include my keys for Google integration:
:google=>{
:key=>"anonymous",
:secret=>"anonymous",
:scope=>"https://www.google.com/calendar/feeds/",
:options => {
:site => "http://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path=> "/accounts/OAuthAuthorizeToken"
},
}
Edited routes.rb to add the route for oauth_consumer:
resources :oauth_consumers
Edited application_controller.rb to implement the logged_in? method as follows:
def logged_in?
true
end
Now when I access http://localhost:3000/oauth_consumers/google I get the following error:
uninitialized constant GoogleToken
Does anyone know what causes this error and how can I fix it? GoogleToken is a class that should have been auto generated by oauth-plugin, so I can't tell why I'm getting this uninitialized constant error.
The GoogleToken class doesn't get auto-generated unless you pass "google" to the generator like so:
script/rails g oauth_consumer google
or for rails 3:
rails g oauth_consumer google
Also check to ensure the relationship is set up in the user model like so:
has_one :google, :class_name => "GoogleToken", :dependent => :destroy
Did you remember to run bundle install from terminal after editing your Gemfile? Sounds like your Rails app doesn't know about these gems yet.
I have the same problem, i think a solution could be in this:
https://github.com/pelle/oauth-plugin/blob/master/lib/generators/oauth_consumer/USAGE
You need some sort of authentication like restful-authentication plugin, if you uncomment line 27..29 in your oauth_consumers_controller.rb file, you'll jump to next step!

Sinatra app as Rails 3 subpath

I'm trying to get a sinatra app as a subpath in my rails 3 app.
Specifically, the resque queuing system has a sinatra based web interface that I would like to have accessible through /resque on my usual rails app.
You can see the project here: http://github.com/defunkt/resque
I found some people talking about adding a rackup file and doing this sort of thing:
run Rack::URLMap.new( \
"/" => ActionController::Dispatcher.new,
"/resque" => Resque::Server.new
)
But I don't really know where to put that or how to make it run. My deployment is with passenger, but it would me nice to also have it running when I run 'rails server' too. Any suggestions?
--edit--
I've made some progress by putting the following in config/routes.rb:
match '/resque(/:page)', :to => Rack::URLMap.new("/resque" => Resque::Server.new)
Which seems to work pretty well, however it loses the public folder, (which is defined within the gem I guess), and as a result, there is no styling information, nor images.
You can setup any rack endpoint as a route in rails 3. This guide by wycats goes over what you are looking for and many of the other things you can do in rails3:
http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/
For example:
class HomeApp < Sinatra::Base
get "/" do
"Hello World!"
end
end
Basecamp::Application.routes do
match "/home", :to => HomeApp
end
Yehuda (/Scott S)'s solution doesn't work for me with Rails 3.0.4 and Sinatra 1.2.1... setting :anchor => false in the matcher is the key:
# in routes.rb
match "/blog" => MySinatraBlogApp, :anchor => false
# Sinatra app
class MySinatraBlogApp < Sinatra::Base
# this now will match /blog/archives
get "/archives" do
"my old posts"
end
end
(answer c/o Michael Raidel - http://inductor.induktiv.at/blog/2010/05/23/mount-rack-apps-in-rails-3/)

Resources