8113Ch00CMP2 3/28/07 5:27 PM Page i

Practical Ruby Gems

David Berube 8113Ch00CMP2 3/28/07 5:27 PM Page ii

Practical Ruby Gems Copyright © 2007 by David Berube All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-811-5 ISBN-10 (pbk): 1-59059-811-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Jason Gilmore Technical Reviewer: Yan Pritzker Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Jeff Pepper, Paul Sarknas, Dominic Shakeshaft, Jim Sumser, Matt Wade Project Manager: Richard Dal Porto Copy Edit Manager: Nicole Flores Copy Editor: Candace English Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Diana Van Winkle, Van Winkle Design Proofreader: Liz Welch Indexer: Julie Grady Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/ Download section. 8113Ch00CMP2 3/28/07 5:27 PM Page iii

Dedicated to my parents 8113Ch00CMP2 3/28/07 5:27 PM Page iv

Contents at a Glance

About the Author ...... xiii Acknowledgments ...... xv PART 1 ■ ■ ■ Using RubyGems ■CHAPTER 1 What Is RubyGems? ...... 3 ■CHAPTER 2 Installing RubyGems ...... 7 ■CHAPTER 3 Using RubyGems in Your Code ...... 13 ■CHAPTER 4 Managing Installed Gem Versions ...... 25 PART 2 ■ ■ ■ Using Particular Gems ■CHAPTER 5 Data Access with the ActiveRecord Gem ...... 35 ■CHAPTER 6 Easy Text Markup with the BlueCloth Gem ...... 45 ■CHAPTER 7 Creating Web Applications with ...... 53 ■CHAPTER 8 Creating Command-Line Utilities with cmdparse ...... 69 ■CHAPTER 9 HTML Templating with erubis ...... 81 ■CHAPTER 10 Parsing Feeds with feedtools ...... 89 ■CHAPTER 11 Creating Graphical User Interfaces with fxruby ...... 95 ■CHAPTER 12 Retrieving Stock Quotes with YahooFinance ...... 103 ■CHAPTER 13 Parsing HTML with hpricot ...... 109 ■CHAPTER 14 Writing HTML as Ruby with Markaby ...... 115 ■CHAPTER 15 Parsing CSV with fastercsv ...... 121 ■CHAPTER 16 Multiple Dispatch with multi ...... 127 ■CHAPTER 17 Serving Web Applications with ...... 137 ■CHAPTER 18 Transferring Files Securely with net-sftp ...... 145 ■CHAPTER 19 Executing Commands on Remote Servers with net-ssh ...... 149 ■CHAPTER 20 Validating Credit Cards with creditcard ...... 155 ■CHAPTER 21 Writing PDFs with pdf-writer ...... 159 ■CHAPTER 22 Handling Recurring Events with runt ...... 167

iv 8113Ch00CMP2 3/28/07 5:27 PM Page v

■CHAPTER 23 Building Websites with Rails ...... 175 ■CHAPTER 24 Automating Development Tasks with ...... 183 ■CHAPTER 25 Manipulating Images with RMagick ...... 191 ■CHAPTER 26 Speeding Up Web Applications with memcache-client ...... 199 ■CHAPTER 27 Managing Zip Archives with rubyzip ...... 209 ■CHAPTER 28 Speeding Up Function Calls with memoize ...... 215 ■CHAPTER 29 Tagging MP3 Files with id3lib-ruby ...... 221 ■CHAPTER 30 Shortening URLs with shorturl ...... 227 ■CHAPTER 31 Creating Standalone Ruby Applications with rubyscript2exe . . . 231 ■CHAPTER 32 Cleaning Dirty HTML with tidy ...... 237 ■CHAPTER 33 Parsing XML with xml-simple ...... 245 PART 3 ■ ■ ■ Creating Gems ■CHAPTER 34 Creating Our Own Gems ...... 255 ■CHAPTER 35 Distributing Gems ...... 261

■INDEX ...... 267

v 8113Ch00CMP2 3/28/07 5:27 PM Page vi 8113Ch00CMP2 3/28/07 5:27 PM Page vii

Contents

About the Author ...... xiii Acknowledgments ...... xv

PART 1 ■ ■ ■ Using RubyGems

■CHAPTER 1 What Is RubyGems? ...... 3

Why Use RubyGems? ...... 3 How Does RubyGems Compare to Other Packaging Systems? ...... 6

■CHAPTER 2 Installing RubyGems ...... 7

Installing Ruby ...... 7 Installing RubyGems Under Linux and Mac OS X ...... 10 Updating Your RubyGems System After You’ve Installed It ...... 11

■CHAPTER 3 Using RubyGems in Your Code ...... 13

Getting Started with a Ruby Gem ...... 13 Working with Source Gems ...... 20 Debugging RubyGems ...... 23

■CHAPTER 4 Managing Installed Gem Versions ...... 25

What Is Gem Versioning? ...... 25 Installing an Older Gem Version ...... 26 Updating Gems ...... 27 Uninstalling Gems ...... 28 Specifying Gem Versions ...... 29

vii 8113Ch00CMP2 3/28/07 5:27 PM Page viii

viii ■CONTENTS

PART 2 ■ ■ ■ Using Particular Gems

■CHAPTER 5 Data Access with the ActiveRecord Gem ...... 35

How Does It Work? ...... 36 Archiving RSS News with ActiveRecord ...... 39 Conclusion ...... 44

■CHAPTER 6 Easy Text Markup with the BlueCloth Gem ...... 45

How Does It Work? ...... 45 BlueCloth-to-HTML Converter ...... 46 bluecloth2pdf BlueCloth-to-PDF Converter ...... 48 Conclusion ...... 51

■CHAPTER 7 Creating Web Applications with Camping ...... 53

How Does It Work? ...... 53 Tracking Time with Camping ...... 56 Conclusion ...... 68

■CHAPTER 8 Creating Command-Line Utilities with cmdparse ...... 69

How Does It Work? ...... 69 A Job-Search Tool Built with cmdparse ...... 71 Conclusion ...... 79

■CHAPTER 9 HTML Templating with erubis ...... 81

How Does It Work? ...... 81 HTML MySQL Table Viewer with erubis ...... 83 Conclusion ...... 88

■CHAPTER 10 Parsing Feeds with feedtools ...... 89

How Does It Work? ...... 89 A News Search Tool Built with feedtools ...... 91 Conclusion ...... 93 8113Ch00CMP2 3/28/07 5:27 PM Page ix

■CONTENTS ix

■CHAPTER 11 Creating Graphical User Interfaces with fxruby ...... 95

How Does It Work? ...... 95 Dynamic MySQL Data Form with fxruby ...... 96 Conclusion ...... 102

■CHAPTER 12 Retrieving Stock Quotes with YahooFinance ...... 103

How Does It Work? ...... 103 Displaying a Stock-Market Ticker with YahooFinance ...... 104 Conclusion ...... 107

■CHAPTER 13 Parsing HTML with hpricot ...... 109

How Does It Work? ...... 109 Screen-Scraping a Catalog with hpricot ...... 111 Conclusion ...... 114

■CHAPTER 14 Writing HTML as Ruby with Markaby ...... 115

How Does It Work? ...... 115 Graphical HTML Stock Charts with Markaby ...... 116 Conclusion ...... 120

■CHAPTER 15 Parsing CSV with fastercsv ...... 121

How Does It Work? ...... 121 Processing Census Data with fastercsv ...... 123 Conclusion ...... 125

■CHAPTER 16 Multiple Dispatch with multi ...... 127

How Does It Work? ...... 127 Formatting SQL for Legibility Using multi ...... 129 Conclusion ...... 135 8113Ch00CMP2 3/28/07 5:27 PM Page x

x ■CONTENTS

■CHAPTER 17 Serving Web Applications with mongrel ...... 137

How Does It Work? ...... 137 Using mongrel as a Rails Development Server ...... 138 mongrel Running Rails as a Service on Win32 ...... 139 mongrel Running Camping ...... 140 mongrel as a Small Web Server ...... 140 mongrel Serving a Rails App via Apache 2.2 ...... 141 Conclusion ...... 143

■CHAPTER 18 Transferring Files Securely with net-sftp ...... 145

How Does It Work? ...... 145 Sending Files via SFTP Using net-sftp ...... 146 Conclusion ...... 148

■CHAPTER 19 Executing Commands on Remote Servers with net-ssh . . . 149

How Does It Work? ...... 149 Editing Remote Files with net-ssh and Vim ...... 151 Conclusion ...... 154

■CHAPTER 20 Validating Credit Cards with creditcard ...... 155

How Does It Work? ...... 155 Verifying Credit-Card Numbers in Batch with creditcard ...... 156 Conclusion ...... 158

■CHAPTER 21 Writing PDFs with pdf-writer ...... 159

How Does It Work? ...... 159 Creating Reports with pdf-writer and Net/SFTP ...... 160 Conclusion ...... 165

■CHAPTER 22 Handling Recurring Events with runt ...... 167

How Does It Work? ...... 167 Planning User-Group Meetings with runt ...... 169 Executing Commands on a Recurring Schedule ...... 172 Conclusion ...... 173 8113Ch00CMP2 3/28/07 5:27 PM Page xi

■CONTENTS xi

■CHAPTER 23 Building Websites with Rails ...... 175

How Does It Work? ...... 175 A Simple Database Application with Rails ...... 176 Conclusion ...... 182

■CHAPTER 24 Automating Development Tasks with rake ...... 183

How Does It Work? ...... 183 Easy Documentation with BlueCloth and rake ...... 184 Conclusion ...... 189

■CHAPTER 25 Manipulating Images with RMagick ...... 191

How Does It Work? ...... 191 Creating Thumbnails with RMagick ...... 192 Conclusion ...... 198

■CHAPTER 26 Speeding Up Web Applications with memcache-client . . 199

How Does It Work? ...... 199 Speeding Up the Session Cache with memcached . . . . . 200 Accessing memcached Servers with a Graphical Client ...... 205 Conclusion ...... 207

■CHAPTER 27 Managing Zip Archives with rubyzip ...... 209

How Does It Work? ...... 209 Reading Text from a Zip File ...... 210 Conclusion ...... 213

■CHAPTER 28 Speeding Up Function Calls with memoize ...... 215

How Does It Work? ...... 215 Organizing a List of MP3s ...... 217 Conclusion ...... 220

■CHAPTER 29 Tagging MP3 Files with id3lib-ruby ...... 221

How Does It Work? ...... 221 Changing MP3 Tags with ID3 Mass Tagger ...... 222 Conclusion ...... 225 8113Ch00CMP2 3/28/07 5:27 PM Page xii

xii ■CONTENTS

■CHAPTER 30 Shortening URLs with shorturl ...... 227

How Does It Work? ...... 227 Shortening RSS Feeds with shorturl ...... 228 Conclusion ...... 230

■CHAPTER 31 Creating Standalone Ruby Applications with rubyscript2exe ...... 231

How Does It Work? ...... 231 Packaging the id3tool Script with rubyscript2exe ...... 232 Conclusion ...... 236

■CHAPTER 32 Cleaning Dirty HTML with tidy ...... 237

How Does It Work? ...... 237 Tidying Up HTML on the Web with tidy ...... 240 Conclusion ...... 243

■CHAPTER 33 Parsing XML with xml-simple ...... 245

How Does It Work? ...... 245 Tracking OpenSSL Vulnerabilities with xml-simple ...... 248 Conclusion ...... 251

PART 3 ■ ■ ■ Creating Gems

■CHAPTER 34 Creating Our Own Gems ...... 255

What Is Inside a Gem? ...... 255 What’s a Gemspec? ...... 255 Building a Gem Package from a Gemspec ...... 256 Conclusion ...... 260

■CHAPTER 35 Distributing Gems ...... 261

Distribution Methods ...... 261 Conclusion ...... 266

■INDEX ...... 267 8113Ch00CMP2 3/28/07 5:27 PM Page xiii

About the Author

■DAVID BERUBE is a Ruby developer, trainer, author, and speaker. He’s used both Ruby and Ruby on Rails since 2003, when he became a Ruby advocate after he wrote about the language for Dr Dobb’s Journal. Prior to that he worked professionally with PHP, , C++, and Visual Basic. David’s professional accomplishments include creating the Ruby on Rails engine for Cool- Ruby.com (http://coolruby.com), a site that tracks the latest Ruby developments, and working with thoughtbot (www.thoughtbot.com) on the Rails engine that powers Sermo’s America’s Top Doc contest. He also worked with the Casting Frontier on the Ruby on Rails backend that is powering their digital casting services for Los Angeles. He has worked on several other Ruby projects, including the engine powering CyberKnowHow’s BirdFluBreakingNews search engine. David’s writing has been in print in over 65 countries, in magazines such as Linux Maga- zine, Dr Dobb’s Journal, and International PHP Magazine. He’s also taught college courses and spoken publicly on topics such as “MySQL and You” and “Making Money with Open Source Software.” Feel free to contact the author via his website at http://berubeconsulting.com or via his email address at [email protected].

xiii 8113Ch00CMP2 3/28/07 5:27 PM Page xiv 8113Ch00CMP2 3/28/07 5:27 PM Page xv

Acknowledgments

I’ like to thank my parents and my sisters; I can’t imagine writing this book without them. I’d also like to thank the many friends that have supported me; in particular, I’d like to thank Wayne Hammar and Matthew Gifford. I’d also like to thank the vast array of professional associates I’ve worked with and learned from, and in particular I’d like to thank Terry Simkin, Ted Roche, Bill Sconce, Bruce Dawson, K.C. Singh, and Joey Rubenstein. Thanks to Peter Cooper for introducing me to the possibility of writing this book. Finally, I’d like to thank my editors, originally Keir Thomas and later Jason Gilmore, as well as my technical reviewer Yan Pritzker, my project manager Richard Dal Porto, and my copy editor Candace English.

xv 8113Ch00CMP2 3/28/07 5:27 PM Page xvi