Skip to content
This repository has been archived by the owner on Dec 26, 2020. It is now read-only.

Commit

Permalink
Fetching now working via DelayedJob
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopch committed Dec 29, 2010
1 parent 105268f commit d3f57af
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
6 changes: 5 additions & 1 deletion app/controllers/book_controller.rb
Expand Up @@ -21,7 +21,11 @@ def view
@bookseer = BookseerInfo::link(@bookinfo) @bookseer = BookseerInfo::link(@bookinfo)
end 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 @not_available = Bookprice::NOT_AVAILABLE


respond_with(@stores) do |format| respond_with(@stores) do |format|
Expand Down
31 changes: 29 additions & 2 deletions app/models/bookprice.rb
Expand Up @@ -6,6 +6,27 @@ class Bookprice


NOT_AVAILABLE = 999_999 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 class << self


def find_price_at_end(text) def find_price_at_end(text)
Expand Down Expand Up @@ -33,16 +54,22 @@ def is_isbn(text)
def searches def searches
# e.g. ["search_a1books", "search_infibeam", "search_rediff"] # e.g. ["search_a1books", "search_infibeam", "search_rediff"]
functions = self.methods.select { |name| name =~ /^search_(\w+)$/ }.sort 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) ] } functions.collect { |fname| [ fname.split("_")[1].to_sym, self.method(fname) ] }
end end


def names def names
self.searches.map { |name, search| name.to_s }.sort.map(&:to_sym) self.searches.map { |name, search| name.to_s }.sort.map(&:to_sym)
end 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) 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] } self.searches.map { |name, search| [name, search.call(isbn)] }.sort_by { |p| p[1][:price] }
end end


Expand Down
53 changes: 32 additions & 21 deletions app/views/book/view.html.erb
Expand Up @@ -29,26 +29,37 @@
<% end %> <% end %>
<% end %> <% end %>
<h2>Where to Buy</h2> <% unless @stores.nil? %>
<div class="stores"> <h2>Where to Buy</h2>
<p>(Sorted by price)</p> <div class="stores">
<table> <p>(Sorted by price)</p>
<% @stores.each do |store, data| %> <table>
<tr> <% @stores.each do |store, data| %>
<td> <tr>
<a href="<%= data[:url] %>"><%= store %></a> <td>
</td> <a rel="nofollow" href="<%= data[:url] %>"><%= store %></a>
<td> </td>
<% unless data[:price] == @not_available %> <td>
<span style="font-family:rupee">R</span> <%= data[:price] %> <% unless data[:price] == @not_available %>
<% else %> <span style="font-family:rupee">R</span> <%= data[:price] %>
not available <% else %>
<% end %> not available
</td> <% end %>
</tr> </td>
<% end %> </tr>
</table> <% end %>
</div> </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> <h2>Reviews</h2>
<div class="goodreads"> <div class="goodreads">
Expand All @@ -57,5 +68,5 @@


<h2>What to read next?</h2> <h2>What to read next?</h2>
<% unless @bookseer.nil? %> <% unless @bookseer.nil? %>
<a href="<%= @bookseer %>">Consult the Bookseer</a> <a rel="nofollow" href="<%= @bookseer %>">Consult the Bookseer</a>
<% end %> <% end %>
5 changes: 5 additions & 0 deletions script/delayed_job
@@ -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.