openlibrary-covers 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/openlibrary/covers.rb +68 -64
- data/lib/openlibrary/covers/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f0a9e96db0b71262e34f4e25fd4e5206ad4325ab02244c04f28f83dc3c89571
|
4
|
+
data.tar.gz: 42ec40c4d8e330a12bc990ba985fbca89e999f6c16424259b3b2d953946caa56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac47fdcbb7378c17cf2fe9212fe3d52b638d0346f9ebd864210082934875345dbc267312835462c8902d5baf48d4a297eeb3952c37708e63e5c9bdc4c81c32fa
|
7
|
+
data.tar.gz: 7c266f4c07628f4df37e08550a64e19374f590c0eef3173ddecd0d06fd9a5b5038dc2b9c32a3aacb45baf4d2ddafec004fa886ae10fcad16cdee62f529cb6814
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
require 'openlibrary
|
25
|
+
require 'openlibrary/covers'
|
26
26
|
image = Openlibrary::Covers::Image.new '9780786965601'
|
27
27
|
image.found? # => true
|
28
28
|
image.url # => "http://covers.openlibrary.org/b/isbn/9780786965601-M.jpg"
|
data/lib/openlibrary/covers.rb
CHANGED
@@ -1,64 +1,68 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'openlibrary/covers/version'
|
4
|
-
require 'net/http'
|
5
|
-
|
6
|
-
module Openlibrary
|
7
|
-
module Covers
|
8
|
-
##
|
9
|
-
# An Image from the OpenLibrary API, if one exists
|
10
|
-
class Image
|
11
|
-
##
|
12
|
-
# @param [String, Array<string>] identifiers the actual values to look up
|
13
|
-
# @param [String, Symbol] identifier_type
|
14
|
-
def initialize(identifiers, identifier_type = :isbn)
|
15
|
-
identifiers = Array(identifiers)
|
16
|
-
@identifier_type = identifier_type
|
17
|
-
|
18
|
-
@found = false
|
19
|
-
|
20
|
-
@successful_identifier = identifiers.detect { |identifier| image_exists(identifier) }
|
21
|
-
@found = true if @successful_identifier
|
22
|
-
end
|
23
|
-
|
24
|
-
def found?
|
25
|
-
@found
|
26
|
-
end
|
27
|
-
|
28
|
-
##
|
29
|
-
# @param [String, Symbol] size (can be 'S', 'M', 'L', :S, :M, :L)
|
30
|
-
def url(size = 'M')
|
31
|
-
@found ? openlibrary_url(@successful_identifier, size) : nil
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def openlibrary_domain
|
37
|
-
'covers.openlibrary.org'
|
38
|
-
end
|
39
|
-
|
40
|
-
def openlibrary_port
|
41
|
-
80
|
42
|
-
end
|
43
|
-
|
44
|
-
def openlibrary_url(identifier, size, default_behavior: true)
|
45
|
-
"http://#{openlibrary_domain}#{openlibrary_path(identifier, size, default_behavior: default_behavior)}"
|
46
|
-
end
|
47
|
-
|
48
|
-
def openlibrary_path(identifier, size, default_behavior: true)
|
49
|
-
base_path = "/b/#{@identifier_type}/#{identifier}-#{size}.jpg"
|
50
|
-
default_behavior ? base_path : "#{base_path}?default=false"
|
51
|
-
end
|
52
|
-
|
53
|
-
def image_exists(identifier)
|
54
|
-
response = nil
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'openlibrary/covers/version'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
module Openlibrary
|
7
|
+
module Covers
|
8
|
+
##
|
9
|
+
# An Image from the OpenLibrary API, if one exists
|
10
|
+
class Image
|
11
|
+
##
|
12
|
+
# @param [String, Array<string>] identifiers the actual values to look up
|
13
|
+
# @param [String, Symbol] identifier_type
|
14
|
+
def initialize(identifiers, identifier_type = :isbn)
|
15
|
+
identifiers = Array(identifiers)
|
16
|
+
@identifier_type = identifier_type
|
17
|
+
|
18
|
+
@found = false
|
19
|
+
|
20
|
+
@successful_identifier = identifiers.detect { |identifier| image_exists(identifier) }
|
21
|
+
@found = true if @successful_identifier
|
22
|
+
end
|
23
|
+
|
24
|
+
def found?
|
25
|
+
@found
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# @param [String, Symbol] size (can be 'S', 'M', 'L', :S, :M, :L)
|
30
|
+
def url(size = 'M')
|
31
|
+
@found ? openlibrary_url(@successful_identifier, size) : nil
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def openlibrary_domain
|
37
|
+
'covers.openlibrary.org'
|
38
|
+
end
|
39
|
+
|
40
|
+
def openlibrary_port
|
41
|
+
80
|
42
|
+
end
|
43
|
+
|
44
|
+
def openlibrary_url(identifier, size, default_behavior: true)
|
45
|
+
"http://#{openlibrary_domain}#{openlibrary_path(identifier, size, default_behavior: default_behavior)}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def openlibrary_path(identifier, size, default_behavior: true)
|
49
|
+
base_path = "/b/#{@identifier_type}/#{identifier}-#{size}.jpg"
|
50
|
+
default_behavior ? base_path : "#{base_path}?default=false"
|
51
|
+
end
|
52
|
+
|
53
|
+
def image_exists(identifier)
|
54
|
+
response = nil
|
55
|
+
begin
|
56
|
+
Net::HTTP.start(openlibrary_domain, openlibrary_port) do |http|
|
57
|
+
response = http.head(openlibrary_path(identifier, :S, default_behavior: false))
|
58
|
+
end
|
59
|
+
rescue Errno::ECONNREFUSED, Net::OpenTimeout, SocketError
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
return true if %w[200 301 302].include? response.code
|
63
|
+
|
64
|
+
false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openlibrary-covers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jane Sandberg
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-benchmark
|