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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3005d3300710dce10002ab997b887d1d7d8e57899f2d982e2f8a6ff19bf0e14f
4
- data.tar.gz: 79e3c02feb45bb14a190a13530c30d55578a3e1ab7db0348fad88257a2e5d3ab
3
+ metadata.gz: 4f0a9e96db0b71262e34f4e25fd4e5206ad4325ab02244c04f28f83dc3c89571
4
+ data.tar.gz: 42ec40c4d8e330a12bc990ba985fbca89e999f6c16424259b3b2d953946caa56
5
5
  SHA512:
6
- metadata.gz: 2fc6b15a55356e037e6ab2794f4a18c002e12093878eef4c002925dd9919d7207544d747a7bc99ece856a42ae0a18e1eb1f142fa715b86b264dd8e302f819107
7
- data.tar.gz: '0949b3817b23065a3fcfb9e6dfb4185c6dfe11dc8685ab9765376ead3663084a3c05634f6ed7725afe5efa4b92cf802d141cc564eb960491c683e1bd95774c5a'
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-covers'
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"
@@ -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
- Net::HTTP.start(openlibrary_domain, openlibrary_port) do |http|
56
- response = http.head(openlibrary_path(identifier, :S, default_behavior: false))
57
- end
58
- return true if %w[200 301 302].include? response.code
59
-
60
- false
61
- end
62
- end
63
- end
64
- end
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Openlibrary
4
4
  module Covers
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
  end
7
7
  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.0
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: 2020-10-31 00:00:00.000000000 Z
11
+ date: 2021-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-benchmark