Maik Schmidt — «Enterprise Recipes with Ruby and Rails
Total Page:16
File Type:pdf, Size:1020Kb
What readers are saying about Enterprise Recipes with Ruby and Rails Enterprise Recipes with Ruby and Rails covers most of the tasks you need to accomplish in the enterprise, including integration with other systems, databases, and security measures. I wish I’d had this book three years ago. Ola Bini JRuby Core Developer, ThoughtWorks Studios This book is full of practical, relevant advice instead of theoretical background or “Hello, World” samples. Once you move beyond the basic skills of using Ruby and Rails, this is exactly what you need— real-world recipes that you can put to use immediately. It’s like getting condensed experience on paper, giving you a two-year head start on those who have to acquire this knowledge by making their own mis- takes. Stefan Tilkov CEO and Principal Consultant, innoQ If you’re responsible for developing enterprise software, after reading this book you’ll want to review all your projects to see where you can save time and money with Ruby and Rails. Maik Schmidt shows us once again that enterprise software doesn’t have to be “enterprisey.” Steve Vinoski IEEE Internet Computing Columnist and Member of Technical Staff, Verivue, Inc. On exactly the right level, this book explains many interesting libraries and tools invaluable for enterprise developers. Even experi- enced Ruby and Rails developers will find new information. Thomas Baustert Rails Book Author, b-simple.de Enterprise Recipes with Ruby and Rails Maik Schmidt The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Many of the designations used by manufacturers and sellers to distinguish their prod- ucts are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright © 2008 Maik Schmidt. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-23-9 ISBN-13: 978-1-934356-23-4 Printed on acid-free paper. P1.0 printing, November 2008 Version: 2009-4-20 For my girls: Mia, Henriette, and Caro. Contents Foreword 9 Acknowledgments 12 Preface 13 Part I—Security & E-commerce Recipes 20 1 Implement Enterprise-wide Security 21 1. Protect Information with Symmetric Ciphers . 23 2. Protect Secrets with Asymmetric Ciphers . 28 3. Verify Data Integrity with Signatures ........... 31 4. Generate Real Random Numbers ............. 35 5. Create Strong and Convenient Passwords . 38 6. Store Passwords Securely ................. 44 7. Reanimate Good Old Basic Authentication . 48 8. Implement a Single Sign-on System with OpenID . 51 9. Authenticate with LDAP .................. 58 2 Process E-commerce Payments 62 10. Charge Credit Cards with ActiveMerchant . 64 11. Integrate ActiveMerchant with Rails ........... 70 12. Transfer Money with PayPal ................ 78 Part II—Databases & XML Recipes 88 3 Get the Most Out of Databases 89 13. Execute Stored Procedures ................ 91 14. Feed Rails Databases from the Outside ......... 98 15. Access Databases from Different Vendors Simultane- ously ............................. 105 16. Manage Data with Subversion ............... 110 CONTENTS 7 4 Tame File and Data Formats 116 17. Manipulate CSV with Ruby ................ 118 18. Read and Write Fixed-Length Records .......... 123 19. Harness JSON in Ruby ................... 130 20. Master Binary Data ..................... 134 5 Process XML Documents the Ruby Way 140 21. XML Data Binding on Steroids .............. 142 22. Use XML Files as Models .................. 146 23. Handle Large XML Documents .............. 154 24. High-Performance Parsing ................. 159 25. Work with HTML and Microformats ............ 165 26. Build Plain-Vanilla XML Documents ........... 172 27. Build Arbitrary XML Documents ............. 174 Part III—Networking & Messaging Recipes 178 6 Perform Basic Networking Tasks with Ease 179 28. Harness the Power of Sockets ............... 180 29. Find Solutions Quickly with open-uri .......... 186 30. Get the Most Out of HTTP ................. 190 31. Scrape Screens with WWW::Mechanize ......... 196 7 Use and Build Web Services 202 32. Publish Resources with REST ............... 203 33. Use REST Services ..................... 211 34. Build Your Own SOAP Services .............. 217 35. Use SOAP Services with WSDL .............. 221 8 Talk to Message Brokers 224 36. Transfer Messages with Files ............... 226 37. Create a Messaging Infrastructure ............ 233 38. Integrate with JMS ..................... 242 39. Connect to Message Queues with ActiveMessaging . 248 Report erratum this copy is (P1.0 printing, November 2008) CONTENTS 8 Part IV—Integration & Administration Recipes 256 9 Speak Foreign Languages 257 40. Embed C and C++ ...................... 258 41. Mix Java and Ruby Code .................. 265 42. Use RMI Services ...................... 271 43. Mix Ruby and .NET with IronRuby ............ 275 10 Maintain and Administer Your Applications 284 44. Turn Your Code into Daemons and Services . 286 45. Monitor Your Applications with Monit .......... 295 46. Let god Take Care of Your System ............ 301 47. Create Plug-ins for Common Tasks ............ 306 48. Avoid Code Duplication with Generators ......... 311 11 Test the Easy Way 315 49. Improve Your Testing with RSpec ............. 316 50. Integrate RSpec with Rails ................. 323 51. Create Mock Objects with RSpec ............. 326 52. Prototype Services with Sinatra .............. 334 12 Get Documentation Nearly for Free 343 53. Generate Documentation Automatically ......... 345 54. Annotate Your Models Automatically ........... 352 55. Create Great Reports .................... 356 Bibliography 365 Index 366 Report erratum this copy is (P1.0 printing, November 2008) Foreword I’m glad someone finally wrote this book. Let me explain. I’ve been bullish on Ruby in the enterprise for a long time now, both with and without Rails. And, the company for which I work, ThoughtWorks, has also been a strong advocate for enterprise Rails. It happened for me shortly after I fully understood what sets Rails apart from other web frameworks. At the time, the last thing I wanted to see was another web framework, recently having completed a book comparing the dizzying array of web frameworks in the Java space (the now very outdated Art of Java Web Development [For03]). Once you’ve spent that much time looking at frameworks, a new one is not high on your list of priorities. But when Rails came along, I could tell that it was completely different and that it had lots of compelling, obvious- in-hindsight ideas embedded inside it. I remember thinking “Wow, this is going to be a really cool thing when all the libraries catch up.” For something to be “enterprise ready,” you have to have tons of libraries to support all the interactions with the outside world and repositories of reusable code, so I estimated at the time that it would take five or six years for Ruby to even sit at the table in the enterprise world. But I was wrong in two ways. First, I greatly underestimated the pas- sion and fire in the Ruby community to roll up their sleeves and create all the libraries needed to let Rails play in any space it wants. The sec- ond way I was wrong reflects the first: it’s just plain easier to write stuff in Ruby. I was carrying all the prejudices from my experience with other languages, where it takes a lot of work to write reusable libraries of code. And the reason for that comes from what I call the “Lockdown Experiment.” Back in the mid-90s, an experiment started as a way to make average developers more effective, because the demand continued (as it does today) to outstrip the supply of good developers. If the software industry can figure out a way to make mediocre developers productive, software FOREWORD 10 development can expand to enterprise scales. Thus, we saw the rise of languages like Visual Basic and Java and later C#. These languages were specifically made less powerful than alternatives (like Smalltalk). The goal of the Lockdown Experiment: make tools to keep average devel- opers out of trouble while still being able to write code. But then a cou- ple of interesting things happened. First, creating restrictive tools and languages didn’t really keep average developers out of trouble, because average developers sometimes apply great ingenuity to coming up with ridiculously complex solutions to problems. But while this didn’t really make the average developers better, it put a serious governor on the best developers. The whole industry seemed to be optimizing for the wrong thing: safety at the expense of power, with the stated goal of creating