diff --git a/app/models/book.rb b/app/models/book.rb index b116b1c8..47012fd6 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -32,27 +32,9 @@ def populate! end def merge(response) - self.author = response.author if self.author.blank? and response.respond_to? :author - self.dewey_decimal = response.dewey_decimal if self.dewey_decimal.blank? and response.respond_to? :dewey_decimal - self.editor = response.editor if self.editor.blank? and response.respond_to? :editor - self.format = response.format if self.format.blank? and response.respond_to? :format - self.full_text = response.full_text if self.full_text.blank? and response.respond_to? :full_text - self.google_books_embeddable = response.google_books_embeddable if self.google_books_embeddable.blank? and response.respond_to? :google_books_embeddable - self.google_books_id = response.google_books_id if self.google_books_id.blank? and response.respond_to? :google_books_id - self.introducer = response.introducer if self.introducer.blank? and response.respond_to? :introducer - self.isbn_10 = response.isbn_10 if self.isbn_10.blank? and response.respond_to? :isbn_10 - self.isbn_13 = response.isbn_13 if self.isbn_13.blank? and response.respond_to? :isbn_13 - self.lang = response.lang if self.lang.blank? and response.respond_to? :lang - self.lcc_number = response.lcc_number if self.lcc_number.blank? and response.respond_to? :lcc_number - self.library_thing_id = response.library_thing_id if self.library_thing_id.blank? and response.respond_to? :library_thing_id - self.open_library_id = response.open_library_id if self.open_library_id.blank? and response.respond_to? :open_library_id - self.page_count = response.page_count if self.page_count.blank? and response.respond_to? :page_count - self.published_city = response.published_city if self.published_city.blank? and response.respond_to? :published_city - self.published_date = response.published_date if self.published_date.blank? and response.respond_to? :published_date - self.publisher = response.publisher if self.publisher.blank? and response.respond_to? :publisher - self.title = response.title if self.title.blank? and response.respond_to? :title - self.translator = response.translator if self.translator.blank? and response.respond_to? :translator - self.weight = response.weight if self.weight.blank? and response.respond_to? :weight + response.metadata.each do |key, value| + self.send("#{ key }=", value) if !value.blank? && self.send("#{ key }").blank? + end end def scan_notes_for_references diff --git a/app/models/google_books.rb b/app/models/google_books.rb index f9096692..db048448 100644 --- a/app/models/google_books.rb +++ b/app/models/google_books.rb @@ -5,33 +5,32 @@ class GoogleBooks base_uri Settings.books.google_books.domain - attr_accessor :isbn, :title, :author, :lang, :published_date, :isbn_10, :isbn_13, :page_count, :google_books_id, - :google_books_embeddable, :response + attr_accessor :metadata def initialize(isbn) - @isbn = isbn - params = { 'country' => 'GB', 'q' => "ISBN:#{ isbn }", 'maxResults' => 1 } response = self.class.get(Settings.books.google_books.path, query: params) - populate(response) if response && response['items'].first['volumeInfo'] + populate(response, isbn) if response && response['items'].first['volumeInfo'] end - def populate(response) + def populate(response, isbn) response = response['items'].first volume_info = response['volumeInfo'] + metadata = {} # GoogleBooks does not return nil when a book is not found so we need to verify that the data returned is # relevant if volume_info['industryIdentifiers'].collect { |id| id['identifier'] }.include? isbn - @response = response - @google_books_embeddable = response['accessInfo']['embeddable'] - @title = volume_info['title'] - @author = volume_info['authors'].flatten.join(', ') - @lang = volume_info['language'] - @published_date = volume_info['publishedDate'] - @page_count = volume_info['pageCount'] - @google_books_id = response['id'] + metadata['google_books_embeddable'] = response['accessInfo']['embeddable'] + metadata['title'] = volume_info['title'] + metadata['author'] = volume_info['authors'].flatten.join(', ') + metadata['lang'] = volume_info['language'] + metadata['published_date'] = volume_info['publishedDate'] + metadata['page_count'] = volume_info['pageCount'] + metadata['google_books_id'] = response['id'] end + + self.metadata = metadata end end diff --git a/app/models/isbndb.rb b/app/models/isbndb.rb index d86dc4ec..eb57879f 100644 --- a/app/models/isbndb.rb +++ b/app/models/isbndb.rb @@ -5,14 +5,10 @@ class Isbndb base_uri Settings.books.isbndb.domain - attr_accessor :isbn, :data, :title, :publisher, :published_city, :published_date, :isbn_10, :isbn_13, :response, - :dewey_decimal, :lcc_number + attr_accessor :metadata def initialize(isbn) - - @isbn = isbn - - response = self.class.get("http://isbndb.com/api/v2/json/#{ Secret.auth.isbndb.api_key }/book/#{ isbn }") + response = self.class.get("#{ Settings.books.isbndb.path }#{ Secret.auth.isbndb.api_key }/book/#{ isbn }") response = JSON.parse response @@ -21,19 +17,21 @@ def initialize(isbn) def populate(response) response = response['data'].first - @response = response + metadata = {} - @title = response['title'] - @publisher = response['publisher_name'] - # @title_long = response['Title'] if author_statement.nil? - # @author_statement = response[''] if title.nil? + metadata['title'] = response['title'] + metadata['publisher'] = response['publisher_name'] + # metadata['title_long'] = response['Title'] if author_statement.nil? + # metadata['author_statement'] = response[''] if title.nil? # Guess introducer and translator from authortext and description parsed_publisher_text = response['publisher_text'].scan(/(.*?) : (.*?)\, (c?\d\d\d\d)/) - @published_city = parsed_publisher_text[0][0] if parsed_publisher_text.size > 0 - @published_date = parsed_publisher_text[0][2] if parsed_publisher_text.size > 0 - @isbn_10 = response['isbn10'] - @isbn_13 = response['isbn13'] - @dewey_decimal = response['dewey_normal'] - @lcc_number = response['lcc_number'] + metadata['published_city'] = parsed_publisher_text[0][0] if parsed_publisher_text.size > 0 + metadata['published_date'] = parsed_publisher_text[0][2] if parsed_publisher_text.size > 0 + metadata['isbn_10'] = response['isbn10'] + metadata['isbn_13'] = response['isbn13'] + metadata['dewey_decimal'] = response['dewey_normal'] + metadata['lcc_number'] = response['lcc_number'] + + self.metadata = metadata end end diff --git a/app/models/open_library.rb b/app/models/open_library.rb index 5b50a5ac..f56f4e95 100644 --- a/app/models/open_library.rb +++ b/app/models/open_library.rb @@ -5,34 +5,31 @@ class OpenLibrary base_uri Settings.books.open_library.domain - attr_accessor :isbn, :title, :author, :isbn_10, :isbn_13, :page_count, :publisher, :library_thing_id, - :open_library_id, :response, :lcc_number, :dewey_decimal, :introducer, :editor, :translator + attr_accessor :metadata def initialize(isbn) - @isbn = isbn - params = { 'format' => 'json', 'jscmd' => 'details', 'bibkeys' => "ISBN:#{ isbn }" } response = self.class.get(Settings.books.open_library.path, query: params) - populate(response) if response["ISBN:#{ isbn }"] + populate(response, isbn) if response["ISBN:#{ isbn }"] end - def populate(response) + def populate(response, isbn) response = response["ISBN:#{ isbn }"]['details'] - - # TODO - # @editor = response['by_statement'].try { scan(/translated by (.*?)[.]/) } - # @introducer = response['by_statement'].try { scan(/translated by (.*?)[.]/) } - # @translator = response['by_statement'].try { scan(/translated by (.*?)[.]/) } - @author = response['authors'][0]['name'] - @dewey_decimal = response['dewey_decimal_class'].try { first } - @lcc_number = response['lccn'].try { first } - @library_thing_id = response['identifiers'].try { response['identifiers']['librarything'].first } - @open_library_id = response['identifiers'].try { response['identifiers']['goodreads'].first } - @page_count = response['number_of_pages'] - @publisher = response['publishers'].first - @response = response - @title = response['title'] - + metadata = {} + # TODO: + # metadata['editor = response['by_statement'].try { scan(/translated by (.*?)[.]/) } + # metadata['introducer = response['by_statement'].try { scan(/translated by (.*?)[.]/) } + # metadata['translator = response['by_statement'].try { scan(/translated by (.*?)[.]/) } + metadata['author'] = response['authors'][0]['name'] + metadata['dewey_decimal'] = response['dewey_decimal_class'].try { first } + metadata['lcc_number'] = response['lccn'].try { first } + metadata['library_thing_id'] = response['identifiers'].try { response['identifiers']['librarything'].first } + metadata['open_library_id'] = response['identifiers'].try { response['identifiers']['goodreads'].first } + metadata['page_count'] = response['number_of_pages'] + metadata['publisher'] = response['publishers'].first + metadata['title'] = response['title'] + + self.metadata = metadata end end diff --git a/app/models/world_cat.rb b/app/models/world_cat.rb index 14af946c..19ac0dc1 100644 --- a/app/models/world_cat.rb +++ b/app/models/world_cat.rb @@ -6,12 +6,9 @@ class WorldCat base_uri Settings.books.world_cat.domain - attr_accessor :isbn, :data, :title, :author, :author_statement, :publisher, :published_date, :translator, - :introducer, :editor, :response + attr_accessor :metadata def initialize(isbn) - @isbn = isbn - params = { 'recordSchema' => 'info:srw/schema/1/dc', 'servicelevel' => 'full', 'wskey' => Secret.auth.world_cat.api_key } response = self.class.get("#{ Settings.books.world_cat.path }#{ isbn }", query: params) @@ -21,19 +18,21 @@ def initialize(isbn) def populate(response) response = response['oclcdcs'] - @response = response - # OPTIONAL - # @author = response['creator'] - # @title = response['title'] - # @author_statement = response['Title'] - @publisher = Array(response['publisher']).first - @published_date = "1-1-#{ response['date'].scan(/\d\d\d\d/).first }" + metadata = {} + # OPTIONAL: + # @author = response['creator'] + # @title = response['title'] + # @author_statement = response['Title'] + metadata['publisher'] = Array(response['publisher']).first + metadata['published_date'] = "1-1-#{ response['date'].scan(/\d\d\d\d/).first }" if response['description'] description = Array(response['description']).join(' ') - # REVIEW: These regular expressions can be refined (by [A-Z]{1}\w) - @editor = description.scan(/edited.*? by ([\w ]+\w)/i).join(', ') - @introducer = description.scan(/introduc.*? by ([\w ]+\w)/i).join(', ') - @translator = description.scan(/translated.*? by ([\w ]+\w)/i).join(', ') + # OPTIMIZE: These regular expressions can be refined (by [A-Z]{1}\w) + metadata['editor'] = description.scan(/edited.*? by ([\w ]+\w)/i).join(', ') + metadata['introducer'] = description.scan(/introduc.*? by ([\w ]+\w)/i).join(', ') + metadata['translator'] = description.scan(/translated.*? by ([\w ]+\w)/i).join(', ') end + + self.metadata = metadata end end diff --git a/spec/models/google_books_spec.rb b/spec/models/google_books_spec.rb index 5191b865..fa9ca507 100644 --- a/spec/models/google_books_spec.rb +++ b/spec/models/google_books_spec.rb @@ -9,16 +9,15 @@ end end - subject { @google_books_book } + subject { @google_books_book.metadata } - its (:author) { should == 'Friedrich A. Kittler' } - its (:google_books_embeddable) { should == true } - its (:google_books_id) { should == 'nRo0Pk8djjoC' } - its (:isbn) { should == '0804720991' } - its (:lang) { should == 'en' } - its (:page_count) { should == 459 } - its (:published_date) { should == '1992-08-01' } - its (:title) { should == 'Discourse Networks 1800/1900' } + its (['author']) { should == 'Friedrich A. Kittler' } + its (['google_books_embeddable']) { should == true } + its (['google_books_id']) { should == 'nRo0Pk8djjoC' } + its (['lang']) { should == 'en' } + its (['page_count']) { should == 459 } + its (['published_date']) { should == '1992-08-01' } + its (['title']) { should == 'Discourse Networks 1800/1900' } end context 'when a book is not found' do @@ -30,13 +29,6 @@ subject { @google_books_book_nil } - its (:author) { should == nil } - its (:google_books_embeddable) { should == nil } - its (:google_books_id) { should == nil } - its (:lang) { should == nil } - its (:page_count) { should == nil } - its (:published_date) { should == nil } - its (:response) { should == nil } - its (:title) { should == nil } + its (:metadata) { should == {} } end end diff --git a/spec/models/isbndb_spec.rb b/spec/models/isbndb_spec.rb index 8e7aacd2..74df78db 100644 --- a/spec/models/isbndb_spec.rb +++ b/spec/models/isbndb_spec.rb @@ -5,22 +5,20 @@ context 'when a book is found:' do before do VCR.use_cassette('model/isbndb') do - # Secret['auth']['isbndb']['api_key'] = 'OBSCURED' @isbndb_book = Isbndb.new('0804720991') end end - subject { @isbndb_book } + subject { @isbndb_book.metadata } - its (:dewey_decimal) { should == '830.9357' } - its (:isbn) { should == '0804720991' } - its (:isbn_10) { should == '0804720991' } - its (:isbn_13) { should == '9780804720991' } - its (:lcc_number) { should == '' } - its (:published_city) { should == 'Stanford, Calif.' } - its (:published_date) { should == 'c1990' } - its (:publisher) { should == 'Stanford University Press' } - its (:title) { should == 'Discourse networks 1800/1900' } + its (['dewey_decimal']) { should == '830.9357' } + its (['isbn_10']) { should == '0804720991' } + its (['isbn_13']) { should == '9780804720991' } + its (['lcc_number']) { should == '' } + its (['published_city']) { should == 'Stanford, Calif.' } + its (['published_date']) { should == 'c1990' } + its (['publisher']) { should == 'Stanford University Press' } + its (['title']) { should == 'Discourse networks 1800/1900' } end context 'when a book is not found:' do @@ -32,14 +30,6 @@ subject { @isbndb_book_nil } - its (:dewey_decimal) { should == nil } - its (:isbn_10) { should == nil } - its (:isbn_13) { should == nil } - its (:lcc_number) { should == nil } - its (:published_city) { should == nil } - its (:published_date) { should == nil } - its (:publisher) { should == nil } - its (:response) { should == nil } - its (:title) { should == nil } + its (:metadata) { should == nil } end end \ No newline at end of file diff --git a/spec/models/open_library_spec.rb b/spec/models/open_library_spec.rb index d90f0ef5..093ea6b5 100644 --- a/spec/models/open_library_spec.rb +++ b/spec/models/open_library_spec.rb @@ -9,17 +9,16 @@ end end - subject { @open_library_book } + subject { @open_library_book.metadata } - its (:author) { should == 'Friedrich A. Kittler' } - its (:dewey_decimal) { should == nil } - its (:isbn) { should == '0804720991' } - its (:lcc_number) { should == nil } - its (:library_thing_id) { should == '430888' } - its (:open_library_id) { should == '212450' } - its (:page_count) { should == 459 } - its (:publisher) { should == 'Stanford University press' } - its (:title) { should == 'Discourse networks, 1800-1900' } + its (['author']) { should == 'Friedrich A. Kittler' } + its (['dewey_decimal']) { should == nil } + its (['lcc_number']) { should == nil } + its (['library_thing_id']) { should == '430888' } + its (['open_library_id']) { should == '212450' } + its (['page_count']) { should == 459 } + its (['publisher']) { should == 'Stanford University press' } + its (['title']) { should == 'Discourse networks, 1800-1900' } end @@ -32,14 +31,6 @@ subject { @open_library_book } - its (:author) { should == nil } - its (:dewey_decimal) { should == nil } - its (:lcc_number) { should == nil } - its (:library_thing_id) { should == nil } - its (:open_library_id) { should == nil } - its (:page_count) { should == nil } - its (:publisher) { should == nil } - its (:response) { should == nil } - its (:title) { should == nil } + its (:metadata) { should == nil } end end diff --git a/spec/models/world_cat_spec.rb b/spec/models/world_cat_spec.rb index 361b631d..70812d8a 100644 --- a/spec/models/world_cat_spec.rb +++ b/spec/models/world_cat_spec.rb @@ -9,14 +9,13 @@ end end - subject { @world_cat_book } + subject { @world_cat_book.metadata } - its (:editor) { should == '' } - its (:introducer) { should == '' } - its (:isbn) { should == '0804720991' } - its (:published_date) { should == '1-1-1990' } - its (:publisher) { should == 'Stanford University Press' } - its (:translator) { should == '' } + its (['editor']) { should == '' } + its (['introducer']) { should == '' } + its (['published_date']) { should == '1-1-1990' } + its (['publisher']) { should == 'Stanford University Press' } + its (['translator']) { should == '' } end context 'when a book is not found:' do @@ -26,13 +25,7 @@ end end - subject { @world_cat_book_nil } - - its (:editor) { should == nil } - its (:introducer) { should == nil } - its (:published_date) { should == nil } - its (:publisher) { should == nil } - its (:response) { should == nil } - its (:translator) { should == nil } + # TODO: This fails! + pending "its (:metadata) { should == nil }" end end