relaton-isbn 1.17.0 → 1.17.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: 67f72a126942d761c281807d95cfb1c4feefb68a00029f3d284cd19d77022288
4
- data.tar.gz: fd31af74cfbaca0480129f4aa3f02e3cc4761022d2c3f2b8cb46c1278d46dd40
3
+ metadata.gz: b53fa9bf8bfa0f56c32b9282e3bf7f7937fb635b3a9d93c12dbe98274ed2e103
4
+ data.tar.gz: 27afa75b95b45dd585a1786586fe8bcbdf11f3c7c1c45b9ad619eb1b8d45020b
5
5
  SHA512:
6
- metadata.gz: 5d199ef47b949e0fdeec3b6fa2d43fe93e136faef853e6ab1cfa45e26378f2b4bdd2e308813f31eec60a60714ded305e85f9d67105b5224e03b92a4072f3feac
7
- data.tar.gz: 68b8bb3a2a2ea06f687a984145dedde9a782bcb9979e4286ba17bbcad4bc9a039942efa4b20313d98fe765d0c2cd73cff2e4b2fe6baffa1113ec345986c933aa
6
+ metadata.gz: f69d9927c61750e4f094b291f0d3247a1541830ad56cf7805d05b1c613fc97b621bd80363cddb2e1250daff24d114a36f22cedc61c0d4d7e1d56f76321b100d8
7
+ data.tar.gz: 0e617090456c285b8d5eec2dd57a12b6bb7ea523e3465312bb27b5f2584b45e5cc21b21dcc317442d089efc015d10cbecbdac98a913f855f2a57b6a40bbedc23
data/README.adoc CHANGED
@@ -11,7 +11,7 @@ image:https://img.shields.io/github/commits-since/relaton/relaton-isbn/latest.sv
11
11
  RelatonIsbn is a Ruby gem that implements the
12
12
  https://github.com/metanorma/metanorma-model-iso#iso-bibliographic-item[IsoBibliographicItem model].
13
13
 
14
- You can use it to retrieve metadata of Standards from https://crossref.org, and
14
+ You can use it to retrieve metadata of Standards from https://openlibrary.org, and
15
15
  access such metadata through the `BibliographicItem` object.
16
16
 
17
17
  == Installation
@@ -53,20 +53,80 @@ RelatonIsbn.configure do |config|
53
53
  end
54
54
  ----
55
55
 
56
- === Retrieving items of known type using OpenLibrary API
56
+ === Retrieving bibliographic items using OpenLibrary API
57
57
 
58
- If the resulting bibliographic item returned from CrossRef is a known Relaton
59
- flavor, such as a NIST or IEEE standard, the corresponding Relaton class object
60
- will be returned via the call.
58
+ To retrieve bibliographic items, use `RelatonIsbn::OpenLibrary.get` method with ISBN-10 or ISBN-13 as an argument. Prefix `ISBN` and hyphens are optional. The method returns `RelatonBib::BibliographicItem` object.
61
59
 
62
60
  [source,ruby]
63
61
  ----
64
- # get document by ISBN
65
- RelatonIsbn::OpenLibrary.get "ISBN 9780120644810"
62
+ # get document by ISBN-13
63
+ > bibitem = RelatonIsbn::OpenLibrary.get "ISBN 978-0-12-064481-0"
66
64
  [relaton-isbn] (ISBN 9780120644810) Fetching from OpenLibrary ...
67
65
  [relaton-isbn] (ISBN 9780120644810) Found: `9780120644810`
68
66
  => #<RelatonBib::BibliographicItem:0x0000000113889258
69
67
  ...
68
+
69
+ # get document by ISBN-10
70
+ > RelatonIsbn::OpenLibrary.get "0120644819"
71
+ [relaton-isbn] (ISBN 0120644819) Fetching from OpenLibrary ...
72
+ [relaton-isbn] (ISBN 0120644819) Found: `9780120644810`
73
+ => #<RelatonBib::BibliographicItem:0x00000001098ac960
74
+ ...
75
+ ----
76
+
77
+ === Serializing bibliographic items
78
+ [source,ruby]
79
+ ----
80
+ # serialize to XML
81
+ > puts bibitem.to_xml
82
+ <bibitem id="9780120644810" schema-version="v1.2.7">
83
+ <title type="main" format="text/plain">Graphics gems II</title>
84
+ <uri type="src">http://openlibrary.org/books/OL21119585M/Graphics_gems_II</uri>
85
+ <docidentifier type="ISBN" primary="true">9780120644810</docidentifier>
86
+ <date type="published">
87
+ <on>1991</on>
88
+ </date>
89
+ <contributor>
90
+ <role type="author"/>
91
+ <person>
92
+ <name>
93
+ <completename>James Arvo</completename>
94
+ </name>
95
+ </person>
96
+ </contributor>
97
+ <contributor>
98
+ <role type="publisher"/>
99
+ <organization>
100
+ <name>AP Professional</name>
101
+ </organization>
102
+ </contributor>
103
+ <place>
104
+ <city>Boston</city>
105
+ </place>
106
+ <place>
107
+ <city>London</city>
108
+ </place>
109
+ </bibitem>
110
+
111
+ # serialize to bibdata XML
112
+ > puts bibitem.to_xml bibdata: true
113
+ <bibdata schema-version="v1.2.7">
114
+ <title type="main" format="text/plain">Graphics gems II</title>
115
+ ...
116
+
117
+ # serialize to hash
118
+ > bibitem.to_hash
119
+ => {"schema-version"=>"v1.2.7",
120
+ "id"=>"9780120644810",
121
+ "title"=>[{"content"=>"Graphics gems II", "format"=>"text/plain", "type"=>"main"}],
122
+ "link"=>[{"content"=>"http://openlibrary.org/books/OL21119585M/Graphics_gems_II", "type"=>"src"}],
123
+ "docid"=>[{"id"=>"9780120644810", "type"=>"ISBN", "primary"=>true}],
124
+ "date"=>[{"type"=>"published", "value"=>"1991"}],
125
+ "contributor"=>
126
+ [{"person"=>{"name"=>{"completename"=>{"content"=>"James Arvo"}}, "url"=>"http://openlibrary.org/authors/OL2646519A/James_Arvo"}, "role"=>[{"type"=>"author"}]},
127
+ {"organization"=>{"name"=>[{"content"=>"AP Professional"}]}, "role"=>[{"type"=>"publisher"}]}],
128
+ "revdate"=>"1991",
129
+ "place"=>[{"city"=>"Boston"}, {"city"=>"London"}]}
70
130
  ----
71
131
 
72
132
  == Development
@@ -0,0 +1,59 @@
1
+ module RelatonIsbn
2
+ class Isbn
3
+ #
4
+ # Create ISBN object.
5
+ #
6
+ # @param [String] isbn ISBN 13 number
7
+ #
8
+ def initialize(isbn)
9
+ @isbn = isbn&.delete("-")&.sub(/^ISBN\s/, "")
10
+ end
11
+
12
+ def parse
13
+ convert_to13
14
+ end
15
+
16
+ def check?
17
+ case @isbn
18
+ when /^\d{9}[\dX]$/i then check10?
19
+ when /^\d{13}$/ then check13?
20
+ else false
21
+ end
22
+ end
23
+
24
+ def check10?
25
+ @isbn[9] == calc_check_digit10
26
+ end
27
+
28
+ def check13?
29
+ @isbn[12] == calc_check_digit13
30
+ end
31
+
32
+ def calc_check_digit10
33
+ sum = 0
34
+ @isbn[..-2].chars.each_with_index do |c, i|
35
+ sum += c.to_i * (10 - i)
36
+ end
37
+ chk = (11 - sum % 11) % 11
38
+ chk == 10 ? "X" : chk.to_s
39
+ end
40
+
41
+ def calc_check_digit13
42
+ sum = 0
43
+ @isbn[..-2].chars.each_with_index do |c, i|
44
+ sum += c.to_i * (i.even? ? 1 : 3)
45
+ end
46
+ ((10 - sum % 10) % 10).to_s
47
+ end
48
+
49
+ def convert_to13
50
+ return unless check?
51
+
52
+ return @isbn if @isbn.size == 13
53
+
54
+ @isbn = "978#{@isbn}"
55
+ @isbn[12] = calc_check_digit13
56
+ @isbn
57
+ end
58
+ end
59
+ end
@@ -7,10 +7,16 @@ module RelatonIsbn
7
7
 
8
8
  ENDPOINT = "http://openlibrary.org/api/volumes/brief/isbn/".freeze
9
9
 
10
- def get(ref, _date = nil, _opts = {})
10
+ def get(ref, _date = nil, _opts = {}) # rubocop:disable Metrics/MethodLength
11
11
  Util.warn "(#{ref}) Fetching from OpenLibrary ..."
12
12
 
13
- resp = request_api ref
13
+ isbn = Isbn.new(ref).parse
14
+ unless isbn
15
+ Util.warn "(#{ref}) Incorrect ISBN."
16
+ return
17
+ end
18
+
19
+ resp = request_api isbn
14
20
  unless resp
15
21
  Util.warn "(#{ref}) Not found."
16
22
  return
@@ -21,10 +27,7 @@ module RelatonIsbn
21
27
  bib
22
28
  end
23
29
 
24
- def request_api(ref)
25
- /ISBN\s*(?<isbn>\w+)/ =~ ref
26
- return unless isbn
27
-
30
+ def request_api(isbn)
28
31
  uri = URI "#{ENDPOINT}#{isbn}.json"
29
32
  response = Net::HTTP.get_response uri
30
33
  return unless response.is_a? Net::HTTPSuccess
@@ -1,3 +1,3 @@
1
1
  module RelatonIsbn
2
- VERSION = "1.17.0".freeze
2
+ VERSION = "1.17.1".freeze
3
3
  end
data/lib/relaton_isbn.rb CHANGED
@@ -3,6 +3,7 @@ require "relaton_bib"
3
3
  require_relative "relaton_isbn/version"
4
4
  require_relative "relaton_isbn/config"
5
5
  require_relative "relaton_isbn/util"
6
+ require_relative "relaton_isbn/isbn"
6
7
  require_relative "relaton_isbn/parser"
7
8
  require_relative "relaton_isbn/open_library"
8
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-isbn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.17.0
4
+ version: 1.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-16 00:00:00.000000000 Z
11
+ date: 2023-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: relaton-bib
@@ -39,6 +39,7 @@ files:
39
39
  - Rakefile
40
40
  - lib/relaton_isbn.rb
41
41
  - lib/relaton_isbn/config.rb
42
+ - lib/relaton_isbn/isbn.rb
42
43
  - lib/relaton_isbn/open_library.rb
43
44
  - lib/relaton_isbn/parser.rb
44
45
  - lib/relaton_isbn/processor.rb