Created
December 18, 2010 17:47
-
-
Save pelf/746697 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
class Amazon | |
class Book | |
attr_accessor :title, :author, :amazon_id, :img | |
def initialize(url) | |
url =~ /\/(\d+)\// | |
@amazon_id = $1 | |
doc = Nokogiri::HTML(open(url)) | |
doc.css('title').first.content =~ /(.+):[^:]+:(.+):[^:]+$/ | |
@title = $1.strip | |
@author = $2.strip | |
@img = doc.css('table.productImageGrid img#prodImage').first['src'] | |
end | |
def embed_code(show_titles=false) | |
if !show_titles | |
return "<a href=\"http://www.amazon.co.uk/gp/product/#{@amazon_id}/\"><img title=\"#{@author}: #{@title}\" src=\"#{@img}\"/></a>" | |
else | |
return "<a href=\"http://www.amazon.co.uk/gp/product/#{@amazon_id}/\"><img src=\"#{@img}\"/></a><br/><strong>#{@author}</strong>: <em>#{@title}</em>" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment