May 20, 2008

The Camping Short, Short Example
Camping encourages short, elegant applications. In this example, we’re going to skip the database and put together a simple home page with a few of your favorite links.

The site can be stored in a single file called homepage.rb. Run it like: camping homepage.rb

#!ruby
#!/usr/local/bin/ruby -rubygems
require ‘camping’

Camping.goes :HomePage

module HomePage::Controllers

# The root slash shows the `index’ view.
class Index Views#index
# /sample -> Views#sample
#
class Page ‘http://google.com’ }
li { a ‘A sample page’, :href => ‘/sample’ }
end
end

# The `sample’ view.
def sample
p ‘A sample page’
end
end
This example doesn’t have any Models (the database objects.) Since those are just ActiveRecord objects, it’s probably best to learn about those from the ActiveRecord Base documentation.

Further Examples

Camping includes a number of other larger examples!

Blog: An expanded clone of http://k4ml.com/wiki/python/webpy/simpleblog, which includes sessioning examples.
Tepee: A fine little Wiki, versioning and Textile.
Campsh: A Wiki for storing scripts which can then be run from the browser.

ruby script/generate scaffold ComicPublisher name:string permalink:string albums_count:integer

class ApplicationController ‘f95a48d10f88e6bb443d11646b9d89d9’ #protect_from_forgery :exclude => [:auto_complete_for_publisher_company_name] end —— class PublishersController 20, :order => ‘company_name ASC’ # view # def index @publishers = Publisher.find(:all) end def autocomplete render(:layout => ‘comicmrkt’) end end ——

ComicMrkt.com:

Sidebar

comicmrk@comicmrkt.com [~/site]# rake my_admin:setup
(in /home/comicmrk/site)
To setup RailsMyAdmin, run the generator: ruby script/generate my_admin
The generator will copy MyAdmin files from the generator’s template directory over to your application’s app/ and public/ folders.
comicmrk@comicmrkt.com [~/site]# ruby script/generate my_admin
exists app/controllers/my_admin
exists app/helpers/my_admin
exists app/views/my_admin
exists app/views/my_admin/main
exists app/views/my_admin/model
exists app/views/my_admin/spy
exists public/my_admin
exists public/my_admin/images
identical app/controllers/my_admin/common_controller.rb
identical app/controllers/my_admin/main_controller.rb
identical app/controllers/my_admin/model_controller.rb
identical app/controllers/my_admin/spy_controller.rb
identical app/controllers/my_admin/my_admin_tool.rb
identical app/helpers/my_admin/common_helper.rb
identical app/helpers/my_admin/main_helper.rb
identical app/helpers/my_admin/model_helper.rb
identical app/helpers/my_admin/spy_helper.rb
identical app/views/layouts/my_admin.rhtml
identical app/views/my_admin/main/_ajax.rhtml
identical app/views/my_admin/main/_includes.rhtml
identical app/views/my_admin/main/index.rhtml
identical app/views/my_admin/main/main.rhtml
identical app/views/my_admin/main/models.rhtml
identical app/views/my_admin/model/_form.rhtml
identical app/views/my_admin/model/edit.rhtml
identical app/views/my_admin/model/list.rhtml
identical app/views/my_admin/model/new.rhtml
identical app/views/my_admin/spy/_single.rhtml
identical app/views/my_admin/spy/index.rhtml
identical public/my_admin/my_admin.css
identical public/my_admin/my_admin.js
identical public/my_admin/images/destroy.png
identical public/my_admin/images/edit.png
identical public/my_admin/images/home.png
identical public/my_admin/images/new.png
identical public/my_admin/images/ajax_indicator.gif
comicmrk@comicmrkt.com [~/site]#

fsxtrader

by Shanti A. Braford
http://shanti.railsblog.com/

DESCRIPTION:

* FSX Trader allows you to automate trading on the Fantasy Stock Exchange app on Facebook
* Uses the WWW::Mechanize library (a ruby gem) to simulate a browser logging into Facebook, making trades, etc

FEATURES/PROBLEMS:

* Order types supported: buy, short, sell and cover
* Pulls real-time stock quotes from Yahoo! Finance

SYNOPSIS:

Fantasy Stock Exchange app on Facebook (by HedgeStop.com):
http://apps.facebook.com/hedgestop/

As of this writing, the Fantasy Stock Exchange application on Facebook
does not allow one to place stop-limit orders or otherwise automatically
trigger a buy/sell or cover/short order based upon specified prices.

FSX Trader, when combined with a periodic cron job, can make these trades
for you.

Simply configure your trades in the ‘~/.fsx_trader.yml’ file and they
will happen automatically whenever FSX Trader is run AND conditions for
making a trade (as specified via the config file) have been met.

Note: fsxtrader of course depends upon the external HTML form variables
of the FSX App and Facebook login page. If these variables have changed,
fsxtrader be temporarily broken until the gem can be updated.

A programmatic API into FSX would be ideal, but is currently not yet offered
by HedgeStop.

REQUIREMENTS:

* Currently only works on OS X or Linux
* Other gems required: mechanize and cooloptions

INSTALL:

* sudo gem install fsxtrader
* fsxtrader setup
* Now edit your ’~/.fsx_config.yml’ file, either by hand or using: fsxtrader edit
* Add a cron job to run fsx periodically on your system
* For more options, run: fsxtrader —help

SAMPLE CRON:

Note: market hours are 9:30 AM to 4:00 PM EST

The following cron job would run fsxtrader every ten minutes
during the hours from 7 AM to 3PM (Sample West Coast config):

0-59/10 7-15 * * * fsxtrader

Every five minutes from 9 AM to 4 PM (Sample East Coast config):

0-59/5 9-16 * * * fsxtrader

If the above are giving you problems try doing a ‘which fsxtrader’ and using its
full path in the cron job or try:

0-59/5 9-16 * * * `which fsxtrader` » /path/to/a/fsxlog.txt

SAMPLE CONFIG:

auth:
email: your_fb_email@gmail.com
pass: your_fb_password
trades:
# Format:
# prefix_key: symbol condition price action shares
#
# Options
# symbol - any valid NYSE or Nasdaq ticker tradeable on the FSX
# action - any of ‘sell’, ‘cover’, ‘buy’, ‘short’
# condition - any of ‘>’, ‘>=’, ‘= 57 short 1500
#
# The above config tells FSX Trader to do the following:
# * buy 500 shares of WMT (Wal-Mart Stores Inc) when its share price reaches 18.5 or lower
# * short 1500 shares of WMT (Wal-Mart Stores Inc) when its share prices reaches 57 or greater
#
#
# Configurations to automatically make NEW positions:
#
# Buy 20000 shares of CSCO if its price hits 15 dollars or lower:
buy_low_csco: CSCO = 220 short 1700
#
# Configurations to automatically get out of EXISTING positions:
#
# Sell 2000 shares of GOOG if its price hits 500:
goog: GOOG >= 500 sell 2000
#
# Cover 10000 shares of LEND if its price hits 1.75:
cover_lend: LEND

RailsMyAdmin

RailsMyAdmin is a backend database management addon for your Rails applications.

It’s available in the form of a Rails plugin, with an accompanying generator which copies the various RailsMyAdmin files necessary into subfolders in your application.

It’s inspired by the simplicity and usefulness of phpMyAdmin, but leverages Rails functionality to achieve similar results (and more!).
Installation

Install the plugin with:

ruby script/plugin install http://railsmyadmin.googlecode.com/svn/trunk/my_admin/

Once the plugin is installed, generate the RailsMyAdmin files with:

ruby script/generate my_admin

Note: this will copy files from the plugin folders into your application folders.

Next, copy the following code snippet into the bottom of your environment.rb file:

## MY ADMIN CONFIG
require ‘my_admin/my_admin_tool’

# If you only want certain models to be available to RailsMyAdmin,
# set :all_models to false and specify the desired models in MY_ADMIN_MODELS
MY_ADMIN_GLOBALS = {:all_models => true, :confirm_destroy => false}

# Uncomment the following line if you set :all_models to false above.
#MY_ADMIN_MODELS = [User, Content]
# Replace [User, Content] with your desired array of model classes that
# RailsMyAdmin should be restricted to.

# MY_ADMIN_AUTH must define a Proc object that takes as a paramater
# an ApplicationController instance variable (c - in the example below).
# If you have a method defined in your ApplicationController,
# ‘admin_logged_in?’ for example, the following sample code will
# authenticate against that method and only allow visitors to
# view RailsMyAdmin if the ‘admin_logged_in?’ method returns true.

MY_ADMIN_AUTH = Proc.new { |c| c.send(‘admin_logged_in?’) }

Note: authentication is disabled by default for the Development environment.
Usage

If you are developing your application locally (port 3000), you can test drive railsMyAdmin once the above steps have been completed by visiting:

http://localhost:3000/my_admin/main

Command List

To obtain a list of other RailsMyAdmin rake tasks:

rake -T my_admin

Other Features

RailsMyAdmin features an AJAX-driven DiggSpy-like functionality for watching new rows getting added to tables.

This could come in handy, for say, watching an ‘events’ table on a very busy site, where new rows are getting added every few seconds or minutes.
Works at the Rails Level

RailsMyAdmin uses ActiveRecord object’s methods to edit, update, save and destroy objects.

For example, when an object is destroyed in RailsMyAdmin, the object’s destroy method is called (as opposed to simply deleting the database row), so that any before_destroy and after_destroy hooks are called when the object is destroyed.

RailsMyAdmin

RailsMyAdmin is a backend database management addon for your Rails applications.

It’s available in the form of a Rails plugin, with an accompanying generator which copies the various RailsMyAdmin files necessary into subfolders in your application.

It’s inspired by the simplicity and usefulness of phpMyAdmin, but leverages Rails functionality to achieve similar results (and more!).
Installation

Install the plugin with:

ruby script/plugin install http://railsmyadmin.googlecode.com/svn/trunk/my_admin/

Once the plugin is installed, generate the RailsMyAdmin files with:

ruby script/generate my_admin

Note: this will copy files from the plugin folders into your application folders.

Next, copy the following code snippet into the bottom of your environment.rb file:

## MY ADMIN CONFIG
require ‘my_admin/my_admin_tool’

# If you only want certain models to be available to RailsMyAdmin,
# set :all_models to false and specify the desired models in MY_ADMIN_MODELS
MY_ADMIN_GLOBALS = {:all_models => true, :confirm_destroy => false}

# Uncomment the following line if you set :all_models to false above.
#MY_ADMIN_MODELS = [User, Content]
# Replace [User, Content] with your desired array of model classes that
# RailsMyAdmin should be restricted to.

# MY_ADMIN_AUTH must define a Proc object that takes as a paramater
# an ApplicationController instance variable (c - in the example below).
# If you have a method defined in your ApplicationController,
# ‘admin_logged_in?’ for example, the following sample code will
# authenticate against that method and only allow visitors to
# view RailsMyAdmin if the ‘admin_logged_in?’ method returns true.

MY_ADMIN_AUTH = Proc.new { |c| c.send(‘admin_logged_in?’) }

Note: authentication is disabled by default for the Development environment.
Usage

If you are developing your application locally (port 3000), you can test drive railsMyAdmin once the above steps have been completed by visiting:

http://localhost:3000/my_admin/main

Command List

To obtain a list of other RailsMyAdmin rake tasks:

rake -T my_admin

Other Features

RailsMyAdmin features an AJAX-driven DiggSpy-like functionality for watching new rows getting added to tables.

This could come in handy, for say, watching an ‘events’ table on a very busy site, where new rows are getting added every few seconds or minutes.
Works at the Rails Level

RailsMyAdmin uses ActiveRecord object’s methods to edit, update, save and destroy objects.

For example, when an object is destroyed in RailsMyAdmin, the object’s destroy method is called (as opposed to simply deleting the database row), so that any before_destroy and after_destroy hooks are called when the object is destroyed.

was looking for something like phpmyadmin earlier but gave up: here’s railsmyadmin: http://code.google.com/p/railsmyadmin/

NICE scrubyt tutorial on scraping yahoo finance data… http://www.straw-dogs.co.uk/09/05/scrubyt-tutorial-dogs-of-the-ftse/

Clicky Web Analytics