Class: Alexandria::BookProviders

Inherits:
Array
  • Object
show all
Includes:
Logging, GetText, Observable, Singleton
Defined in:
lib/alexandria/book_providers.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/adlibris.rb,
lib/alexandria/book_providers/worldcat.rb,
lib/alexandria/book_providers/siciliano.rb,
lib/alexandria/book_providers/amazon_aws.rb,
lib/alexandria/book_providers/thalia_provider.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, DoubanProvider, GenericProvider, InvalidSearchTypeError, LOCProvider, NoResultsError, Preferences, ProviderSkippedError, ProxisProvider, 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.



310
311
312
313
314
# File 'lib/alexandria/book_providers.rb', line 310

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

Instance Attribute Details

#abstract_classesObject (readonly)

Returns the value of attribute abstract_classes.



308
309
310
# File 'lib/alexandria/book_providers.rb', line 308

def abstract_classes
  @abstract_classes
end

Class Method Details

.abstract_classesObject



372
373
374
# File 'lib/alexandria/book_providers.rb', line 372

def self.abstract_classes
  instance.abstract_classes
end

.isbn_search(criterion) ⇒ Object



126
127
128
# File 'lib/alexandria/book_providers.rb', line 126

def self.isbn_search(criterion)
  search(criterion, SEARCH_BY_ISBN)
end

.listObject



368
369
370
# File 'lib/alexandria/book_providers.rb', line 368

def self.list
  instance
end

.search(criterion, type) ⇒ Object



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
124
# File 'lib/alexandria/book_providers.rb', line 46

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) # 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
      results
    end
  rescue StandardError => ex
    if ex.is_a? NoResultsError
      unless ex.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 = 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
                  _("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
                  ex.message
                end
      log.debug { "raising empty error #{message}" }
      raise SearchEmptyError, message # rubocop:disable GetText/DecorateFunctionMessage
    else
      factory_n += 1
      retry
    end
  end
end

Instance Method Details

#update_priorityObject



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

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 < 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