Screencast: Screen Scraping mit Nokogiri

Screen Scraping, ein Verfahren um ganze oder Teile von Webseiten auszulesen, wird gerne für Mashups eingesetzt. Ryan Bates zeigt in seinem dieswöchigen Screencast, wie dies mit Nokogiri umgesetzt werden kann.

Download (43 MB, 13:34)
Alternativer Download für iPod & Apple TV (21.9 MB, 13:34)

Ressourcen:

Quellcode:

[bash]
sudo gem install nokogiri — –with-xml2-include=/usr/local/include/libxml2 –with-xml2-lib=/usr/local/lib
[/bash]

[ruby]
# nokogiri_test.rb
require ‚rubygems‘
require ’nokogiri‘
require ‚open-uri‘

url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
doc.css(".item").each do |item|
title = item.at_css(".prodLink").text
price = item.at_css(".PriceCompare .BodyS, .PriceXLBold").text[/$[0-9.]+/]
puts "#{title} – #{price}"
puts item.at_css(".prodLink")[:href]
end

# lib/tasks/product_prices.rake
desc "Fetch product prices"
task :fetch_prices => :environment do
require ’nokogiri‘
require ‚open-uri‘

Product.find_all_by_price(nil).each do |product|
url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=#{CGI.escape(product.name)}&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
price = doc.at_css(".PriceCompare .BodyS, .PriceXLBold").text[/[0-9.]+/]
product.update_attribute(:price, price)
end
end
[/ruby]

Rails 2.3.5 erschienen

Heute wurde das ein weiteres zweier Release veröffentlicht. Es enthält neben ein paar Bugfixes auch einen Security-Fix.
Daher wird empfohlen dieses Update durchzuführen. Es kann über die bekannten gems-Mechanismen „gem update rails“ durchgeführt werden.

Neben den Fixes sind auch ein paar Feaures in dieses Release gefloßen:

  • Kompatibilität mit Ruby 1.9 wurde verbessert
  • In Rails 3 wird die View-Helper-Methode h standardmäßig den ausgegebenen Text escapen.
  • Fixes für das Nokogiri-Backend für XmlMini

Weitere Details findet Ihr unter folgendem Link: http://weblog.rubyonrails.org/2009/11/30/ruby-on-rails-2-3-5-released

Ruby on Rails for Microsoft Developers

This definitive guide examines how to take advantage of the new Agile methodologies offered when using Ruby on Rails (RoR). You’ll quickly grasp the RoR methodology by focusing on the RoR development from the point of view of the beginner- to intermediate-level Microsoft developer.

Plus, you’ll get a reliable roadmap for migrating your applications, skill set, and development processes to the newer, more agile programming platform that RoR offers.

Programming Ruby 1.9: The Pragmatic Programmers‘ Guide

Ruby is a fully object-oriented language, much like the classic object-oriented language, Smalltalk. Like Smalltalk, it is dynamically typed (as opposed to Java or C++), but unlike Smalltalk, Ruby features the same conveniences found in modern scripting languages, making Ruby a favorite tool of intelligent, forward-thinking programmers and the basis for the Rails web framework.

This is the reference manual for Ruby, including a description of all the standard library modules, a complete reference to all built-in classes and modules (including all the new and changed methods introduced by Ruby 1.9). It also includes all the new and changed syntax and semantics introduced since Ruby 1.8. Learn about the new parameter passing rules, local variable scoping in blocks, fibers, and the new block declaration syntax, among other exciting new features.

Programming Cocoa with Ruby

This is a book for the Ruby programmer who’s never written a Mac app before. Through this hands-on tutorial, you’ll learn all about the Cocoa framework for programming on Mac OS X. Join the author’s journey as this experienced Ruby programmer delves into the Cocoa framework right from the beginning, answering the same questions and solving the same problems that you’ll face. Together you’ll build a single application that threads throughout the book, and it’s not a toy.

You’ll cover topics that may not be the flashiest parts of Cocoa, but they’re ones you’ll need to know to create robust, feature-rich applications for yourself. And you’ll learn more than just Cocoa and RubyCocoa, you’ll get first-hand effective agile development practices. You’ll see test-first development of user-interface code, little domain-specific languages that take advantage of Ruby features, and other Rubyish tricks. At the end of the book, you’ll be ready to write a real Mac OS X application that can be distributed to real users.

Beginning Ruby: From Novice to Professional

Based on the best-selling first edition, Beginning Ruby: From Novice to Professional, Second Edition is the leading guide for every type of reader who wants to learn Ruby from the ground up. The new edition of this book provides the same excellent introduction to Ruby as the first edition plus updates for the newest version of Ruby, including the addition of the Sinatra and Ramaze web application frameworks and a chapter on GUI development so developers can take advantage of these new trends.

Beginning Ruby starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a full Ruby application. By the end of the book, in addition to in-depth knowledge of Ruby, you’ll also have basic understanding of many ancillary technologies such as SQL, XML, web frameworks, and networking. * Introduces readers to the Ruby programming language * Takes readers from basic programming skills to web development with topics like Ruby-based frameworks and GUI programming * Covers many ancillary technologies in order to provide a broader picture (e.g., databases, XML, network daemons)

Screencast: Embedded Association

Diese Woche zeigt Ryan Bates in seinem Screencast wie selbstreferenzierende DB-Beziehungen (one-to-many, many-to-many) umgesetzt werden können.

Download (18.7 MB, 14:03)
Alternativer Download für iPod & Apple TV (15.4 MB, 14:03)

Ressourcen:

Quellcode:

One to many:

[bash]
script/generate migration add_role_to_users role:string
rake db:migrate
[/bash]

[ruby]
# models/user.rb
class User < ActiveRecord::Base
acts_as_authentic
has_many :articles
has_many :comments

ROLES = %w[admin moderator author]

def role_symbols
[role.to_sym]
end
end
[/ruby]

[ror]
<!– users/new.html.erb –>
<p>
<%= f.label :role %><br />
<%= f.collection_select :role, User::ROLES, :to_s, :titleize %>
</p>
[/ror]

Many to many

[bash]
script/generate migration add_roles_mask_to_users roles_mask:integer
rake db:migrate
[/bash]

[ruby]
# models/user.rb
class User < ActiveRecord::Base
acts_as_authentic
has_many :articles
has_many :comments

named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0"} }

ROLES = %w[admin moderator author]

def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end

def roles
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end

def role_symbols
roles.map(&:to_sym)
end
end
[/ruby]

[ror]
<!– users/new.html.erb –>
<p>
<%= f.label :roles %><br />
<% for role in User::ROLES %>
<%= check_box_tag "user[roles][]", role, @user.roles.include?(role) %>
<%=h role.humanize %><br />
<% end %>
<%= hidden_field_tag "user[roles][]", "" %>
</p>
[/ror]

Phusion Passenger 2.2.6 veröffentlicht

Phusion Passenger ist in Version 2.2.6 erschienen. Dies soll eine der letzten Releases für die Version 2 sein, denn die dritte Versions-Familie steht wohl schon als Nächstes bereit.

Auch in diesem Release wurden bekannte Fehler behoben und stehen somit als Update zur Verfügung.

Das Update kann über die bekannten Wege über gem durchgeführt werden.

[bash]
gem install passenger

# wenn der Zielserver Apache ist:
passenger-install-apache2-module

# wenn der Zielserver nginx ist:
passenger-install-nginx-module

[/bash]

Weitere Details und wie das Update über Linux-Packages durchgeführt werden kann, sind auf der Projektseite zu finden: http://blog.phusion.nl/2009/11/18/phusion-passenger-2-2-6-released/

Merb Way

„Foy is a gifted writer and his descriptions of the philosophies that( impacted the design and implementation of Merb are definitely useful to( everyone affected by the changes in Rails 3.“ –Obie Fernandez, Series Editor The Expert Guide to Building Merb Applications Thousands of Ruby and Rails developers are discovering the extraordinary scalability, agility, flexibility, and performance offered by the new Merb MVC framework. The Merb Way is the first comprehensive guide to using, extending, and deploying Merb. Like the bestseller The Rails Way (Addison-Wesley, 2008), this book can either be read cover-to-cover as a tutorial or used for modular coverage that makes it an ideal task reference.

Foy Savas systematically covers everything developers need to know to build production-quality Merb applications, offering detailed code examples for jumpstarting virtually any project. Savas is not only involved in the Merb project as an open source contributor: He uses Merb every day as a professional developer. Drawing on his extensive practical expertise, he delves deeply into the Merb framework’s architecture and source code, revealing its elegance and offering powerful best practices for using it. To maximize this book’s practical value, he also covers the tools most widely used alongside Merb, including the DataMapper ORM, the RSpec tester (and associated behavior-driven development techniques), and several leading Merb plugins. The Merb Way will help you *Master Merb innovations that will be incorporated into Rails 3*Understand the fundamentals of Merb development, one step at a time *Use Merb’s sophisticated application router to guide incoming requests*Explore the Merb stack, master its configuration options, and dive into its internals*Use Merb controllers to integrate application code, handle responses, and manage sessions, filters, and exceptions*Build templates with both ERB (Erubis) and HAML*Use the DataMapper ORM to represent object properties and behaviors *Learn best practices for RESTful development with Merb *Leverage Helpers to simplify many common development tasks *Refactor code, encapsulate it in gems, and share it across multiple applications*Customize the responses your application sends to each user*Gain a deep, practical understanding of Merb plugins and extensibility*Authenticate users with Merb’s modular authentication plugin *Configure, generate, describe, and test Merb mailers *Use the merb-parts gem to create component-like regions on Web pages *Cache content to relieve stress on Web servers *Make the most of Behavior Driven Development and testing with RSpec

Beginning Rails 3

Beginning Rails 3 is the practical starting point for anyone wanting to learn how to build dynamic web applications using the latest release of the Rails framework for Ruby. You’ll learn how all of the components of Rails fit together and how you can leverage them to create sophisticated web applications with less code and more joy. This book is particularly well suited to those with little or no experience with web application development, or who have some experience but are new to Rails.

Beginning Rails 3 assumes basic familiarity with web terms and technologies, but doesn’t require you to be an expert. Rather than delving into the arcane details of Rails, the focus is on the aspects of the framework that will become your pick, shovel, and axe. Part history lesson, part introduction to object-oriented programming, and part dissertation on open source software, this title doesn’t just explain how to do something in Rails, it explains why. Learn to create Rails web applications from scratch Includes a gentle introduction to the Ruby programming language Completely updated to include the new features of Rails 3 Beginning Rails.