List of Listings – Ruby in Practice

List of Listings

Listing 1.1. SQL for graph example database

Listing 1.2. Setting up our database with ActiveRecord

Listing 1.3. Generating a graph with Scruffy

Listing 1.4. Using blocks to reduce code duplication

Listing 1.5. A small example of DRY syntax

Listing 1.6. Map is one way Ruby uses functional programming for parallelism

Listing 1.7. Using attr_accessor to define accessor methods on your class

Listing 1.8. A reimplementation of attr_accessor

Listing 1.9. Using ActsAsTaggable to get a lot of features in one line of code

Listing 1.10. Building an RSS feed for our projects

Listing 1.11. A simplified look at XML::Builder’s use of method_missing

Listing 1.12. A validation domain-specific language example

Listing 2.1. A simple test using Test::Unit

Listing 2.2. Using setup and teardown to prepare tests

Listing 2.3. Tests for our to-be-implemented XML reporter

Listing 2.4. Our PayrollReporter implemented

Listing 2.5. A context for an empty queue string

Listing 2.6. A few specs for a string object

Listing 2.7. A basic spec for our IntranetReader class

Listing 2.8. Our implementation of IntranetReader

Listing 2.9. A process checker class and test

Listing 2.10. A revised test with fixture data

Listing 2.11. Our refactored and expanded tests

Listing 2.12. Building tests for our supplier class

Listing 2.13. A class for handling purchasing from a supplier

Listing 2.14. A stubbed SupplierInterface

Listing 2.15. Tests for shipping components

Listing 2.16. An updated test to use our custom mock

Listing 2.17. The previous mocking example, rewritten using Mocha

Listing 2.18. A simple class to Heckle

Listing 2.19. Our tests to Heckle

Listing 2.20. Our updated tests that can stand up to even a strong Heckling!

Listing 3.1. Turn our photos into smaller, Polaroid-like images, and upload to Amazon S3

Listing 3.2. Twitter a link to an image

Listing 3.3. Turn TODO and FIXME comments into Outlook tasks

Listing 3.4. Turn TODO and FIXME comments into iCal To Dos

Listing 3.5. AppleScript example for adding a To Do item to iCal

Listing 3.6. Ruby example for adding a To Do item to iCal using Appscript

Listing 3.7. Ruby example for adding a To Do item to iCal using RubyOSA

Listing 3.8. Growl notification using Cocoa API

Listing 3.9. Rakefile for creating RDoc documentation and running RSpec tests

Listing 3.10. Running rake rdoc twice with --trace

Listing 3.11. Listing all the tasks and their prerequisites

Listing 3.12. Tasks to merge JavaScript files and create HTML from Textile documents

Listing 3.13. Example Textile document that we’ll convert to HTML

Listing 4.1. The whole Ultraviolet helper

Listing 4.2. A URL validation library

Listing 4.3. Our model, pre-metaprogramming

Listing 4.4. Our metaprogrammed model

Listing 5.1. Get historical stock prices from Google Finance

Listing 5.2. Using HTTP POST and XmlSimple to send a document to the web server

Listing 5.3. A service for packaging log files and serving them as a zip file

Listing 5.4. Defining our task manager resources in config/routes.rb

Listing 5.5. Routes for our RESTful tasks list

Listing 5.6. Routes for our RESTful tasks list

Listing 5.7. Responding with different content types

Listing 5.8. An Atom feed for our tasks list

Listing 5.9. Using our task manager with ActiveResource

Listing 5.10. WSDL describing our task manager service

Listing 5.11. Our task manager servant

Listing 5.12. A simple task manager SOAP service

Listing 5.13. Task manager client using WSDLDriver

Listing 5.14. Task manager client using generated stubs

Listing 6.1. Constructing a basic MailFactory object, attribute by attribute

Listing 6.2. Sending email to administrators

Listing 6.3. Fetching email using POP3

Listing 6.4. Restarting MySQL via email

Listing 6.5. Creating tickets via email

Listing 6.6. Sending an IM with Net::TOC

Listing 6.7. Sending the results of uptime over AIM

Listing 6.8. Building a simple Jabber::Simple object

Listing 6.9. Managing MySQL via Jabber rather than email

Listing 7.1. A method that takes a Ruby error object and generates XML from it

Listing 7.2. Implementing our error reporter

Listing 7.3. Processing all messages from the queue

Listing 7.4. Our testing consumer for the error reporter

Listing 7.5. Demonstrating the Reliable Messaging library’s core functionality

Listing 7.6. Passing data from a Rails application to a Ruby application

Listing 7.7. Creating a work order in the database and passing a message

Listing 7.8. Consuming messages from the queue

Listing 7.9. AccountsController create action for creating a new account

Listing 7.10. A config/wmq.yml configuration file

Listing 7.11. The app/controllers/application.rb file modified to read WMQ configuration

Listing 7.12. AccountsController queues new accounts in ACCOUNTS.CREATED

Listing 7.13. Test case for putting message in ACCOUNTS.CREATED

Listing 7.14. Processing messages from WMQ to create new leads

Listing 8.1. Gem specification

Listing 8.2. Requiring Ruby files from a gem

Listing 8.3. Deployable binary

Listing 8.4. Gem specification including binary

Listing 8.5. Using a custom repository with the gem command

Listing 8.6. Using a custom location for gem server

Listing 8.7. Capfile for deploying a simple daemon

Listing 8.8. Tailing a remote log

Listing 8.9. Rakefile loading Vlad with Subversion and Mongrel recipes

Listing 8.10. config/deploy.rb for Vlad the Deployer

Listing 8.11. Watching daemon processes with God.rb

Listing 8.12. Telling God.rb how to notify our team members

Listing 8.13. Monitoring with notifications

Listing 9.1. Class declaration and setup method for testing the contact code

Listing 9.2. Testing the removal of a Contact object from a ContactList object

Listing 9.3. Initial implementation of the ContactList class

Listing 9.4. The Contact class we use to store contact records

Listing 9.5. Saving and loading a ContactList object

Listing 9.6. Second set of methods for the ContactList class

Listing 9.7. The contacts output file, in YAML format

Listing 9.8. The contact application tests

Listing 9.9. The Contact class for the gdbm implementation of the contacts library

Listing 9.10. The ContactList class for storing contact records

Listing 9.11. Adding and removing a contact

Listing 9.12. The remaining methods for the ContactList class

Listing 9.13. An example of a YAML file for contact records

Listing 9.14. SQL instructions for creating the contacts database

Listing 9.15. The loop through the contacts table

Listing 10.1. Our calculator’s XML configuration file

Listing 10.2. Getting our XML into Ruby with REXML

Listing 10.3. Reading and writing the configuration

Listing 10.4. A simple postprocessing filter

Listing 10.5. The associated CSS

Listing 10.6. A sample blog entry

Listing 10.7. Getting the HTML into a Post object

Listing 10.8. Redone Config#to_xml

Listing 10.9. Redone Keyboard#to_xml

Listing 10.10. A simple RSS parser

Listing 10.11. A small command-line script for running the RSS parser

Listing 10.12. Wrapping FeedTools

Listing 10.13. Creating a new feed

Listing 10.14. Generating RSS 2.0

Listing 10.15. Generating ATOM 1.0

Listing 10.16. Using YAML to solve the calculator config problem

Listing 10.17. Using the YAML solution in an equivalent manner to our XML solution

Listing 11.1. An ActiveRecord class implementing password hashing

Listing 11.2. Per-user salt for password hashing

Listing 11.3. Authenticating with ruby-net-ldap

Listing 11.4. Authenticating against Active Directory using ruby-net-ldap

Listing 11.5. An ActiveRecord migration to add our token column

Listing 11.6. A User model that generates tokens upon creation

Listing 11.7. A comments controller implementing our token authentication

Listing 11.8. Authenticating with HTTP Basic using CGI

Listing 11.9. A simple OpenID login controller

Listing 11.10. A database migration to create the OpenID tables

Listing 11.11. Edited view to support OpenID authentication

Listing 11.12. A new SessionsController that supports OpenID

Listing 12.1. Basic document search

Listing 12.2. multifield document searchseparate indexerMultifield document search

Listing 12.3. Separate indexer and query client programs

Listing 12.4. The MySolr class

Listing 12.5. Adding and indexing documents

Listing 12.6. Querying the index

Listing 12.7. Deleting items from the index

Listing 12.8. Using FTSearch to build an indexer

Listing 12.9. Querying the index

Listing 12.10. A simple search-enabled class using acts_as_ferret

Listing 12.11. A model that’s indexed with Ultrasphinx

Listing 12.12. Executing a full-text search with Ultrasphinx

Listing 12.13. Searching for “ruby” using Yahoo!’s API

Listing 12.14. Scraping Google for results for “ruby”

Listing 13.1. Currency converter

Listing 13.2. File output for currency converter

Listing 13.3. Rope Configuration (config/environment.rb)

Listing 13.4. Sales Report (lib/reports/daily_sales.rb)

Listing 13.5. Sales Report Controller (lib/controllers/sales.rb)

Listing 13.6. Sales Report, modified for AR (lib/reports/daily_sales.rb)

Listing 13.7. Transaction report, legacy data file

Listing 13.8. Transaction report, CSV database dump

Listing 13.9. Transaction report analysis script

Listing 13.10. Purchase Notes PDF renderer and formatter

Listing 13.11. Custom row rendering report