class BookDetailsImporter
  class BookNotFound < Exception; end
  class InvalidResponse < Exception; end

  def self.find_by_isbn(isbn)
    format_response( GoogleBooks.search("isbn:#{isbn}").first )
  end

  def self.find_cover(isbn)
    format_response json_request("")
  end

  private
    def self.json_request(uri)
      response = Net::HTTP.get URI.parse(uri)
      JSON.parse(response)
    end

    def self.format_response(book)
      if book
        {
          :title => book.title,
          :author => book.authors,
          :google_id => book.id
        }
      elsif book.nil?
        raise BookNotFound, book.inspect
      else
        raise InvalidResponse, book.inspect
      end
    end
end

class BookDetailsImporter
  class BookNotFound < Exception; end
  class InvalidResponse < Exception; end

  def self.find_by_isbn(isbn)
    format_response( GoogleBooks.search("isbn:#{isbn}", { }, ENV['REQUEST_IP']).first )
  end

  def self.find_cover(isbn)
    format_response json_request("")
  end

  private
    def self.json_request(uri)
      response = Net::HTTP.get URI.parse(uri)
      JSON.parse(response)
    end

    def self.format_response(book)
      if book
        {
          :title => book.title,
          :author => book.authors,
          :google_id => book.id
        }
      elsif book.nil?
        raise BookNotFound, book.inspect
      else
        raise InvalidResponse, book.inspect
      end
    end
end

Allow IP to be sent in Google Books API request
Allow IP to be sent in Google Books API request

Google Books requires an IP address to be sent
with all requests for Geo purposes - this breaks
on Heroku so we need to specify one manually.