Class: WCID::Search
- Inherits:
-
Object
- Object
- WCID::Search
- Defined in:
- lib/wcid/search.rb
Overview
WCID::Search objects are for creating and running queries against WorldCat Identities.
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#do_search(uri) ⇒ Object
This should maybe be private or somesuch?.
-
#initialize(query) ⇒ Search
constructor
pass it a string name_search = WCID::Search.new(‘Twain, Mark’).
-
#search ⇒ Object
Actually performs the search returns array of Hit objects hits = name_search.search or hits = (WCID::Search.new(‘Twain, Mark’)).search.
-
#search_exact ⇒ Object
returns single ID object (the first hit) or nil hit = name_search.search_exact or hit = (WCID::Search.new(‘Twain, Mark’)).search_exact.
Constructor Details
#initialize(query) ⇒ Search
pass it a string name_search = WCID::Search.new(‘Twain, Mark’)
12 13 14 |
# File 'lib/wcid/search.rb', line 12 def initialize query @query = query end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
7 8 9 |
# File 'lib/wcid/search.rb', line 7 def query @query end |
Instance Method Details
#do_search(uri) ⇒ Object
This should maybe be private or somesuch?
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/wcid/search.rb', line 59 def do_search uri begin file = '' Timeout.timeout 20 do file = Net::HTTP.get(URI.parse("#{uri}")) #puts file end doc = REXML::Document.new file rescue Timeout::Error puts "timeout error!" raise rescue Errno::ECONNRESET puts "errno!" raise end end |
#search ⇒ Object
Actually performs the search returns array of Hit objects
hits = name_search.search
or
hits = (WCID::Search.new('Twain, Mark')).search
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/wcid/search.rb', line 22 def search uri = URI.escape("http://orlabs.oclc.org/Identities/find?fullName=#{@query}") doc = do_search uri m = doc.elements.to_a("nameAuthorities/match") hits = [] m.each do | m | hit_object = WCID::Hit.new(m) hits << hit_object end hits end |
#search_exact ⇒ Object
returns single ID object (the first hit) or nil hit = name_search.search_exact
or
hit = (WCID::Search.new(‘Twain, Mark’)).search_exact
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wcid/search.rb', line 39 def search_exact uri = URI.escape("http://orlabs.oclc.org/SRW/search/Identities?query=local.pnkey+exact+#{@query}") #puts uri begin doc = do_search uri #puts doc if doc.elements['searchRetrieveResponse/numberOfRecords'].text == '0' #puts "no record" return nil else #puts 'a record here' record = WCID::ID.new(doc) end rescue #puts 'um no text for node?' raise end end |