Sha256: 23767e46eb38c0b791e6e8b2f45d145a3a4cb593e96db1e453fce272b42bb218
Contents?: true
Size: 1.81 KB
Versions: 2
Compression:
Stored size: 1.81 KB
Contents
require 'cgi' require 'httparty' # A classy finder module GoogleBook include HTTParty format :xml class << self # Query parameters passed on to Google attr_accessor :parameters # Total results for query attr_accessor :total_results # The query response attr_accessor :response # Queries the Google Book Search Data API. # # Optionally, specify a page and count to paginate through # the result set. # # GoogleBook.find('deleuze', :page => 2, :count => 20) # def find(query, opts={}) self.parameters = { 'q' => query } if opts[:page] && opts[:page].to_i > 0 parameters['start-index'] = opts[:page] end if opts[:count] && (10..20).include?(opts[:count].to_i) parameters['max-results'] = opts[:count] end self.response = self.get(uri.to_s) self.total_results = response['feed']['openSearch:totalResults'].to_i response['feed']['entry'].map do |book| Book.new( :images => Images.new(book['link'][0]['href']), :info => book['link'][1]['href'], :preview => book['link'][2]['href'], :creator => book['dc:creator'], :date => book['dc:date'], :description => book['dc:description'], :format => book['dc:format'], :identifier => book['dc:identifier'], :publisher => book['dc:publisher'], :subject => book['dc:subject'], :title => book['dc:title']) end end private def query parameters.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&') end def uri URI::HTTP.build :host => 'books.google.com', :path => '/books/feeds/volumes', :query => query end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
googlebook-0.1.1 | lib/googlebook/finder.rb |
googlebook-0.1.0 | lib/googlebook/finder.rb |