How To: Build shell equivalent of a rails app

Hey folks, I have an application that I have written in Rails and I am
wondering about the feasability of writing an equivalent as a shell
app using rails concepts. The initial app was interactive and made
sense beign implemented for the web, but this new app needs to be run
from the command line with its output dumped to stdout.

I have played around with ActiveRecord and understand how to use it in
a ruby shell app, but I am not sure how I might be able to get
ActionView to work or similiar behavior to allow a controller and view
to be implemented as in a rails app.

Does anyone know of an example I can look at which does the above, or
if not does anyone have suggestions for where I can look for answers?

Thanks in advance for any help you can offer!
-Andy

Hey Andy,

I've got a little script I call controller_instance you can have if
you wish, it can be used like so:

script/controller_instance RssController google_base_feed -e
production

This spits out the response from the google_base_feed action of the
Rss controller (if that wasn't obvious:-)

I use it in several cron jobs... something like this

open(rss_file, 'w') do | f |
  cmd = open("|"+rootd +"script/controller_instance RssController
google_base_feed -e #{ENV['RAILS_ENV'] == 'production' ?
'production' : 'development'}")
  while string = cmd.gets
    f.write(string)
  end
  cmd.close
end

This avoids having to use a tool such as curl which in my opinion is a
convoluted way to hit your own local server.

I dug this up after >much< looking on google. I didn't write it and
can't recall who did but I emailed and thanked him when I found it and
he was happy to share.

If you would like it please email me at tmac at easystreet dot com.

Tim

Thanks guys, looks like some promising options!

-Andy

Hey again, as my luck goes I am having problems with both of the
suggested routes and am wondering if
you guys might have any further suggestions for what needs to be
tweaked.

1. Tim, with the controller_instance script you sent me I see this
problem when I try to use it with a basic rails app that I created
just to test with it, has a model tied to a database table and just
iterates through some of the records and displays two of the fields
( in the web app I see it working fine ).
When I try this:

ruby script/controller_instance MainController index -e development

I see this error:

undefined method `camelize' for "top":String

I googled the error and the only proposed solutions I could find were
not applicable to my situation so am unsure what might be causing
this.

2. Daniel, I also tried script/console and it immediately generates
the following error:

Loading development environment.
/usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/console.rb:
25:in `exec': No such file or directory - irb -r irb/completion -r
script/../config/../config/environment -r console_app -r
console_with_helpers --simple-prompt (Errno::ENOENT)
        from /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/
commands/console.rb:25
        from /usr/local/lib/ruby/site_ruby/1.8/rubygems/
custom_require.rb:27:in `require'
        from script/console:3

Do I have an inferior rails installation? :wink: I have a gem-installed
rails version 1.2.3

Anyway, wondering if any of this rings a bell, maybe there is
something easy to tweak to fix it.

Thanks again folks!

-Andy

Hey Andy,

Sorry I didn't notice you posted this question/issue, I just happened
across it while looking for something else.

I suspect you've already found a solution but anyway... as far as the
script issue ...you could probably do something like:

require 'active_support/core_ext/string/inflections'
include ActiveSupport::CoreExtensions::String::Inflections

or perhaps

require 'active_support/inflector'
include ActiveSupport::Inflector

or something similar.

Should give the script access to the camelize method.

best!
Tim