1

I have a modular Sinatra app and am looking for the most straightforward way to call Sinatra API-level methods (such as get()) from inside other modules. I figured Sinatra extensions would be it, but something's not working.

This is the main file that imports all other modules:

app/app.rb

require 'sinatra/base'
Dir[File.join(__dir__, 'articles', '*.rb')].each { |file| require file }

module Tir
  class App < Sinatra::Base
    register Sinatra::Blog

    get '/articles' do
      articles = parse_articles('articles/*.mdown')
    end

  end 
end 

This is the method from inside which I'm calling get() — and cannot:

app/articles/blog.rb

module Sinatra
  module Blog

  ARTICLES = []

  def parse_articles(dir)
    Dir.glob(dir) do |file|
      article = initialize_article file
      if article.ready?
        generate_path(article)
        ARTICLES << article
      end
    end
  end

  def generate_path(article)
    get("/#{article.relative_path}") do
      erb :'articles/article',
      :locals => { :article => article },
      :layout => :'articles/layout_article'
    end
  end

  end
  register Blog
end

config.ru

require 'rubygems'
require 'bundler'

Bundler.require

require_relative 'app/app'

run Tir::App

When I run this, I get undefined method 'parse_articles' for #<Tir::App:0x00007f9b6536c5d8>.

What's not right here? Thanks.

fullstackplus
  • 1,061
  • 3
  • 17
  • 31
  • This seems correct to me, what’s happening when you run this? – matt Aug 28 '20 at 17:43
  • I am sorry, my question was a bit misleading. It's not the get() method that isn't working but the extension methods in general. I've modified the source code listing accordingly. When I run the above, I get "undefined method `parse_articles' for #" – fullstackplus Aug 30 '20 at 07:26

0 Answers0