Class: Alexandria::BookProviders

Inherits:
Array
  • Object
show all
Includes:
Logging, GetText, Observable, Singleton
Defined in:
lib/alexandria/book_providers.rb,
lib/alexandria/book_providers/mcu.rb,
lib/alexandria/book_providers/web.rb,
lib/alexandria/book_providers/z3950.rb,
lib/alexandria/book_providers/douban.rb,
lib/alexandria/book_providers/proxis.rb,
lib/alexandria/book_providers/renaud.rb,
lib/alexandria/book_providers/thalia.rb,
lib/alexandria/book_providers/adlibris.rb,
lib/alexandria/book_providers/deastore.rb,
lib/alexandria/book_providers/worldcat.rb,
lib/alexandria/book_providers/siciliano.rb,
lib/alexandria/book_providers/amazon_aws.rb,
lib/alexandria/book_providers/barnes_and_noble.rb

Overview

FIXME: Use delegation instead of inheritance.

Defined Under Namespace

Classes: AbstractProvider, AdLibrisProvider, AmazonProvider, BLProvider, BarnesAndNobleProvider, DeaStoreProvider, DoubanProvider, GenericProvider, InvalidSearchTypeError, LOCProvider, MCUProvider, NoResultsError, Preferences, ProviderSkippedError, ProxisProvider, RENAUDProvider, SBNProvider, SearchEmptyError, SearchError, SicilianoProvider, 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

#initializeBookProviders

Returns a new instance of BookProviders.



315
316
317
318
319
# File 'lib/alexandria/book_providers.rb', line 315

def initialize
  @prefs = Alexandria::Preferences.instance
  @abstract_classes = []
  update_priority
end

Instance Attribute Details

#abstract_classesObject (readonly)

Returns the value of attribute abstract_classes.



313
314
315
# File 'lib/alexandria/book_providers.rb', line 313

def abstract_classes
  @abstract_classes
end

Class Method Details

.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

.method_missing(id, *args, &block) ⇒ Object

FIXME: Define the handful of methods that use this.



371
372
373
374
375
376
377
# File 'lib/alexandria/book_providers.rb', line 371

def self.method_missing(id, *args, &block)
  if instance.respond_to? id
    instance.method(id).call(*args, &block)
  else
    super
  end
end

.search(criterion, type) ⇒ Object



44
45
46
47
48
49
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 44

def self.search(criterion, type)
  factory_n = 0
  # puts "book_providers search #{self.instance.count_observers}"

  begin
    factory = instance[factory_n]
    puts factory.fullname + ' lookup' if $DEBUG
    unless factory.enabled
      puts factory.fullname + ' disabled!, skipping...' if $DEBUG
      raise ProviderSkippedError
    end
    instance.changed
    instance.notify_observers(:searching, factory.fullname) # new
    results = factory.search(criterion, type)

    # sanity check if at least one valid result is actually found
    results.delete_if { |book, _cover| book.nil? }

    if results.empty?
      instance.changed
      instance.notify_observers(:not_found, factory.fullname) # new
      raise NoResultsError
    else
      log.info { 'found at ' + factory.fullname }
      instance.changed
      instance.notify_observers(:found, factory.fullname) # new
      return results
    end
  rescue => boom
    if boom.is_a? NoResultsError
      unless boom.instance_of? ProviderSkippedError
        instance.changed
        instance.notify_observers(:not_found, factory.fullname) # new
        Thread.new { sleep(0.5) }.join
      end
    else
      instance.changed
      instance.notify_observers(:error, factory.fullname) # new
      Thread.new { sleep(0.5) }.join # hrmmmm, to make readable...
      trace = boom.backtrace.join("\n >")
      log.warn { "Provider #{factory.name} encountered error: #{boom.message} #{trace}" }
    end
    if last == factory
      log.warn { "Error while searching #{criterion}" }
      message = case boom
                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, boom.message)

                when NoResultsError
                  _('No results were found.  Make sure your ' \
                    'search criterion is spelled correctly, and ' \
                    'try again.')

                when 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
                  boom.message
                end
      puts "raising empty error #{message}"
      raise SearchEmptyError, message
    else
      factory_n += 1
      retry
    end
  end
end

Instance Method Details

#update_priorityObject



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
361
362
363
364
365
366
367
368
# File 'lib/alexandria/book_providers.rb', line 321

def update_priority
  # This is weird code that sorts through the list of classes brought
  # in by requires and sorts through whether they are 'Abstract' or not,
  # adding their names to @prefs.

  @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.ancestors.include?(AbstractProvider) &&
        (klass != GenericProvider) &&
        (klass != WebsiteBasedProvider) &&
        (klass != AbstractProvider)

      if klass.abstract?
        @abstract_classes << klass
      else
        providers[md[1]] = klass.instance
      end
    end
  end
  if (ary = @prefs.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