Skip to content

Instantly share code, notes, and snippets.

@pelf
Created December 18, 2010 17:47
Show Gist options
  • Save pelf/746697 to your computer and use it in GitHub Desktop.
Save pelf/746697 to your computer and use it in GitHub Desktop.
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