Sha256: bc09b58997391c440ea5c27d11938145cb1d667a361e9ea0469ccaebc1b732f8

Contents?: true

Size: 1019 Bytes

Versions: 3

Compression:

Stored size: 1019 Bytes

Contents

require 'cgi'
require 'httparty'

module Google
  module Book
    module Request
      include HTTParty
      format :xml

      class << self
        attr_accessor :parameters

        # 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 }
          parameters['start-index'] = opts[:page] if opts[:page]
          parameters['max-results'] = opts[:count] if opts[:count]

          Response.new(get(url.to_s))
        end

        private

        def query
          parameters.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
        end

        def url
          URI::HTTP.build(:host  => 'books.google.com',
                          :path  => '/books/feeds/volumes',
                          :query => query)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
google-book-0.2.2 lib/google/book/request.rb
google-book-0.2.1 lib/google/book/request.rb
google-book-0.2.0 lib/google/book/request.rb