alexandrite 0.1.0.pre.alpha → 0.1.2.pre.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: daf4377dc5fb6c1b1b122236cdd76e760863a575bd6e1bd177d20d98aab92508
4
- data.tar.gz: ce9bfcf98c1142b5e539b52189962779c2286705a9c361ba3da7bfa76b80ac81
3
+ metadata.gz: 1904340a4469d9f82f1d42d16a58cdd7a63e0a23711ed040d20c63648164d4fc
4
+ data.tar.gz: a6e306d3416403661661ed1bee823d43bd332f446cb1e1ee61e1529c8f5362aa
5
5
  SHA512:
6
- metadata.gz: 370748c92b5a9db2b9942fa8b8d779a2324a3a3c95c3114059d708346ff4ef6ee3afa8405a6f5aee58312bfef674e93b14b120d0ed4ef5a6c1cfdbaa4c55bc92
7
- data.tar.gz: b04df9e530984ad698e71ea0057a60911480ad5c423d83ecea46413b8eea13af96e7db0fae3cb7d6c96e538b7d50e4457bd63ca04d6176ac5e8282f18f221a31
6
+ metadata.gz: 40f3d5f4f06ce58a3f99156f4a26e399cb8b3e1c852a34de923947aa1b5cad75f39a2d75cc39ed86fa2a765292318ec890e5acc201fa922d861b91618e4d1d96
7
+ data.tar.gz: 0fc0a88be83ab17e5753cd389dac7de71ecffaa6915e59d7047a5dd4dc547b84a7fb0a2231be8e22fd911cf162945ad2fa16dafa531af82b0891f6539e22d3eb
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alexandrite
4
- VERSION = '0.1.0-alpha'
4
+ VERSION = '0.1.2-alpha'
5
5
  end
data/lib/alexandrite.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'alexandrite_book'
3
+ require_relative 'alexandrite_book_data'
4
4
  require_relative 'alexandrite_google'
5
5
  require_relative 'alexandrite_oclc'
6
6
  require_relative 'error_type/errors'
@@ -8,37 +8,38 @@ require_relative 'error_type/errors'
8
8
  # Main gem methods for creating books from ISBN
9
9
  module Alexandrite
10
10
  include ErrorType
11
+ include Alexandrite::BookData
11
12
 
12
13
  # Get and Process data from Google API
13
14
  class Google
15
+ include Alexandrite
14
16
  extend Alexandrite::GoogleAPI
15
17
 
16
18
  attr_reader :result
17
19
 
18
20
  def initialize(key, query)
19
- @result = search_by(key, query)
21
+ @result = search_with(key, query)
20
22
  end
21
23
 
22
- # @return [Alexandrite::Book]
24
+ # @return [Alexandrite::BookData]
23
25
  def self.create_from_google(key, query)
24
- volume_info = get_volume_info(key, query)
25
-
26
- Alexandrite::Book.new(volume_info)
26
+ Alexandrite::BookData.create_data(get_volume_info(key, query))
27
27
  end
28
28
  end
29
29
 
30
30
  # Get and Process data from OCLC API
31
31
  class OCLC
32
+ include Alexandrite
32
33
  extend Alexandrite::OCLCAPI
33
34
 
34
35
  attr_reader :result
35
36
 
36
37
  # return [Alexandrite::OCLC]
37
38
  def initialize(key, query, api: :oclc)
38
- @result = search_by(key, query, api: api)
39
+ @result = search_with(key, query, api: api)
39
40
  end
40
41
 
41
- # @return [Alexandrite::Book]
42
+ # @return [Alexandrite::BookData]
42
43
  def self.create_from_oclc(type, identifier)
43
44
  query = Alexandrite::OCLC.new(type, identifier)
44
45
  response_code = get_response_code(query.result)
@@ -52,20 +53,20 @@ module Alexandrite
52
53
  oclc: Alexandrite::OCLC
53
54
  }.freeze
54
55
 
55
- def search_by(key, query, api: :google)
56
+ def search_with(key, query, api: :google)
56
57
  API[api].search_by(key, query)
57
58
  end
58
59
 
59
60
  def create_book(key, query)
60
61
  book = API[:google].create_from_google(key, query)
61
- return API[:oclc].create_from_oclc(key, query) if book.error_message
62
+ return API[:oclc].create_from_oclc(key, query) if book[:error_message]
62
63
 
63
64
  book
64
65
  end
65
66
 
66
67
  # @param key [String]
67
68
  # @param data [Array<String>]
68
- # @return [Array<Alexandrite::Books]
69
+ # @return [Array<Alexandrite::BookData]
69
70
  def bulk_create(key, data)
70
71
  bookshelf = []
71
72
  data.each do |query|
@@ -75,13 +76,4 @@ module Alexandrite
75
76
 
76
77
  bookshelf
77
78
  end
78
-
79
- private
80
-
81
- def get_volume_info(key, query)
82
- query = search_by(key, query)
83
- return query[:books].first['volumeInfo'] unless query[:error_message]
84
-
85
- query
86
- end
87
79
  end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'alexandrite'
4
+ require 'nokogiri'
5
+
6
+ module Alexandrite
7
+ # Create a book objects
8
+ module BookData
9
+ # @param volume_info [Hash]
10
+ # @return [Hash]
11
+ def self.create_data(volume_info)
12
+ data = add_basic_values(volume_info)
13
+ data[:data_source] = add_origin(volume_info)
14
+ data = add_industry_identifiers(volume_info, data)
15
+ data[:error_message] = volume_info[:error_message]
16
+
17
+ data
18
+ end
19
+
20
+ def self.add_basic_values(volume_info)
21
+ {
22
+ title: volume_info['title'],
23
+ authors: volume_info['authors'],
24
+ published_date: volume_info['publisher'],
25
+ page_count: volume_info['pageCount'],
26
+ description: volume_info['description'],
27
+ categories: volume_info['categories'],
28
+ language: volume_info['language']
29
+
30
+ }
31
+ end
32
+
33
+ # @param [Hash] volume_info
34
+ # @return [String]
35
+ def self.add_origin(volume_info) = volume_info['origin'] || 'Google API'
36
+
37
+ # @param [Hash] volume_info
38
+ # @param [Hash] data
39
+ # @return [NilClass]
40
+ def self.add_industry_identifiers(volume_info, data)
41
+ return data unless volume_info[:error_message].nil?
42
+
43
+ if data[:data_source] == 'OCLC API'
44
+ data[:isbn10] = volume_info['ISBN_10']
45
+ data[:isbn13] = volume_info['ISBN_13']
46
+ return data
47
+ end
48
+ begin
49
+ volume_info['industryIdentifiers'].each do |identifier|
50
+ data[:isbn10] = identifier['identifier'] if identifier['type'] == 'ISBN_10'
51
+ data[:isbn13] = identifier['identifier'] if identifier['type'] == 'ISBN_13'
52
+ end
53
+ rescue NoMethodError
54
+ data
55
+ end
56
+ data
57
+ end
58
+ end
59
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'alexandrite'
4
- require_relative 'alexandrite_book'
4
+ require_relative 'alexandrite_book_data'
5
5
  require_relative 'helpers/file_handler'
6
6
 
7
7
  # Create, Read, Update and Delete books in a book collection
@@ -62,6 +62,13 @@ module Alexandrite
62
62
 
63
63
  private
64
64
 
65
+ def get_volume_info(key, query)
66
+ query = search_by(key, query)
67
+ return query[:books].first['volumeInfo'] unless query[:error_message]
68
+
69
+ query
70
+ end
71
+
65
72
  def zero_results?(response_body) = response_body['totalItems'].zero?
66
73
 
67
74
  def process_response(response)
@@ -17,6 +17,7 @@ module Alexandrite
17
17
  include Nokogiri
18
18
  include Alexandrite::Helpers::Format
19
19
  include Alexandrite::Helpers::Validation
20
+ include Alexandrite::BookData
20
21
 
21
22
  BASE_URL = 'http://classify.oclc.org/classify2/Classify?'
22
23
 
@@ -30,28 +31,37 @@ module Alexandrite
30
31
  Nokogiri::XML(conn.get(search).body)
31
32
  end
32
33
 
34
+ def extract_value(attribute)
35
+ return nil unless attribute.respond_to?(:value)
36
+
37
+ attribute.value
38
+ end
39
+
40
+ def extract_nokogiri_data(document, children, attribute)
41
+ extract_value(document.css(children).attribute(attribute))
42
+ end
43
+
33
44
  def get_book_data(result)
34
45
  isbn = result.css('input').children.text
35
46
  isbn_type = define_isbn_type(isbn)
36
47
  {
37
48
  'origin' => 'OCLC API',
38
- 'title' => result.css('work').attribute('title').value,
39
- 'authors' => result.css('work').attribute('author').value,
40
- 'ddc' => result.css('mostPopular').attribute('nsfa').value,
49
+ 'title' => extract_nokogiri_data(result, 'work', 'title'),
50
+ 'authors' => extract_nokogiri_data(result, 'work', 'author'),
51
+ 'ddc' => extract_nokogiri_data(result, 'mostPopular', 'nsfa'),
41
52
  isbn_type => isbn
42
53
  }
43
54
  end
44
55
 
45
56
  def recommend_classification(key, query, length = 5, mode: 'ddc')
46
57
  results = search_by(key, query)
47
-
48
58
  response_cases(results, length, mode)
49
59
  end
50
60
 
51
61
  private
52
62
 
53
63
  # @return [Integer]
54
- def get_response_code(result) = result.css('response').attribute('code').value.to_i
64
+ def get_response_code(result) = extract_nokogiri_data(result, 'response', 'code').to_i
55
65
 
56
66
  def get_owis(result, length)
57
67
  owis = []
@@ -83,22 +93,13 @@ module Alexandrite
83
93
  end
84
94
 
85
95
  def create_new_book(query)
86
- volume_info = get_book_data(query.result)
87
-
88
- Alexandrite::Book.new(volume_info)
89
- end
90
-
91
- def retry_create_new_book(query)
92
- owi = get_owis(query.result, 1)[0]
93
- new_query = Alexandrite::OCLC.new('owi', owi)
94
- volume_info = get_book_data(new_query.result)
95
- Alexandrite::Book.new(volume_info)
96
+ Alexandrite::BookData.create_data(get_book_data(query.result))
96
97
  end
97
98
 
98
- def handle_error_creating_book(query)
99
- raise ErrorType::DataNotFound, response_cases(query.result, 0, 'none')
99
+ def handle_error_creating_book
100
+ raise ErrorType::DataNotFound, yield
100
101
  rescue StandardError => e
101
- Alexandrite::Book.new({ :error_message => e.message, 'origin' => 'OCLC API' })
102
+ Alexandrite::BookData.create_data({ :error_message => e.message, 'origin' => 'OCLC API' })
102
103
  end
103
104
 
104
105
  # @param [Nokogiri::XML] result
@@ -130,9 +131,9 @@ module Alexandrite
130
131
  when 0, 2
131
132
  create_new_book(query)
132
133
  when 4
133
- retry_create_new_book(query)
134
+ handle_error_creating_book { 'Not found exact item. Check coincidences' }
134
135
  else
135
- handle_error_creating_book(query)
136
+ handle_error_creating_book { response_cases(query.result, 0, 'none') }
136
137
  end
137
138
  end
138
139
  end
@@ -16,4 +16,4 @@ module Alexandrite
16
16
  end
17
17
  end
18
18
  end
19
- end
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alexandrite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.alpha
4
+ version: 0.1.2.pre.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. P. Pérez-Tejada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-22 00:00:00.000000000 Z
11
+ date: 2023-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -156,7 +156,7 @@ files:
156
156
  - data.dump
157
157
  - lib/alexandrite.rb
158
158
  - lib/alexandrite/version.rb
159
- - lib/alexandrite_book.rb
159
+ - lib/alexandrite_book_data.rb
160
160
  - lib/alexandrite_bookshelf.rb
161
161
  - lib/alexandrite_google.rb
162
162
  - lib/alexandrite_oclc.rb
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  - !ruby/object:Gem::Version
190
190
  version: 1.3.1
191
191
  requirements: []
192
- rubygems_version: 3.3.3
192
+ rubygems_version: 3.4.5
193
193
  signing_key:
194
194
  specification_version: 4
195
195
  summary: Tools for fetching books data from Google and OCLC
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'alexandrite'
4
- require 'nokogiri'
5
-
6
- module Alexandrite
7
- # Create a book objects
8
- class Book
9
- attr_accessor :title, :authors, :publisher, :published_date, :page_count, :description, :categories, :language,
10
- :country, :isbn10, :isbn13, :error_message, :ddc, :lcc, :suggested_classifications
11
-
12
- # @param volume_info [Hash]
13
- # @return [Alexandrite::Books]
14
- def initialize(volume_info)
15
- add_origin(volume_info)
16
- add_basic_values(volume_info)
17
- add_industry_identifiers(volume_info, @origin)
18
- @error_message = volume_info[:error_message]
19
- end
20
-
21
- def self.collection = @@collection
22
-
23
- def self.add_to_collection(book)
24
- @@collection.push(book)
25
- end
26
-
27
- # @param [String] ddc_code
28
- # @return [NilClass]
29
- def add_ddc(ddc_code) = @ddc = ddc_code
30
-
31
- # @param [String] lcc_code
32
- # @return [NilClass]
33
- def add_lcc(lcc_code) = @lcc = lcc_code
34
-
35
- # @param [Array<String>] suggestions
36
- # @return [NilClass]
37
- def add_classification_suggested(suggestions) = @suggested_classifications = suggestions
38
-
39
- private
40
-
41
- def add_basic_values(volume_info)
42
- @title = volume_info['title']
43
- @authors = volume_info['authors']
44
- @publisher = volume_info['publisher']
45
- @published_date = volume_info['publishedDate']
46
- @page_count = volume_info['pageCount']
47
- @description = volume_info['description']
48
- @categories = volume_info['categories']
49
- @language = volume_info['language']
50
- end
51
-
52
- # @param [Hash] volume_info
53
- # @return [String]
54
- def add_origin(volume_info) = @origin = volume_info['origin'] || 'Google API'
55
-
56
- # @param [Hash] volume_info
57
- # @param [String] origin
58
- # @return [NilClass]
59
- def add_industry_identifiers(volume_info, origin)
60
- return nil unless volume_info[:error_message].nil?
61
-
62
- if origin == 'OCLC API'
63
- @isbn10 = volume_info['ISBN_10']
64
- @isbn13 = volume_info['ISBN_13']
65
- return
66
- end
67
-
68
- volume_info['industryIdentifiers'].each do |identifier|
69
- @isbn10 = identifier['identifier'] if identifier['type'] == 'ISBN_10'
70
- @isbn13 = identifier['identifier'] if identifier['type'] == 'ISBN_13'
71
- end
72
- end
73
- end
74
- end