Sinatra app with sprockets not working on Heroku - ruby

I have a little Sinatra app including this module:
module Sprockets
module Helpers
def asset_path(source)
"/assets/#{Environment.instance.find_asset(source).digest_path}"
end
def sprockets
Environment.instance.call(env)
end
end
class << self
def precompile
dir = 'public/assets'
FileUtils.rm_rf(dir, secure: true)
::Sprockets::StaticCompiler.new(Environment.instance, 'public/assets', [/\.(png|jpg)$/, /^(application|ie)\.(css|js)$/]).compile
end
end
class Environment < ::Sprockets::Environment
include Singleton
def initialize
super
%w[app lib vendor].each do |dir|
%w[images javascripts stylesheets].each do |type|
path = File.join(root, dir, 'assets', type)
append_path(path) if File.exist?(path)
end
end
js_compressor = Uglifier.new
css_compressor = YUI::CssCompressor.new
context_class.instance_eval do
include Helpers
end
end
end
end
and with following route defined:
get('/assets/*') do
sprockets # Defined in the module above
end
Everything works just great, assets are loaded and displayed properly on my local machine using pow. But on Heroku no single asset is loaded, the server just returns 404 for every asset file.

Simplified the module and now it works! Weird...
class Assets < Sprockets::Environment
class << self
def instance(root = nil)
#instance ||= new(root)
end
end
def initialize(root)
super
%w[app lib vendor].each do |dir|
%w[images javascripts stylesheets].each do |type|
path = File.join(root, dir, 'assets', type)
self.append_path(path) if File.exist?(path)
end
end
self.css_compressor = YUI::CssCompressor.new
self.js_compressor = Uglifier.new
context_class.instance_eval do
include Helpers
end
end
def precompile
dir = 'public/assets'
FileUtils.rm_rf(dir, secure: true)
Sprockets::StaticCompiler.new(self, 'public/assets', ['*']).compile
end
module Helpers
def asset_path(source)
"/assets/#{Assets.instance.find_asset(source).digest_path}"
end
end
end

Related

Capybara.page not in scope after extending capybara-screenshot's after_failed_example method

I'm trying to override the after_failed_example method so I can inflict some custom file naming on our screenshots. I'm loading the module as an initializer.
So far, so good, but the Capybara.page.current_url is blank, making me think I need to require something additional?
require "capybara-screenshot/rspec"
module Capybara
module Screenshot
module RSpec
class << self
attr_accessor :use_description_as_filename
attr_accessor :save_html_file
end
self.use_description_as_filename = true
self.save_html_file = true
def self.after_failed_example(example)
if example.example_group.include?(Capybara::DSL) # Capybara DSL method has been included for a feature we can snapshot
Capybara.using_session(Capybara::Screenshot.final_session_name) do
puts ">>>> Capybara.page.current_url: " + Capybara.page.current_url.to_s
if Capybara::Screenshot.autosave_on_failure && failed?(example) && Capybara.page.current_url != ''
saver = Capybara::Screenshot.new_saver(Capybara, Capybara.page, Capybara::Screenshot.save_html_file?, set_saver_filename_prefix(example))
saver.save
example.metadata[:screenshot] = {}
example.metadata[:screenshot][:html] = saver.html_path if saver.html_saved?
example.metadata[:screenshot][:image] = saver.screenshot_path if saver.screenshot_saved?
end
end
end
private
def self.set_saver_filename_prefix(example)
return example.description.to_s.gsub(" ", "-") if Capybara::Screenshot.use_description_as_filename?
return Capybara::Screenshot.filename_prefix_for(:rspec, example)
end
end
end
end
end
This is successfully overriding the capybara-screenshot/rspec method, and any of the Capybara::Screenshot static information is accessible, but not Capybara session related information (afa I can tell).
For example, Capybara.page.current_url.to_s is null when overridden, but present when not.
I was missing a require (kind of silly mistake):
require 'capybara/rspec'

Calling a helper method from a route in Sinatra

I've set up a simple Sinatra app based on this excellent SO answer. My code is working and looks like this:
# app.rb
require 'sinatra'
class MyApp < Sinatra::Application
set :public_folder, Proc.new { File.join(root, "app/public") }
set :views, Proc.new { File.join(root, "app/views") }
register Sinatra::Namespace
register Sinatra::Flash
enable :sessions
end
require_relative 'app/helpers/init'
require_relative 'app/models/init'
require_relative 'app/routes/init'
Then I have a dirty image uploader in a helper, which is being required in app/helpers/init.rb
# app/helpers/image.rb
require 'imgur'
module ImageUploader
def save(image)
#filename = image[:filename]
file = image[:tempfile]
File.open("#{ENV['PHOTO_TMP_DIR']}/#{#filename}", 'wb') do |f|
f.write(file.read)
upload(#filename)
end
end
def upload(filename)
client = Imgur.new(ENV['IMGUR_CLIENT_ID'])
image = Imgur::LocalImage.new("#{ENV['PHOTO_TMP_DIR']}/#{#filename}")
uploaded = client.upload(image)
File.delete("#{ENV['PHOTO_TMP_DIR']}/#{#filename}")
uploaded.link
end
end
And I'm successfully calling the save method in my app/routes/admin.rb file, like so:
# app/routes/admin.rb
class MyApp < Sinatra::Application
...
imgur_url = save(params[:image])
...
end
The problem is that save method name is so generic. I've tried calling with ImageUploader::save and ImageUploader.save, but they both throw errors. Is there another way I can call this helper method and have it namespaced to the helper module?
I should note that I'm loading the helper method like this:
# app/helpers/init.rb
require_relative 'image'
MyApp.helpers ImageUploader
Figured it out! To namespace the module methods, put self in front of the method name. Now doing:
# app/helpers/image.rb
require 'imgur'
module ImageUploader
def self.save(image)
#filename = image[:filename]
file = image[:tempfile]
File.open("#{ENV['PHOTO_TMP_DIR']}/#{#filename}", 'wb') do |f|
f.write(file.read)
upload(#filename)
end
end
def self.upload(filename)
client = Imgur.new(ENV['IMGUR_CLIENT_ID'])
image = Imgur::LocalImage.new("#{ENV['PHOTO_TMP_DIR']}/#{#filename}")
uploaded = client.upload(image)
File.delete("#{ENV['PHOTO_TMP_DIR']}/#{#filename}")
uploaded.link
end
end
Allows me to call ImageUploader.save without any errors.

Ruby: const_set outside block?

I want to mock a class with Ruby.
How do I write a method that will take care of the boilerplate code?
The following code:
module Mailgun
end
module Acani
def self.mock_mailgun(mock)
temp = Mailgun
const_set(:Mailgun, mock)
p Mailgun
yield
ensure
const_set(:Mailgun, temp)
end
end
Acani.mock_mailgun('mock') { p Mailgun }
prints:
"mock"
Mailgun
What's going on here? Why is Mailgun its original value inside the block? Does this have to do with Ruby bindings?
Ruby version: 2.1.1p76
Try putting Object. before each const_set.
The code in the question is simplified. Here is the pertinent code:
test/test_helper.rb
require 'minitest/autorun'
module Acani
def self.const_mock(const, mock)
temp = const_get(const)
const_set_silent(const, mock)
yield
ensure
const_set_silent(const, temp)
end
private
def self.const_set_silent(const, value)
temp = $VERBOSE
$VERBOSE = nil
Object.const_set(const, value)
ensure
$VERBOSE = temp
end
end
test/web_test.rb
require 'test_helper'
require 'rack/test'
require_relative '../web'
class AppTest < MiniTest::Test
include Rack::Test::Methods
def app
Sinatra::Application
end
def test_password_reset
post '/users', {email: 'user1#gmail.com', password: 'password1'}
mailgun_mock = MiniTest::Mock.new
mailgun_mock.expect(:send, 200, [Hash])
Acani.const_mock(:Mailgun, mailgun_mock) do
post '/password_resets', {email: 'user1#gmail.com'}
end
mailgun_mock.verify
assert_equal 201, last_response.status
end
end

Test helpers with RSpec in Padrino (Sinatra)

I'm trying to test a helper in a Padrino (Sinatra) app. My helper method is itself calling Padrino core helper methods but they are undefined. The error appears only in RSpec, while the app works fine. So the way I'm including my helper in RSpec makes it loose "Padrino scope" but I don't know how to bring Padrino helper's scope properly in my RSpec environment.
My helper:
module AdminHelper
Sort = Struct.new(:column, :order)
def sort_link(model, column)
order = sorted_by_this?(column) ? 'desc' : 'asc'
link_to mat(model, column), url(:pages, :index, sort: column, order: order)
end
def sorted_by_this?(column)
column.to_s == #sort.column && #sort.order == 'asc'
end
end
Lenstroy::Admin.helpers AdminHelper
My spec:
describe AdminHelper do
before(:all) do
class AdminHelperClass
include AdminHelper
end
end
subject(:helper) { AdminHelperClass.new }
describe '#sort_link' do
context "with :pages and :title parameters" do
before do
sort = AdminHelperClass::Sort.new('title', 'asc')
helper.instance_variable_set('#sort', sort)
end
subject { helper.sort_link(:pages, :title) }
it { should match(/<a href=([^ ]+)pages/) }
end
end
end
Results in error:
1) AdminHelper#sort_link with :pages and :title parameters
Failure/Error: subject { helper.sort_link(:pages, :title) }
NoMethodError:
undefined method `mat' for #<AdminHelperClass:0x007f1d951dc4a0>
Including a helper where mat is defined doesn't work, as one method is dependent on another helper and it goes on and on...
Update
In my spec helper I have:
def app(app = nil, &blk)
#app ||= block_given? ? app.instance_eval(&blk) : app
#app ||= Lenstroy::Admin
#app.register Padrino::Helpers
#app.register Padrino::Rendering
#app
end
in my spec I have:
it "returns link to resource with sort parameters" do
app do
get '/' do
sort_link(:pages, :title)
end
end
get "/"
last_response.body.should =~ /<a href=([^ >]+)pages/
end
And now tests fail, last_response.body is ''.
Method #mat is defined in Padrino::Admin::Helpers::ViewHelpers. You can do
class AdminHelperClass
include Padrino::Admin::Helpers::ViewHelpers
include AdminHelper
end
Update:
If your methods are really dependent on all these routes and helpers you should consider doing full mockup of your app like this:
def mock_app(base=Padrino::Application, &block)
#app = Sinatra.new(base, &block)
#app.register Padrino::Helpers
#app.register Padrino::Rendering
# register other things
end
def app
Rack::Lint.new(#app)
end
mock_app do
get '/' do
sort_link(my_model, my_column)
end
end
get "/"
assert_equal "some test text", body
Here's how it's done in padrino-admin: https://github.com/padrino/padrino-framework/blob/master/padrino-admin/test/test_admin_application.rb
I was having the same problem (and getting very frustrated tracking down the modules and including them). So far, I've got my specs working by:
1) Explicitly defining my module (as explained in how to use padrino helper methods in rspec)
module MyHelper
...
end
MyApp::App.helpers MyHelper
2) Automatically including helpers at the top of my spec. (Right now I only have one helper spec, but in the future I might try to move this into spec_helper.rb.)
describe MyHelper do
let(:helpers) { Class.new }
before { MyApp::App.included_modules.each { |m| helpers.extend m } }
subject { helpers }
it 'blah' do
expect(subject.helper_method).to eq 'foo'
end
end

How mix in routes in Sinatra for a better structure

I found nothing about how I can mix-in routes from another module, like this:
module otherRoutes
get "/route1" do
end
end
class Server < Sinatra::Base
include otherRoutes
get "/" do
#do something
end
end
Is that possible?
You don't do include with Sinatra. You use extensions together with register.
I.e. build your module in a separate file:
require 'sinatra/base'
module Sinatra
module OtherRoutes
def self.registered(app)
app.get "/route1" do
...
end
end
end
register OtherRoutes # for non modular apps, just include this file and it will register
end
And then register:
class Server < Sinatra::Base
register Sinatra::OtherRoutes
...
end
It's not really clear from the docs that this is the way to go for non-basic Sinatra apps. Hope it helps others.
You could do this:
module OtherRoutes
def self.included( app )
app.get "/route1" do
...
end
end
end
class Server < Sinatra::Base
include OtherRoutes
...
end
Unlike Ramaze, Sinatra's routes are not methods, and so cannot use Ruby's method lookup chaining directly. Note that with this you can't later monkey-patch OtherRoutes and have the changes reflected in Server; this is just a one-time convenience for defining the routes.
I prefer the use of sinatra-contrib gem to extend sinatra for cleaner syntax and shared namespace
# Gemfile
gem 'sinatra', '~> 1.4.7'
gem 'sinatra-contrib', '~> 1.4.6', require: 'sinatra/extension'
# other_routes.rb
module Foo
module OtherRoutes
extend Sinatra::Extension
get '/some-other-route' do
'some other route'
end
end
end
# app.rb
module Foo
class BaseRoutes < Sinatra::Base
get '/' do
'base route'
end
register OtherRoutes
end
end
sinata-contrib is maintained alongside the sinatra project
Well you can also use the map method to map routes to your sinatra apps
map "/" do
run Rack::Directory.new("./public")
end
map '/posts' do
run PostsApp.new
end
map '/comments' do
run CommentsApp.new
end
map '/users' do
run UserssApp.new
end
Just my two cents:
my_app.rb:
require 'sinatra/base'
class MyApp < Sinatra::Base
set :root, File.expand_path('../', __FILE__)
set :app_file, __FILE__
disable :run
files_to_require = [
"#{root}/app/helpers/**/*.{rb}",
"#{root}/app/routes/**/*.{rb}"
]
files_to_require.each {|path| Dir.glob(path, &method(:require))}
helpers App::Helpers
end
app/routes/health.rb:
MyApp.configure do |c|
c.before do
content_type "application/json"
end
c.get "/health" do
{ Ruby: "#{RUBY_VERSION}",
Rack: "#{Rack::VERSION}",
Sinatra: "#{Sinatra::VERSION}"
}.to_json
end
end
app/helpers/application.rb:
module App
module Helpers
def t(*args)
::I18n::t(*args)
end
def h(text)
Rack::Utils.escape_html(text)
end
end
end
config.ru:
require './my_app.rb'

Resources