Permalink
Browse files

Fetching now working via DelayedJob

  • Loading branch information...
1 parent 105268f commit d3f57af6add4a4c60007531e350af2772a1a8033 @swaroopch committed Dec 29, 2010
Showing with 71 additions and 24 deletions.
  1. +5 −1 app/controllers/book_controller.rb
  2. +29 −2 app/models/bookprice.rb
  3. +32 −21 app/views/book/view.html.erb
  4. +5 −0 script/delayed_job
@@ -21,7 +21,11 @@ def view
@bookseer = BookseerInfo::link(@bookinfo)
end
- @stores = Rails.cache.fetch("prices:#{@isbn}", :expires_in => 1.day) { Bookprice::prices(@isbn) }
+ @prices = Bookprice.new(:isbn => @isbn)
+ @stores = Rails.cache.read(@prices.cache_key)
+ if @stores.nil?
+ Delayed::Job.enqueue(@prices)
+ end
@not_available = Bookprice::NOT_AVAILABLE
respond_with(@stores) do |format|
View
@@ -6,6 +6,27 @@ class Bookprice
NOT_AVAILABLE = 999_999
+ attr_accessor :isbn
+
+ def initialize(given_isbn)
+ self.isbn = self.class.check_isbn(given_isbn)
+ end
+
+ # For usage with DelayedJob : Bookprice.new(:isbn => "9789380032825").perform
+ def perform
+ prices = self.class.prices(self.isbn)
+ Rails.cache.write(self.cache_key, prices)
+ prices
+ end
+
+ def cache_key
+ "prices:#{self.isbn}"
+ end
+
+ def number_of_stores
+ self.class.searches.size
+ end
+
class << self
def find_price_at_end(text)
@@ -33,16 +54,22 @@ def is_isbn(text)
def searches
# e.g. ["search_a1books", "search_infibeam", "search_rediff"]
functions = self.methods.select { |name| name =~ /^search_(\w+)$/ }.sort
- # e.g. [["a1books", search_a1books], ["infibeam", search_infibeam], ["rediff", search_rediff]]
+ # e.g. [[:a1books, search_a1books], [:infibeam, search_infibeam], [:rediff, search_rediff]]
functions.collect { |fname| [ fname.split("_")[1].to_sym, self.method(fname) ] }
end
def names
self.searches.map { |name, search| name.to_s }.sort.map(&:to_sym)
end
+ def check_isbn(isbn)
+ isbn = isbn[:isbn] if isbn.is_a?(Hash)
+ raise ArgumentError, "Invalid ISBN: #{isbn}" unless !isbn.nil? && isbn.is_a?(String) && is_isbn(isbn)
+ isbn
+ end
+
def prices(isbn)
- raise "Invalid ISBN : #{isbn}" unless is_isbn(isbn)
+ isbn = check_isbn(isbn)
self.searches.map { |name, search| [name, search.call(isbn)] }.sort_by { |p| p[1][:price] }
end
@@ -29,26 +29,37 @@
<% end %>
<% end %>
-<h2>Where to Buy</h2>
-<div class="stores">
- <p>(Sorted by price)</p>
- <table>
- <% @stores.each do |store, data| %>
- <tr>
- <td>
- <a href="<%= data[:url] %>"><%= store %></a>
- </td>
- <td>
- <% unless data[:price] == @not_available %>
- <span style="font-family:rupee">R</span> <%= data[:price] %>
- <% else %>
- not available
- <% end %>
- </td>
- </tr>
- <% end %>
- </table>
-</div>
+<% unless @stores.nil? %>
+ <h2>Where to Buy</h2>
+ <div class="stores">
+ <p>(Sorted by price)</p>
+ <table>
+ <% @stores.each do |store, data| %>
+ <tr>
+ <td>
+ <a rel="nofollow" href="<%= data[:url] %>"><%= store %></a>
+ </td>
+ <td>
+ <% unless data[:price] == @not_available %>
+ <span style="font-family:rupee">R</span> <%= data[:price] %>
+ <% else %>
+ not available
+ <% end %>
+ </td>
+ </tr>
+ <% end %>
+ </table>
+ </div>
+<% else %>
+ <p>
+ <em>Fetching prices from <%= @prices.number_of_stores %> online stores... please be patient!</em> (This page will be automatically refreshed, please do NOT manually refresh)
+ </p>
+ <% content_for :js do %>
+ $( function() {
+ setTimeout("location.reload(true)", 60 * 1000);
+ });
+ <% end %>
+<% end %>
<h2>Reviews</h2>
<div class="goodreads">
@@ -57,5 +68,5 @@
<h2>What to read next?</h2>
<% unless @bookseer.nil? %>
- <a href="<%= @bookseer %>">Consult the Bookseer</a>
+ <a rel="nofollow" href="<%= @bookseer %>">Consult the Bookseer</a>
<% end %>
View
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
+require 'delayed/command'
+Delayed::Command.new(ARGV).daemonize

0 comments on commit d3f57af

Please sign in to comment.