Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Improves tests for GoogleBooks API calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
joegatt committed Jun 3, 2013
1 parent 8f7f57c commit 9fbd110
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
25 changes: 15 additions & 10 deletions app/models/google_books.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
# encoding: utf-8

class GoogleBooks
include HTTParty

base_uri Settings.books.google_books.domain

attr_accessor :isbn, :title, :author, :lang, :published_date, :isbn_10, :isbn_13, :page_count, :google_books_id, :response

def initialize(isbn)
attr_accessor :isbn, :title, :author, :lang, :published_date, :isbn_10, :isbn_13, :page_count, :google_books_id,
:response

def initialize(isbn)
@isbn = isbn

params = { 'country' => 'GB', 'q' => "ISBN:#{ isbn }" }
response = self.class.get(Settings.books.google_books.path, :query => params)
response = self.class.get(Settings.books.google_books.path, query: params)

if response && response['items'].first
assign_values(response) if response && response['items'].first

rescue => error
puts "Error while fetching #{ isbn } from GoogleBooks."
puts error
puts error.backtrace
end

def assign_values(response)
response = response['items'].first
volume_info = response['volumeInfo']

Expand All @@ -23,10 +33,5 @@ def initialize(isbn)
@published_date = volume_info['publishedDate']
@page_count = volume_info['pageCount']
@google_books_id = response['id']
end
rescue => error
puts "Error while fetching #{ isbn } from GoogleBooks."
puts error
puts error.backtrace
end
end
13 changes: 10 additions & 3 deletions spec/models/google_books_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# encoding: utf-8

describe GoogleBooks do

before {
VCR.use_cassette('model/google_books', :decode_compressed_response => true) do
before do
VCR.use_cassette('model/google_books', decode_compressed_response: true) do
@google_book = GoogleBooks.new('0123456789')
end
}
end

subject { @google_book }

its (:title) { should == 'The Man Without Qualities' }
its (:author) { should == 'Professor Robert Musil' }
its (:lang) { should == 'en' }
its (:published_date) { should == '2011-09-16' }
its (:page_count) { should == 1130 }
its (:google_books_id) { should == 'EmQ6ygAACAAJ' }

end
Loading

0 comments on commit 9fbd110

Please sign in to comment.