Permalink
Browse files

Removes unused BookRespository class

  • Loading branch information...
1 parent 716900e commit 8ab8eb9f60df98a574bf37d3bf766f65b187b893 @boutros boutros committed Aug 22, 2012
Showing with 0 additions and 199 deletions.
  1. +0 −6 README.md
  2. +0 −124 app/models/book_repository.rb
  3. +0 −69 spec/models/book_repository_spec.rb
View
@@ -30,9 +30,3 @@ To prepare your local Virtuoso server for SPARQL update, you'll need to set up a
This app currently uses basic auth, so that's what the /sparql-auth endpoint needs to implement.
A good place to start is the documentation at http://virtuoso.openlinksw.com/dataspace/dav/wiki/Main/VOSStart
-
-## Notes
-The following classes are not yet in use or will probably disapper:
-
-* BookRepository
-
@@ -1,124 +0,0 @@
-require 'rest_client'
-require 'nokogiri'
-
-class BookRepository
-
- def self.find_by_isbn(isbn)
- book = nil
- isbn = sanitize_isbn(isbn)
- return book if isbn.nil? or isbn == ""
- response = results_for_isbn(isbn)
- return book if response.empty?
-
- book = parse_response(response)
- book.isbn = isbn
- book.cover_url = fetch_cover_art(isbn)
- book
- end
-
- def self.find_by_tnr(tnr)
- book = nil
- return book if tnr.nil? or tnr == ""
- response = results_for_tnr(tnr)
- return book if response.empty?
-
- book = parse_response(response)
- book.cover_url = fetch_cover_art(book.isbn)
- book
- end
-
- def self.parse_response(response)
- result = response.inject({}) do |aggr, solution|
- book = {}
- solution.each_binding do |name,value|
- book[name] = value.to_s
- end
- aggr.merge!(book) do |key, old, new|
- old == new ? new : [old, new].flatten.uniq
- end
- end
- Book.new result
- end
-
- def self.results_for_isbn(isbn)
- books_graph = '<http://data.deichman.no/books>'
- query = <<-eos
- PREFIX bibo: <http://purl.org/ontology/bibo/>
- PREFIX dct: <http://purl.org/dc/terms/>
- PREFIX foaf: <http://xmlns.com/foaf/0.1/>
- PREFIX fabio: <http://purl.org/spar/fabio/>
- PREFIX bibsys: <http://def.bibsys.no/xmlns/radatana/1.0#>
-
- SELECT ?title ?author ?work WHERE {
- GRAPH #{books_graph} {
- ?book bibo:isbn "#{isbn}" ;
- dct:title ?title ;
- dct:creator ?creator .
- ?creator bibsys:catalogueName ?author .
- }
- }
- eos
- ENDPOINT_QUERY.query(query)
- end
-
- def self.results_for_tnr(tnr)
- query = <<-eos
- PREFIX foaf: <http://xmlns.com/foaf/0.1/>
- PREFIX bibo: <http://purl.org/ontology/bibo/>
- PREFIX dc: <http://purl.org/dc/terms/>
-
- SELECT ?title ?author ?isbn WHERE {
- <http://data.deichman.no/resource/tnr_#{tnr}> a bibo:Document ;
- dc:title ?title ;
- dc:creator ?creator ;
- bibo:isbn ?isbn .
- ?creator foaf:name ?author .
- }
- eos
- ENDPOINT_QUERY.query(query)
- end
-
- def self.merge(response)
- titles = response.map(&:title).uniq
- authors = response.map(&:author).uniq.join(', ')
- books = []
- titles.each do |title|
- book = response.detect{|b| b.title == title}
- book.author = authors
- books << book
- end
- books
- end
-
- def self.fetch_cover_art(isbn)
- # search bokkilden with author lastname and book title as in melville+moby+dick
- # find first occurrence of Produkt that has Innbinding value Innbundet or Paperback
- candidates = isbn.is_a?(Array) ? isbn : [isbn]
- result = ""
- candidates.each do |c|
- response = RestClient.get(search_url_for(c))
- res = Nokogiri::XML(response)
- cover_url = res.xpath("/Produkter/Produkt[Innbinding='Paperback' or Innbinding='Innbundet']/BildeURL").text
- unless cover_url.blank?
- result = cover_url.gsub('&width=80', '')
- end
- break unless result.blank?
- end
- result
- end
-
- def self.search_string_for(book)
- ary = book.title.strip.split /\s+/
- ary.unshift book.author.split(',').first.strip
- search_string = CGI.escape ary.join(' ')
- end
-
- def self.search_url_for(isbn)
- search_url = "http://partner.bokkilden.no/SamboWeb/partner.do?rom=MP&format=XML&uttrekk=5&pid=0&ept=3&xslId=117&antall=10&enkeltsok=#{isbn}&profil=partner&sort=popularitet&order=DESC"
- end
-
- def self.sanitize_isbn(isbn="")
- isbn.strip.gsub(/\s+|-/, '')
- end
-
-end
@@ -1,69 +0,0 @@
-require_relative "../../app/models/book_repository"
-require 'spec_helper'
-require_relative "../../app/models/book"
-
-describe BookRepository do
- let(:valid_isbn) { '9781591169222' }
-
- context 'find by isbn', :vcr do
- it 'returns a collection of books' do
- result = BookRepository.find_by_isbn(valid_isbn)
- result.should be_a Book
- end
-
- it 'handles empty search string' do
- result = BookRepository.find_by_isbn("")
- result.should be_nil
- end
-
- it 'handles invalid search string' do
- result = BookRepository.find_by_isbn("12345")
- result.should be_nil
- end
- end
-
- context 'fetch results', :vcr do
- it 'returns results for a valid isbn' do
- response = BookRepository.results_for_isbn(valid_isbn)
- response.size.should == 4
- end
- end
-
- context 'fetch cover art', :vcr do
- it 'fetches a cover url' do
- cover_url = BookRepository.fetch_cover_art(valid_isbn)
- cover_url.should_not be_nil
- end
- end
-
- it "creates a valid search url for a book" do
- BookRepository.search_url_for(valid_isbn).should ==
- 'http://partner.bokkilden.no/SamboWeb/partner.do?rom=MP&format=XML&'\
- 'uttrekk=5&pid=0&ept=3&xslId=117&antall=10&'\
- 'enkeltsok=' + valid_isbn + '&'\
- 'profil=partner&sort=popularitet&order=DESC'
- end
-
- it "strips unwanted characters" do
- BookRepository.sanitize_isbn(" 2 - 3 ").should == "23"
- end
-
- it 'formats a valid search string' do
- book = Book.new(title: 'Moby Dick', author: 'Herman Melville')
- BookRepository.search_string_for(book).should == 'Herman+Melville+Moby+Dick'
- end
-
- context 'merge results' do
- let(:book1) { Book.new(title: 'One', author: 'fred') }
- let(:book2) { Book.new(title: 'One', author: 'barney') }
- let(:book3) { Book.new(title: 'Two', author: 'wilma') }
-
- it 'merges results based on title' do
- candidates = [book1, book2, book3]
- authors = candidates.map(&:author).uniq.join(', ')
- book1.author = book2.author = authors
- BookRepository.merge(candidates).should == [book1, book3]
- end
- end
-
-end

0 comments on commit 8ab8eb9

Please sign in to comment.