Calling a helper method from a route in Sinatra - ruby

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.

Related

How do I reference a method in a different class from a method in another class?

I have a module and class in a file lib/crawler/page-crawler.rb that looks like this:
require 'oga'
require 'net/http'
require 'pry'
module YPCrawler
class PageCrawler
attr_accessor :url
def initialize(url)
#url = url
end
def get_page_listings
body = Net::HTTP.get(URI.parse(#url))
document = Oga.parse_html(body)
document.css('div.result')
end
newpage = PageCrawler.new "http://www.someurl"
#listings = newpage.get_page_listings
#listings.each do |listing|
bizname = YPCrawler::ListingCrawler.new listing['id']
end
end
end
Then I have another module & class in another file lib/crawler/listing-crawler.rb that looks like this:
require 'oga'
require 'pry'
module YPCrawler
class ListingCrawler
def initialize(id)
#id = id
end
def extract_busines_name
binding.pry
end
end
end
However, when I try to run this script ruby lib/yp-crawler.rb which executes the page-crawler.rb file above and works without the YPCrawler call, I get this error:
/lib/crawler/page-crawler.rb:23:in `block in <class:PageCrawler>': uninitialized constant YPCrawler::ListingCrawler (NameError)
The issue is on this line:
bizname = YPCrawler::ListingCrawler.new listing['id']
So how do I call that other from within my iterator in my page-crawler.rb?
Edit 1
When I just do `ListingCrawler.new listing['id'], I get the following error:
uninitialized constant YPCrawler::PageCrawler::ListingCrawler (NameError)
Edit 2
Here is the directory structure of my project:
Edit 3
My yp-crawler.rb looks like this:
require_relative "yp-crawler/version"
require_relative "crawler/page-crawler"
require_relative "crawler/listing-crawler"
module YPCrawler
end
In your yp-crawler.rb file, based on the structure that you posted, you should have something like:
require 'yp-crawler/version'
require 'crawler/listing-crawler'
require 'crawler/page-crawler'
Try this, in your yp-crawler.rb add the line:
Dir["#{File.dirname(__FILE__)}/crawler/**/*.rb"].each { |file| load(file) }
That should automatically include all files in your /crawler directory at runtime. Might want to do the same for the other directories.
Let me know if that helps :)

How to map routes to modules without the use of multiple Sinatra apps?

I have this structure:
module Analytics
def self.registered(app)
module DepartmentLevel
departmentParticipation = lambda do
end
departmentStatistics = lambda do
end
app.get '/participation', &departmentParticipation
end
module CourseLevel
courseParticipation = lambda do
end
end
end
And at the end of the module Analytics I would like to route each piece of the request to his specific subModule. If it is requested
'analytics/department'
it should redirect to the module DepartmentLevel which has its own routes as
app.get 'participation', &departmentParticipation
I first thought on using map. But how to use it without having to run a new or inherit Sinatra::Base object?
Not sure if this is what you need, but here's how I build my modular Sinatra apps: By using use
First, I have my ApplicationController. It's the base class for all other Controllers. It lives in controllers/application_controller.rb
class ApplicationController < Sinatra::Base
# some settings that are valid for all controllers
set :views, File.expand_path('../../views', __FILE__)
set :public_folder, File.expand_path('../../public', __FILE__)
enable :sessions
# Helpers
helpers BootstrapHelpers
helpers ApplicationHelpers
helpers DatabaseHelpers
configure :production do
enable :logging
end
end
Now, all other Controllers/Modules inherit from ApplicationController. Example controllers/website_controller.rb:
require 'controllers/application_controller'
class WebsiteController < ApplicationController
helpers WebsiteHelpers
get('/') { slim :home }
get('/updates') { slim :website_updates }
get('/test') { binding.pry; 'foo' } if settings.development?
end
At last, in app.rb is where it all comes together:
# Require some stuff
require 'yaml'
require 'bundler'
Bundler.require
require 'logger'
# Require own stuff
APP_ROOT = File.expand_path('..', __FILE__)
$LOAD_PATH.unshift APP_ROOT
require 'lib/core_ext/string'
require 'controllers/application_controller.rb'
# Some Run-Time configuration...
ApplicationController.configure do
# DB Connections, Logging and Stuff like that
end
# Require ALL models, controllers and helpers
Dir.glob("#{APP_ROOT}/{helpers,models,controllers}/*.rb").each { |file| require file }
# here I glue everything together
class MyApp < Sinatra::Base
use WebsiteController
use OtherController
use ThingController
not_found do
slim :'404'
end
end
With this Setup, all I need to do in config.ru is
require './app.rb'
run MyApp
Hope this helps!

How to request separate folder view path based on controller name in Sinatra?

Here's the contents of my app/controllers/application_controller.rb:
require 'sinatra/base'
require 'slim'
require 'colorize'
class ApplicationController < Sinatra::Base
# Global helpers
helpers ApplicationHelper
# Set folders for template to
set :root, File.expand_path(File.join(File.dirname(__FILE__), '../'))
puts root.green
set :sessions,
:httponly => true,
:secure => production?,
:expire_after => 31557600, # 1 year
:secret => ENV['SESSION_SECRET'] || 'keyboardcat',
:views => File.expand_path(File.expand_path('../../views/', __FILE__)),
:layout_engine => :slim
enable :method_override
# No logging in testing
configure :production, :development do
enable :logging
end
# Global not found??
not_found do
title 'Not Found!'
slim :not_found
end
end
As you can see I'm setting the views directory as:
File.expand_path(File.expand_path('../../views/', __FILE__))
which works out to be /Users/vladdy/Desktop/sinatra/app/views
In configure.ru, I then map('/') { RootController }, and in said controller I render a view with slim :whatever
Problem is, all the views from all the controllers are all in the same spot! How do I add a folder structure to Sinatra views?
If I understand your question correctly, you want to override #find_template.
I stick this function in a helper called view_directory_helper.rb.
helpers do
def find_template(views, name, engine, &block)
views.each { |v| super(v, name, engine, &block) }
end
end
and when setting your view directory, pass in an array instead, like so:
set :views, ['views/layouts', 'views/pages', 'views/partials']
Which would let you have a folder structure like
app
-views
-layouts
-pages
-partials
-controllers
I was faced with same task. I have little experience of programming in Ruby, but for a long time been working with PHP. I think it would be easier to do on it, where you can easily get the child from the parent class. There are some difficulties. As I understand, the language provides callback functions like self.innereted for solving of this problem. But it did not help, because I was not able to determine the particular router in a given time. Maybe the environment variables can help with this. But I was able to find a workaround way to solve this problem, by parsing call stack for geting caller class and wrapping output function. I do not think this is the most elegant way to solve the problem. But I was able to realize it.
class Base < Sinatra::Application
configure do
set :views, 'app/views/'
set :root, File.expand_path('../../../', __FILE__)
end
def display(template, *args)
erb File.join(current_dir, template.to_s).to_sym, *args
end
def current_dir
caller_class.downcase!.split('::').last
end
private
def caller_class(depth = 1)
/<class:([\w]*)>/.match(parse_caller(caller(depth + 1)[1]))[1]
end
def parse_caller(at)
Regexp.last_match[3] if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
end
end
The last function is taken from here. It can be used as well as default erb function:
class Posts < Base
get '/posts' do
display :index , locals: { variables: {} }
end
end
I hope it will be useful to someone.

Access Sinatra settings from a model

I have a modular Sinatra app. I'm setting some custom variables in my configure block and want to access these settings in my model.
The problem is, I get a NoMethodError when I try and access my custom settings from MyModel. Standard settings still seem to work fine though. How can I make this work?
# app.rb
require_relative 'models/document'
class App < Sinatra::Base
configure do
set :resource_path, '/xfiles/i_want_to_believe'
end
get '/' do
#model = MyModel.new
haml :index
end
end
# models/my_model.rb
class MyModel
def initialize
do_it
end
def do_it
...
settings.resource_path # no method error
...
settings.root # works fine
end
end
i think that you should be able to access it via
Sinatra::Application.settings.documents_path
I ended up doing:
#document.rb
class Document
def self.documents_path=(path)
#documents_path = path
end
def self.documents_path
#documents_path
end
...
end
#app.rb
configure do
set :documents_path, settings.root + "/../documents/"
Document.documents_path = settings.documents_path
end
then just using Document.documents_path inside my find method.

Sinatra app with sprockets not working on Heroku

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

Resources