class Openlibrary::Covers::Image
An Image
from the OpenLibrary API, if one exists
Public Class Methods
new(identifiers, identifier_type = :isbn)
click to toggle source
@param [String, Array<string>] identifiers the actual values to look up @param [String, Symbol] identifier_type
# File lib/openlibrary/covers.rb, line 14 def initialize(identifiers, identifier_type = :isbn) identifiers = Array(identifiers) @identifier_type = identifier_type @found = false @successful_identifier = identifiers.detect { |identifier| image_exists(identifier) } @found = true if @successful_identifier end
Public Instance Methods
found?()
click to toggle source
# File lib/openlibrary/covers.rb, line 24 def found? @found end
url(size = 'M')
click to toggle source
@param [String, Symbol] size (can be 'S', 'M', 'L', :S, :M, :L)
# File lib/openlibrary/covers.rb, line 30 def url(size = 'M') @found ? openlibrary_url(@successful_identifier, size) : nil end
Private Instance Methods
image_exists(identifier)
click to toggle source
# File lib/openlibrary/covers.rb, line 53 def image_exists(identifier) response = nil begin Net::HTTP.start(openlibrary_domain, openlibrary_port) do |http| response = http.head(openlibrary_path(identifier, :S, default_behavior: false)) end rescue Errno::ECONNREFUSED, Net::OpenTimeout, SocketError return false end return true if %w[200 301 302].include? response.code false end
openlibrary_domain()
click to toggle source
# File lib/openlibrary/covers.rb, line 36 def openlibrary_domain 'covers.openlibrary.org' end
openlibrary_path(identifier, size, default_behavior: true)
click to toggle source
# File lib/openlibrary/covers.rb, line 48 def openlibrary_path(identifier, size, default_behavior: true) base_path = "/b/#{@identifier_type}/#{identifier}-#{size}.jpg" default_behavior ? base_path : "#{base_path}?default=false" end
openlibrary_port()
click to toggle source
# File lib/openlibrary/covers.rb, line 40 def openlibrary_port 80 end
openlibrary_url(identifier, size, default_behavior: true)
click to toggle source
# File lib/openlibrary/covers.rb, line 44 def openlibrary_url(identifier, size, default_behavior: true) "http://#{openlibrary_domain}#{openlibrary_path(identifier, size, default_behavior: default_behavior)}" end