Class: Alexandria::BookProviders
- Inherits:
-
Array
- Object
- Array
- Alexandria::BookProviders
show all
- Includes:
- Logging, GetText, Observable, Singleton
- Defined in:
- lib/alexandria/book_providers.rb,
lib/alexandria/book_providers/web.rb,
lib/alexandria/book_providers/douban.rb,
lib/alexandria/book_providers/worldcat.rb,
lib/alexandria/book_providers/bl_provider.rb,
lib/alexandria/book_providers/loc_provider.rb,
lib/alexandria/book_providers/sbn_provider.rb,
lib/alexandria/book_providers/z3950_provider.rb,
lib/alexandria/book_providers/thalia_provider.rb
Overview
FIXME: Use delegation instead of inheritance.
Defined Under Namespace
Classes: AbstractProvider, BLProvider, DoubanProvider, GenericProvider, InvalidSearchTypeError, LOCProvider, NoResultsError, Preferences, ProviderSkippedError, SBNProvider, SearchEmptyError, SearchError, ThaliaProvider, TooManyResultsError, WebsiteBasedProvider, WorldCatProvider, Z3950Provider
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logging
included, #log
Constructor Details
Returns a new instance of BookProviders.
304
305
306
307
308
|
# File 'lib/alexandria/book_providers.rb', line 304
def initialize
@prefs = Alexandria::Preferences.instance
@abstract_classes = []
update_priority
end
|
Instance Attribute Details
#abstract_classes ⇒ Object
Returns the value of attribute abstract_classes.
302
303
304
|
# File 'lib/alexandria/book_providers.rb', line 302
def abstract_classes
@abstract_classes
end
|
Class Method Details
.abstract_classes ⇒ Object
366
367
368
|
# File 'lib/alexandria/book_providers.rb', line 366
def self.abstract_classes
instance.abstract_classes
end
|
.isbn_search(criterion) ⇒ Object
125
126
127
|
# File 'lib/alexandria/book_providers.rb', line 125
def self.isbn_search(criterion)
search(criterion, SEARCH_BY_ISBN)
end
|
.list ⇒ Object
362
363
364
|
# File 'lib/alexandria/book_providers.rb', line 362
def self.list
instance
end
|
.search(criterion, type) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/alexandria/book_providers.rb', line 50
def self.search(criterion, type)
factory_n = 0
begin
factory = instance[factory_n]
log.debug { factory.fullname + " lookup" }
unless factory.enabled
log.debug { factory.fullname + " disabled!, skipping..." }
raise ProviderSkippedError
end
instance.changed
instance.notify_observers(:searching, factory.fullname) results = factory.search(criterion, type)
results.delete_if { |book, _cover| book.nil? }
if results.empty?
instance.changed
instance.notify_observers(:not_found, factory.fullname) raise NoResultsError
else
log.info { "found at " + factory.fullname }
instance.changed
instance.notify_observers(:found, factory.fullname) results
end
rescue StandardError => ex
if ex.is_a? NoResultsError
unless ex.instance_of? ProviderSkippedError
instance.changed
instance.notify_observers(:not_found, factory.fullname) Thread.new { sleep(0.5) }.join
end
else
instance.changed
instance.notify_observers(:error, factory.fullname) Thread.new { sleep(0.5) }.join trace = ex.backtrace.join("\n >")
log.warn { "Provider #{factory.name} encountered error: #{ex.message} #{trace}" }
end
if factory == instance.last
log.warn { "Error while searching #{criterion}" }
message = case ex
when Timeout::Error
_("Couldn't reach the provider '%s': timeout " \
"expired.") % factory.name
when SocketError
format(_("Couldn't reach the provider '%s': socket " \
"error (%s)."), factory.name, ex.message)
when NoResultsError, ProviderSkippedError
_("No results were found. Make sure your " \
"search criterion is spelled correctly, and " \
"try again.")
when TooManyResultsError
_("Too many results for that search.")
when InvalidSearchTypeError
_("Invalid search type.")
else
ex.message
end
log.debug { "raising empty error #{message}" }
raise SearchEmptyError, message else
factory_n += 1
retry
end
end
end
|
Instance Method Details
#update_priority ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
# File 'lib/alexandria/book_providers.rb', line 310
def update_priority
@abstract_classes.clear
providers = {}
self.class.constants.each do |constant|
md = /(.+)Provider$/.match(constant)
next unless md
klass = self.class.module_eval(constant.to_s)
if klass < AbstractProvider &&
(klass != GenericProvider) &&
(klass != WebsiteBasedProvider)
if klass.abstract?
@abstract_classes << klass
else
providers[md[1]] = klass.instance
end
end
end
if (ary = @prefs.get_variable :abstract_providers)
ary.each do |name|
md = /^(.+)_/.match(name)
next unless md
klass_name = md[1] + "Provider"
klass = @abstract_classes.find { |x| x.name.include?(klass_name) }
next unless klass
fullname = @prefs.send(name.downcase + "_name")
next unless fullname
instance = klass.new
instance.name = name
instance.fullname = fullname
instance.prefs.read
providers[name] = instance
end
end
clear
rejig_providers_priority
priority = (@prefs.providers_priority || [])
priority.map!(&:strip)
rest = providers.keys - priority
priority.each { |pname| self << providers[pname] }
rest.sort.each { |pname| self << providers[pname] }
compact!
end
|