alexandria-book-collection-manager 0.7.3 → 0.7.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +9 -0
  3. data/.github/workflows/ruby.yml +77 -0
  4. data/.gitignore +4 -1
  5. data/.rubocop.yml +86 -36
  6. data/.rubocop_todo.yml +58 -161
  7. data/.simplecov +5 -2
  8. data/CHANGELOG.md +56 -2
  9. data/Gemfile +4 -3
  10. data/INSTALL.md +23 -11
  11. data/README.md +52 -41
  12. data/Rakefile +78 -75
  13. data/alexandria-book-collection-manager.gemspec +50 -44
  14. data/bin/alexandria +12 -22
  15. data/doc/FAQ +1 -2
  16. data/doc/dependency_decisions.yml +27 -8
  17. data/lib/alexandria.rb +25 -23
  18. data/lib/alexandria/about.rb +50 -50
  19. data/lib/alexandria/book_providers.rb +86 -91
  20. data/lib/alexandria/book_providers/adlibris.rb +37 -74
  21. data/lib/alexandria/book_providers/amazon_aws.rb +94 -100
  22. data/lib/alexandria/book_providers/amazon_ecs_util.rb +289 -324
  23. data/lib/alexandria/book_providers/barnes_and_noble.rb +42 -42
  24. data/lib/alexandria/book_providers/douban.rb +25 -41
  25. data/lib/alexandria/book_providers/proxis.rb +34 -29
  26. data/lib/alexandria/book_providers/pseudomarc.rb +77 -85
  27. data/lib/alexandria/book_providers/siciliano.rb +60 -64
  28. data/lib/alexandria/book_providers/thalia_provider.rb +161 -0
  29. data/lib/alexandria/book_providers/web.rb +5 -5
  30. data/lib/alexandria/book_providers/worldcat.rb +66 -95
  31. data/lib/alexandria/book_providers/z3950.rb +153 -169
  32. data/lib/alexandria/config.rb +1 -1
  33. data/lib/alexandria/console.rb +3 -3
  34. data/lib/alexandria/default_preferences.rb +37 -0
  35. data/lib/alexandria/execution_queue.rb +13 -12
  36. data/lib/alexandria/export_format.rb +8 -8
  37. data/lib/alexandria/export_library.rb +128 -127
  38. data/lib/alexandria/import_library.rb +102 -126
  39. data/lib/alexandria/import_library_csv.rb +41 -41
  40. data/lib/alexandria/library_collection.rb +6 -5
  41. data/lib/alexandria/library_sort_order.rb +4 -2
  42. data/lib/alexandria/library_store.rb +39 -28
  43. data/lib/alexandria/logging.rb +10 -14
  44. data/lib/alexandria/models/book.rb +5 -4
  45. data/lib/alexandria/models/library.rb +63 -53
  46. data/lib/alexandria/net.rb +5 -6
  47. data/lib/alexandria/preferences.rb +66 -63
  48. data/lib/alexandria/scanners.rb +2 -2
  49. data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +17 -17
  50. data/lib/alexandria/scanners/keyboard.rb +8 -8
  51. data/lib/alexandria/smart_library.rb +110 -112
  52. data/lib/alexandria/ui.rb +15 -15
  53. data/lib/alexandria/ui/{dialogs/about_dialog.rb → about_dialog.rb} +2 -2
  54. data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +108 -109
  55. data/lib/alexandria/ui/alert_dialog.rb +66 -0
  56. data/lib/alexandria/ui/{dialogs/bad_isbns_dialog.rb → bad_isbns_dialog.rb} +13 -9
  57. data/lib/alexandria/ui/{dialogs/barcode_animation.rb → barcode_animation.rb} +16 -15
  58. data/lib/alexandria/ui/{dialogs/book_properties_dialog.rb → book_properties_dialog.rb} +25 -38
  59. data/lib/alexandria/ui/{dialogs/book_properties_dialog_base.rb → book_properties_dialog_base.rb} +64 -157
  60. data/lib/alexandria/ui/builder_base.rb +1 -1
  61. data/lib/alexandria/ui/calendar_popup.rb +58 -0
  62. data/lib/alexandria/ui/callbacks.rb +187 -155
  63. data/lib/alexandria/ui/completion_models.rb +8 -22
  64. data/lib/alexandria/ui/confirm_erase_dialog.rb +33 -0
  65. data/lib/alexandria/ui/conflict_while_copying_dialog.rb +34 -0
  66. data/lib/alexandria/ui/dndable.rb +7 -7
  67. data/lib/alexandria/ui/error_dialog.rb +25 -0
  68. data/lib/alexandria/ui/{dialogs/export_dialog.rb → export_dialog.rb} +37 -58
  69. data/lib/alexandria/ui/icons.rb +38 -43
  70. data/lib/alexandria/ui/iconview.rb +12 -10
  71. data/lib/alexandria/ui/iconview_tooltips.rb +41 -54
  72. data/lib/alexandria/ui/import_dialog.rb +157 -0
  73. data/lib/alexandria/ui/init.rb +30 -41
  74. data/lib/alexandria/ui/{dialogs/keep_bad_isbn_dialog.rb → keep_bad_isbn_dialog.rb} +9 -6
  75. data/lib/alexandria/ui/libraries_combo.rb +15 -14
  76. data/lib/alexandria/ui/listview.rb +69 -67
  77. data/lib/alexandria/ui/main_app.rb +24 -26
  78. data/lib/alexandria/ui/misc_dialogs.rb +10 -0
  79. data/lib/alexandria/ui/multi_drag_treeview.rb +8 -9
  80. data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +113 -114
  81. data/lib/alexandria/ui/{dialogs/new_book_dialog_manual.rb → new_book_dialog_manual.rb} +22 -19
  82. data/lib/alexandria/ui/new_provider_dialog.rb +100 -0
  83. data/lib/alexandria/ui/new_smart_library_dialog.rb +74 -0
  84. data/lib/alexandria/ui/preferences_dialog.rb +313 -0
  85. data/lib/alexandria/ui/provider_preferences_base_dialog.rb +95 -0
  86. data/lib/alexandria/ui/provider_preferences_dialog.rb +35 -0
  87. data/lib/alexandria/ui/{dialogs/misc_dialogs.rb → really_delete_dialog.rb} +7 -28
  88. data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +53 -48
  89. data/lib/alexandria/ui/skip_entry_dialog.rb +33 -0
  90. data/lib/alexandria/ui/smart_library_properties_dialog.rb +60 -0
  91. data/lib/alexandria/ui/smart_library_properties_dialog_base.rb +242 -0
  92. data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
  93. data/lib/alexandria/ui/sound.rb +11 -13
  94. data/lib/alexandria/ui/ui_manager.rb +216 -200
  95. data/lib/alexandria/version.rb +4 -19
  96. data/lib/alexandria/web_themes.rb +21 -21
  97. data/po/Makefile +2 -2
  98. data/po/cs.po +992 -875
  99. data/po/cy.po +961 -874
  100. data/po/de.po +990 -865
  101. data/po/el.po +989 -865
  102. data/po/es.po +985 -861
  103. data/po/fr.po +987 -867
  104. data/po/ga.po +908 -820
  105. data/po/gl.po +980 -860
  106. data/po/it.po +986 -864
  107. data/po/ja.po +969 -849
  108. data/po/mk.po +984 -860
  109. data/po/nb.po +979 -859
  110. data/po/nl.po +983 -860
  111. data/po/pl.po +1018 -971
  112. data/po/pt.po +988 -857
  113. data/po/pt_BR.po +983 -863
  114. data/po/ru.po +994 -871
  115. data/po/sk.po +989 -867
  116. data/po/sv.po +976 -856
  117. data/po/uk.po +972 -858
  118. data/po/zh_TW.po +974 -854
  119. data/schemas/alexandria.schemas +24 -2
  120. data/share/alexandria/glade/acquire_dialog__builder.glade +1 -1
  121. data/share/alexandria/glade/book_properties_dialog__builder.glade +1 -1
  122. data/share/alexandria/glade/main_app__builder.glade +6 -21
  123. data/share/alexandria/glade/new_book_dialog__builder.glade +1 -1
  124. data/share/alexandria/glade/preferences_dialog__builder.glade +1 -1
  125. data/share/gnome/help/alexandria/C/introduction.xml +0 -4
  126. data/share/gnome/help/alexandria/C/searching.xml +1 -1
  127. data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
  128. data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
  129. data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
  130. data/share/gnome/help/alexandria/ja/introduction.xml +0 -4
  131. data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
  132. data/spec/alexandria/book_providers/thalia_provider_spec.rb +119 -0
  133. data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
  134. data/spec/alexandria/book_providers_spec.rb +62 -156
  135. data/spec/alexandria/book_spec.rb +12 -10
  136. data/spec/alexandria/console_spec.rb +6 -11
  137. data/spec/alexandria/export_library_spec.rb +47 -58
  138. data/spec/alexandria/library_spec.rb +121 -109
  139. data/spec/alexandria/library_store_spec.rb +8 -8
  140. data/spec/alexandria/preferences_spec.rb +44 -17
  141. data/spec/alexandria/scanners/cue_cat_spec.rb +52 -0
  142. data/spec/alexandria/smart_library_spec.rb +15 -15
  143. data/spec/alexandria/ui/about_dialog_spec.rb +14 -0
  144. data/spec/alexandria/ui/acquire_dialog_spec.rb +14 -0
  145. data/spec/alexandria/ui/alert_dialog_spec.rb +16 -0
  146. data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +14 -0
  147. data/spec/alexandria/ui/book_properties_dialog_spec.rb +59 -0
  148. data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +14 -0
  149. data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +16 -0
  150. data/spec/alexandria/ui/error_dialog_spec.rb +14 -0
  151. data/spec/alexandria/ui/export_dialog_spec.rb +36 -0
  152. data/spec/alexandria/ui/icons_spec.rb +26 -0
  153. data/spec/alexandria/ui/iconview_spec.rb +7 -21
  154. data/spec/alexandria/ui/import_dialog_spec.rb +46 -0
  155. data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +17 -0
  156. data/spec/alexandria/ui/main_app_spec.rb +7 -34
  157. data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +51 -0
  158. data/spec/alexandria/ui/{dialogs/new_book_dialog_spec.rb → new_book_dialog_spec.rb} +4 -4
  159. data/spec/alexandria/ui/new_provider_dialog_spec.rb +30 -0
  160. data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +39 -0
  161. data/spec/alexandria/ui/preferences_dialog_spec.rb +14 -0
  162. data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +34 -0
  163. data/spec/alexandria/ui/really_delete_dialog_spec.rb +16 -0
  164. data/spec/alexandria/ui/sidepane_manager_spec.rb +15 -0
  165. data/spec/alexandria/ui/skip_entry_dialog_spec.rb +14 -0
  166. data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +49 -0
  167. data/spec/alexandria/ui/sound_spec.rb +2 -2
  168. data/spec/alexandria/ui/ui_manager_spec.rb +44 -20
  169. data/spec/end_to_end/basic_run_spec.rb +21 -38
  170. data/spec/fixtures/cover.jpg +0 -0
  171. data/spec/spec_helper.rb +54 -10
  172. data/tasks/setup.rb +2 -2
  173. data/tasks/spec.rake +11 -11
  174. data/util/rake/fileinstall.rb +38 -35
  175. data/util/rake/gettextgenerate.rb +7 -7
  176. data/util/rake/omfgenerate.rb +7 -7
  177. metadata +158 -45
  178. data/dogtail/basic_run_test.py +0 -9
  179. data/lib/alexandria/book_providers/renaud.rb +0 -155
  180. data/lib/alexandria/book_providers/thalia.rb +0 -198
  181. data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -63
  182. data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -176
  183. data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -62
  184. data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -563
  185. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -61
  186. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog_base.rb +0 -423
  187. data/spec/alexandria/scanners/cuecat_spec.rb +0 -67
  188. data/spec/alexandria/ui/dialogs_spec.rb +0 -162
  189. data/spec/alexandria/ui/sidepane_spec.rb +0 -29
  190. data/spec/alexandria/ui/ui_utilities_spec.rb +0 -62
  191. data/spec/alexandria/utilities_spec.rb +0 -52
  192. data/tasks/dogtail.rake +0 -6
@@ -1,95 +1,81 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright (C) 2004-2006 Laurent Sansonetti
4
- # Copyright (C) 2008 Cathal Mc Ginley
5
- # Copyright (C) 2014, 2016 Matijs van Zuijlen
3
+ # This file is part of Alexandria.
6
4
  #
7
- # Alexandria is free software; you can redistribute it and/or
8
- # modify it under the terms of the GNU General Public License as
9
- # published by the Free Software Foundation; either version 2 of the
10
- # License, or (at your option) any later version.
11
- #
12
- # Alexandria is distributed in the hope that it will be useful,
13
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
- # General Public License for more details.
16
- #
17
- # You should have received a copy of the GNU General Public
18
- # License along with Alexandria; see the file COPYING. If not,
19
- # write to the Free Software Foundation, Inc., 51 Franklin Street,
20
- # Fifth Floor, Boston, MA 02110-1301 USA.
5
+ # See the file README.md for authorship and licensing information.
21
6
 
22
7
  # http://en.wikipedia.org/wiki/Amazon
23
8
 
24
- require 'hpricot'
25
- require 'alexandria/book_providers/amazon_ecs_util'
9
+ require "hpricot"
10
+ require "alexandria/book_providers/amazon_ecs_util"
26
11
 
27
12
  module Alexandria
28
13
  class BookProviders
29
14
  class AmazonProvider < GenericProvider
30
15
  include Logging
31
16
  include GetText
32
- GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset: 'UTF-8')
17
+ GetText.bindtextdomain(Alexandria::TEXTDOMAIN, charset: "UTF-8")
33
18
 
34
19
  # CACHE_DIR = File.join(Alexandria::Library::DIR, '.amazon_cache')
35
20
 
36
- LOCALES = ['ca', 'de', 'fr', 'jp', 'uk', 'us'].freeze
21
+ LOCALES = ["ca", "de", "fr", "jp", "uk", "us"].freeze
37
22
 
38
23
  def initialize
39
- super('Amazon', 'Amazon')
24
+ super("Amazon", "Amazon")
40
25
  # prefs.add("enabled", _("Enabled"), true, [true,false])
41
- prefs.add('locale', _('Locale'), 'us', AmazonProvider::LOCALES)
42
- prefs.add('dev_token', _('Access key ID'), '')
43
- prefs.add('secret_key', _('Secret access key'), '')
44
- prefs.add('associate_tag', _('Associate Tag'), '')
26
+ prefs.add("locale", _("Locale"), "us", AmazonProvider::LOCALES)
27
+ prefs.add("dev_token", _("Access key ID"), "")
28
+ prefs.add("secret_key", _("Secret access key"), "")
29
+ prefs.add("associate_tag", _("Associate Tag"), "")
45
30
 
46
31
  prefs.read
47
- token = prefs.variable_named('dev_token')
32
+ token = prefs.variable_named("dev_token")
48
33
  # kill old (shorter) tokens, or previously distributed Access Key Id (see #26250)
49
34
 
50
35
  if token
51
36
  token.new_value = token.value.strip if token.value != token.value.strip
37
+ if (token.value.size != 20) || (token.value == "0J356Z09CN88KB743582")
38
+ token.new_value = ""
39
+ end
52
40
  end
53
- token.new_value = '' if token && ((token.value.size != 20) || (token.value == '0J356Z09CN88KB743582'))
54
41
 
55
- secret = prefs.variable_named('secret_key')
56
- if secret
57
- secret.new_value = secret.value.strip if secret.value != secret.value.strip
42
+ secret = prefs.variable_named("secret_key")
43
+ if secret && (secret.value != secret.value.strip)
44
+ secret.new_value = secret.value.strip
58
45
  end
59
46
 
60
- associate = prefs.variable_named('associate_tag')
61
- if associate
62
- associate.new_value = 'rubyalexa-20' if associate.value.strip.empty?
63
- associate.new_value = associate.value.strip if associate.value != associate.value.strip
47
+ associate = prefs.variable_named("associate_tag") or return
48
+
49
+ associate.new_value = "rubyalexa-20" if associate.value.strip.empty?
50
+ if associate.value != associate.value.strip
51
+ associate.new_value = associate.value.strip
64
52
  end
65
53
  end
66
54
 
67
55
  def search(criterion, type)
68
56
  prefs.read
69
57
 
70
- if prefs['secret_key'].empty?
71
- raise(Amazon::RequestError,
72
- 'Secret Access Key required for Authentication:' \
73
- ' you must sign up for your own Amazon AWS account')
58
+ if prefs["secret_key"].empty?
59
+ raise(Amazon::RequestError, _("Provide secret key for your Amazon AWS account."))
74
60
  end
75
61
 
76
62
  if (config = Alexandria::Preferences.instance.http_proxy_config)
77
63
  host, port, user, pass = config
78
- url = 'http://'
79
- url += user + ':' + pass + '@' if user && pass
80
- url += host + ':' + port.to_s
81
- ENV['http_proxy'] = url
64
+ url = "http://"
65
+ url += user + ":" + pass + "@" if user && pass
66
+ url += host + ":" + port.to_s
67
+ ENV["http_proxy"] = url
82
68
  end
83
69
 
84
- access_key_id = prefs['dev_token']
70
+ access_key_id = prefs["dev_token"]
85
71
 
86
72
  Amazon::Ecs.options = { aWS_access_key_id: access_key_id,
87
- associateTag: prefs['associate_tag'] }
88
- Amazon::Ecs.secret_access_key = prefs['secret_key']
73
+ associateTag: prefs["associate_tag"] }
74
+ Amazon::Ecs.secret_access_key = prefs["secret_key"]
89
75
  # #req.cache = Amazon::Search::Cache.new(CACHE_DIR)
90
76
  locales = AmazonProvider::LOCALES.dup
91
- locales.delete prefs['locale']
92
- locales.unshift prefs['locale']
77
+ locales.delete prefs["locale"]
78
+ locales.unshift prefs["locale"]
93
79
  locales.reverse!
94
80
 
95
81
  begin
@@ -99,8 +85,10 @@ module Alexandria
99
85
  when SEARCH_BY_ISBN
100
86
  criterion = Library.canonicalise_isbn(criterion)
101
87
  # This isn't ideal : I'd like to do an ISBN/EAN-specific search
102
- res = Amazon::Ecs.item_search(criterion, response_group: 'ItemAttributes,Images',
103
- country: request_locale)
88
+ res = Amazon::Ecs.item_search(criterion,
89
+ response_group: "ItemAttributes,Images",
90
+ country: request_locale)
91
+
104
92
  res.items.each do |item|
105
93
  products << item
106
94
  end
@@ -120,27 +108,27 @@ module Alexandria
120
108
  # result with different ISBNs
121
109
 
122
110
  if products.length > 1
123
- log.warn {
111
+ log.warn do
124
112
  "ISBN search at Amazon[#{request_locale}] got #{products.length} results;" \
125
- ' returning the first result only'
126
- }
113
+ " returning the first result only"
114
+ end
127
115
  end
128
116
 
129
117
  when SEARCH_BY_TITLE
130
118
  res = Amazon::Ecs.item_search(criterion,
131
- response_group: 'ItemAttributes,Images',
119
+ response_group: "ItemAttributes,Images",
132
120
  country: request_locale)
133
121
 
134
122
  res.items.each do |item|
135
- products << item if item.get('itemattributes/title') =~ /#{criterion}/i
123
+ products << item if /#{criterion}/i.match?(item.get("itemattributes/title"))
136
124
  end
137
125
  # #req.keyword_search(criterion) do |product|
138
126
 
139
127
  when SEARCH_BY_AUTHORS
140
128
  criterion = "author:#{criterion}"
141
129
  res = Amazon::Ecs.item_search(criterion,
142
- response_group: 'ItemAttributes,Images',
143
- country: request_locale, type: 'Power')
130
+ response_group: "ItemAttributes,Images",
131
+ country: request_locale, type: "Power")
144
132
  res.items.each do |item|
145
133
  products << item
146
134
  end
@@ -148,7 +136,7 @@ module Alexandria
148
136
 
149
137
  when SEARCH_BY_KEYWORD
150
138
  res = Amazon::Ecs.item_search(criterion,
151
- response_group: 'ItemAttributes,Images',
139
+ response_group: "ItemAttributes,Images",
152
140
  country: request_locale)
153
141
 
154
142
  res.items.each do |item|
@@ -158,78 +146,63 @@ module Alexandria
158
146
  else
159
147
  raise InvalidSearchTypeError
160
148
  end
161
- raise Amazon::RequestError, 'No products' if products.empty?
149
+ raise Amazon::RequestError, _("No products") if products.empty?
162
150
  # raise NoResultsError if products.empty?
163
- rescue Amazon::RequestError => re
164
- log.debug { "Got Amazon::RequestError at #{request_locale}: #{re}" }
151
+ rescue Amazon::RequestError => ex
152
+ log.debug { "Got Amazon::RequestError at #{request_locale}: #{ex}" }
165
153
  retry unless locales.empty?
166
154
  raise NoResultsError
167
155
  end
168
156
 
169
157
  results = []
170
158
  products.each do |item|
171
- next unless item.get('itemattributes/productgroup') == 'Book'
159
+ next unless item.get("itemattributes/productgroup") == "Book"
172
160
 
173
- atts = item.search_and_convert('itemattributes')
174
- title = normalize(atts.get('title'))
161
+ atts = item.search_and_convert("itemattributes")
162
+ title = normalize(atts.get("title"))
175
163
 
176
- media = normalize(atts.get('binding'))
177
- media = nil if media == 'Unknown Binding'
164
+ media = normalize(atts.get("binding"))
165
+ media = nil if media == "Unknown Binding"
178
166
 
179
- isbn = normalize(atts.get('isbn'))
167
+ isbn = normalize(atts.get("isbn"))
180
168
  isbn = (Library.canonicalise_ean(isbn) if isbn && Library.valid_isbn?(isbn))
181
169
  # hack, extract year by regexp (not Y10K compatible :-)
182
- /([1-9][0-9]{3})/ =~ atts.get('publicationdate')
170
+ /([1-9][0-9]{3})/ =~ atts.get("publicationdate")
183
171
  publishing_year = Regexp.last_match[1] ? Regexp.last_match[1].to_i : nil
184
172
  book = Book.new(title,
185
- atts.get_array('author').map { |x| normalize(x) },
173
+ atts.get_array("author").map { |x| normalize(x) },
186
174
  isbn,
187
- normalize(atts.get('manufacturer')),
175
+ normalize(atts.get("manufacturer")),
188
176
  publishing_year,
189
177
  media)
190
178
 
191
- image_url = item.get('mediumimage/url')
179
+ image_url = item.get("mediumimage/url")
192
180
  log.info { "Found at Amazon[#{request_locale}]: #{book.title}" }
193
181
  results << [book, image_url]
194
182
  end
195
183
  if type == SEARCH_BY_ISBN
196
184
  if results.size == 1
197
- return results.first
185
+ results.first
198
186
  else
199
- log.info { 'Found multiple results for lookup: checking each' }
200
- query_isbn_canon = Library.canonicalise_ean(criterion)
201
- results.each do |rslt|
202
- book = rslt[0]
203
- book_isbn_canon = Library.canonicalise_ean(book.isbn)
204
- return rslt if query_isbn_canon == book_isbn_canon
205
-
206
- log.debug { "rejected possible result #{book}" }
207
- end
208
- # gone through all and no ISBN match, so just return first result
209
- log.info { 'no more results to check. Returning first result, just an approximation' }
210
- return results.first
187
+ exact_match_or_first(criterion, results)
211
188
  end
212
189
  else
213
- return results
190
+ results
214
191
  end
215
192
  end
216
193
 
194
+ LOCALE_URLS = {
195
+ "fr" => "http://www.amazon.fr/exec/obidos/ASIN/%s",
196
+ "uk" => "http://www.amazon.co.uk/exec/obidos/ASIN/%s",
197
+ "de" => "http://www.amazon.de/exec/obidos/ASIN/%s",
198
+ "ca" => "http://www.amazon.ca/exec/obidos/ASIN/%s",
199
+ "jp" => "http://www.amazon.jp/exec/obidos/ASIN/%s",
200
+ "us" => "http://www.amazon.com/exec/obidos/ASIN/%s"
201
+ }.freeze
202
+
217
203
  def url(book)
218
204
  isbn = Library.canonicalise_isbn(book.isbn)
219
- url = case prefs['locale']
220
- when 'fr'
221
- 'http://www.amazon.fr/exec/obidos/ASIN/%s'
222
- when 'uk'
223
- 'http://www.amazon.co.uk/exec/obidos/ASIN/%s'
224
- when 'de'
225
- 'http://www.amazon.de/exec/obidos/ASIN/%s'
226
- when 'ca'
227
- 'http://www.amazon.ca/exec/obidos/ASIN/%s'
228
- when 'jp'
229
- 'http://www.amazon.jp/exec/obidos/ASIN/%s'
230
- when 'us'
231
- 'http://www.amazon.com/exec/obidos/ASIN/%s'
232
- end
205
+ url = LOCALE_URLS.fetch(prefs["locale"])
233
206
  url % isbn
234
207
  rescue StandardError => ex
235
208
  log.warn { "Cannot create url for book #{book}; #{ex.message}" }
@@ -237,9 +210,30 @@ module Alexandria
237
210
  end
238
211
 
239
212
  def normalize(str)
240
- str = str.squeeze(' ').strip unless str.nil?
213
+ str = str.squeeze(" ").strip unless str.nil?
241
214
  str
242
215
  end
216
+
217
+ private
218
+
219
+ def exact_match_or_first(criterion, results)
220
+ log.info { "Found multiple results for lookup: checking for exact isbn match" }
221
+ query_isbn_canon = Library.canonicalise_ean(criterion)
222
+ exact_match = results.find do |book, _|
223
+ book_isbn_canon = Library.canonicalise_ean(book.isbn)
224
+ query_isbn_canon == book_isbn_canon
225
+ end
226
+
227
+ if exact_match
228
+ # gone through all and no ISBN match, so just return first result
229
+ log.info do
230
+ "no more results to check. Returning first result, just an approximation"
231
+ end
232
+ exact_match
233
+ else
234
+ results.first
235
+ end
236
+ end
243
237
  end
244
238
  end
245
239
  end
@@ -1,397 +1,362 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #--
4
- # Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
3
+ # This file is part of Alexandria.
5
4
  #
6
- # Permission is hereby granted, free of charge, to any person obtaining
7
- # a copy of this software and associated documentation files (the
8
- # "Software"), to deal in the Software without restriction, including
9
- # without limitation the rights to use, copy, modify, merge, publish,
10
- # distribute, sublicense, and/or sell copies of the Software, and to
11
- # permit persons to whom the Software is furnished to do so, subject to
12
- # the following conditions:
13
- #
14
- # The above copyright notice and this permission notice shall be
15
- # included in all copies or substantial portions of the Software.
16
- #
17
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
- # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
- # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
- # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
- # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
- #++
25
-
26
- # Modified by Cathal Mc Ginley 2008-02-18
27
- # added Amazon::Ecs.transport - to enable Alexandria's proxy support
28
- # Modified by Cathal Mc Ginley 2008-08-26
29
- # Amazon::Element.get now uses inner_text, not inner_html, fixing #21659
30
- # Modified by Cathal Mc Ginley 2009-08-13
31
- # Added sign_request and hmac_sha256 methods for Authentication support
32
-
33
- require 'net/http'
34
- require 'hpricot'
35
- require 'cgi'
36
-
37
- require 'digest/sha2'
38
-
39
- module Amazon
40
- class RequestError < StandardError; end
41
-
42
- class Ecs
43
- include Alexandria::Logging
44
-
45
- SERVICE_URLS = { us: 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
46
- uk: 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
47
- ca: 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
48
- de: 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
49
- jp: 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
50
- fr: 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService' }.freeze
51
-
52
- @@options = {}
53
- @@debug = false
54
-
55
- @@secret_access_key = ''
56
-
57
- # Default search options
58
- def self.options
59
- @@options
60
- end
5
+ # See the file README.md for authorship and licensing information.
61
6
 
62
- def self.secret_access_key=(key)
63
- @@secret_access_key = key
64
- end
7
+ require "net/http"
8
+ require "hpricot"
9
+ require "cgi"
65
10
 
66
- # Set default search options
67
- def self.options=(opts)
68
- @@options = opts
69
- end
11
+ require "digest/sha2"
70
12
 
71
- # Get debug flag.
72
- def self.debug
73
- @@debug
74
- end
13
+ module Alexandria
14
+ module Amazon
15
+ class RequestError < StandardError; end
75
16
 
76
- # Set debug flag to true or false.
77
- def self.debug=(dbg)
78
- @@debug = dbg
79
- end
17
+ class Ecs
18
+ include Logging
80
19
 
81
- def self.configure(&_proc)
82
- raise ArgumentError, 'Block is required.' unless block_given?
20
+ SERVICE_URLS = {
21
+ us: "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService",
22
+ uk: "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService",
23
+ ca: "http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService",
24
+ de: "http://webservices.amazon.de/onca/xml?Service=AWSECommerceService",
25
+ jp: "http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService",
26
+ fr: "http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService"
27
+ }.freeze
83
28
 
84
- yield @@options
85
- end
29
+ @@options = {}
30
+ @@debug = false
86
31
 
87
- # Search amazon items with search terms. Default search index option is 'Books'.
88
- # For other search type other than keywords, please specify :type => [search type param name].
89
- def self.item_search(terms, opts = {})
90
- opts[:operation] = 'ItemSearch'
91
- opts[:search_index] = opts[:search_index] || 'Books'
92
-
93
- type = opts.delete(:type)
94
- if type
95
- opts[type.to_sym] = terms
96
- else
97
- opts[:keywords] = terms
98
- end
32
+ @@secret_access_key = ""
99
33
 
100
- send_request(opts)
101
- end
34
+ # Default search options
35
+ def self.options
36
+ @@options
37
+ end
102
38
 
103
- # Search an item by ASIN no.
104
- def self.item_lookup(item_id, opts = {})
105
- opts[:operation] = 'ItemLookup'
106
- opts[:item_id] = item_id
39
+ def self.secret_access_key=(key)
40
+ @@secret_access_key = key
41
+ end
107
42
 
108
- send_request(opts)
109
- end
43
+ # Set default search options
44
+ def self.options=(opts)
45
+ @@options = opts
46
+ end
110
47
 
111
- # HACK : copied from book_providers.rb
112
- def self.transport
113
- config = Alexandria::Preferences.instance.http_proxy_config
114
- config ? Net::HTTP.Proxy(*config) : Net::HTTP
115
- end
48
+ # Get debug flag.
49
+ def self.debug
50
+ @@debug
51
+ end
116
52
 
117
- # Generic send request to ECS REST service. You have to specify the :operation parameter.
118
- def self.send_request(opts)
119
- opts = options.merge(opts) if options
120
- request_url = prepare_url(opts)
121
- log.debug { "Request URL: #{request_url}" }
53
+ # Set debug flag to true or false.
54
+ def self.debug=(dbg)
55
+ @@debug = dbg
56
+ end
122
57
 
123
- res = transport.get_response(URI.parse(request_url))
124
- unless res.is_a? Net::HTTPSuccess
125
- raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
58
+ def self.configure(&_proc)
59
+ yield @@options
126
60
  end
127
61
 
128
- Response.new(res.body)
129
- end
62
+ # Search amazon items with search terms. Default search index option is 'Books'.
63
+ # For other search type other than keywords, please specify
64
+ # :type => [search type param name].
65
+ def self.item_search(terms, opts = {})
66
+ opts[:operation] = "ItemSearch"
67
+ opts[:search_index] = opts[:search_index] || "Books"
68
+
69
+ type = opts.delete(:type)
70
+ if type
71
+ opts[type.to_sym] = terms
72
+ else
73
+ opts[:keywords] = terms
74
+ end
130
75
 
131
- # Response object returned after a REST call to Amazon service.
132
- class Response
133
- # XML input is in string format
134
- def initialize(xml)
135
- @doc = Hpricot(xml)
76
+ send_request(opts)
136
77
  end
137
78
 
138
- # Return Hpricot object.
139
- attr_reader :doc
79
+ # Search an item by ASIN no.
80
+ def self.item_lookup(item_id, opts = {})
81
+ opts[:operation] = "ItemLookup"
82
+ opts[:item_id] = item_id
140
83
 
141
- # Return true if request is valid.
142
- def is_valid_request?
143
- (@doc / 'isvalid').inner_html == 'True'
84
+ send_request(opts)
144
85
  end
145
86
 
146
- # Return true if response has an error.
147
- def has_error?
148
- !(error.nil? || error.empty?)
87
+ # HACK : copied from book_providers.rb
88
+ def self.transport
89
+ config = Alexandria::Preferences.instance.http_proxy_config
90
+ config ? Net::HTTP.Proxy(*config) : Net::HTTP
149
91
  end
150
92
 
151
- # Return error message.
152
- def error
153
- Element.get(@doc, 'error/message')
154
- end
93
+ # Generic send request to ECS REST service. You have to specify the
94
+ # :operation parameter.
95
+ def self.send_request(opts)
96
+ opts = options.merge(opts) if options
97
+ request_url = prepare_url(opts)
98
+ log.debug { "Request URL: #{request_url}" }
99
+
100
+ res = transport.get_response(URI.parse(request_url))
101
+ unless res.is_a? Net::HTTPSuccess
102
+ raise Amazon::RequestError, format(_("HTTP Response: %<code>s %<message>s"),
103
+ code: res.code, message: res.message)
104
+ end
155
105
 
156
- # Return an array of Amazon::Element item objects.
157
- def items
158
- @items ||= (@doc / 'item').map { |item| Element.new(item) }
159
- @items
106
+ Response.new(res.body)
160
107
  end
161
108
 
162
- # Return the first item (Amazon::Element)
163
- def first_item
164
- items.first
165
- end
109
+ # Response object returned after a REST call to Amazon service.
110
+ class Response
111
+ # XML input is in string format
112
+ def initialize(xml)
113
+ @doc = Hpricot(xml)
114
+ end
166
115
 
167
- # Return current page no if :item_page option is when initiating the request.
168
- def item_page
169
- @item_page ||= (@doc / 'itemsearchrequest/itempage').inner_html.to_i
170
- @item_page
171
- end
116
+ # Return Hpricot object.
117
+ attr_reader :doc
172
118
 
173
- # Return total results.
174
- def total_results
175
- @total_results ||= (@doc / 'totalresults').inner_html.to_i
176
- @total_results
177
- end
119
+ # Return true if request is valid.
120
+ def is_valid_request?
121
+ (@doc / "isvalid").inner_html == "True"
122
+ end
123
+
124
+ # Return true if response has an error.
125
+ def has_error?
126
+ !(error.nil? || error.empty?)
127
+ end
128
+
129
+ # Return error message.
130
+ def error
131
+ Element.get(@doc, "error/message")
132
+ end
133
+
134
+ # Return an array of Amazon::Element item objects.
135
+ def items
136
+ @items ||= (@doc / "item").map { |item| Element.new(item) }
137
+ @items
138
+ end
139
+
140
+ # Return the first item (Amazon::Element)
141
+ def first_item
142
+ items.first
143
+ end
144
+
145
+ # Return current page no if :item_page option is when initiating the request.
146
+ def item_page
147
+ @item_page ||= (@doc / "itemsearchrequest/itempage").inner_html.to_i
148
+ @item_page
149
+ end
150
+
151
+ # Return total results.
152
+ def total_results
153
+ @total_results ||= (@doc / "totalresults").inner_html.to_i
154
+ @total_results
155
+ end
178
156
 
179
- # Return total pages.
180
- def total_pages
181
- @total_pages ||= (@doc / 'totalpages').inner_html.to_i
182
- @total_pages
157
+ # Return total pages.
158
+ def total_pages
159
+ @total_pages ||= (@doc / "totalpages").inner_html.to_i
160
+ @total_pages
161
+ end
183
162
  end
184
- end
185
163
 
186
- # protected
187
- # def self.log(s)
188
- # return unless self.debug
189
- # if defined? RAILS_DEFAULT_LOGGER
190
- # RAILS_DEFAULT_LOGGER.error(s)
191
- # elsif defined? LOGGER
192
- # LOGGER.error(s)
193
- # else
194
- # puts s
195
- # end
196
- # end
197
-
198
- def self.prepare_url(opts)
199
- country = opts.delete(:country)
200
- country = country.nil? ? 'us' : country
201
- request_url = SERVICE_URLS[country.to_sym]
202
- raise Amazon::RequestError, "Invalid country '#{country}'" unless request_url
203
-
204
- qs = ''
205
- opts.each { |k, v|
206
- next unless v
207
-
208
- v = v.join(',') if v.is_a? Array
209
- qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
210
- }
211
- url = "#{request_url}#{qs}"
212
- # puts ">>> base url >> #{url}"
213
- signed_url = sign_request(url)
214
- # puts ">>> SIGNED >> #{signed_url}"
215
- signed_url
216
- end
164
+ def self.prepare_url(opts)
165
+ country = opts.delete(:country)
166
+ country = country.nil? ? "us" : country
167
+ request_url = SERVICE_URLS[country.to_sym]
168
+ unless request_url
169
+ raise Amazon::RequestError,
170
+ format(_("Invalid country '%<country>s'"), country: country)
171
+ end
217
172
 
218
- def self.camelize(s)
219
- s.to_s.
220
- gsub(/\/(.?)/) { '::' + Regexp.last_match[1].upcase }.
221
- gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
222
- end
173
+ qs = ""
174
+ opts.each do |k, v|
175
+ next unless v
223
176
 
224
- def self.hmac_sha256(message, key)
225
- block_size = 64
226
- ipad = "\x36" * block_size
227
- opad = "\x5c" * block_size
228
- if key.size > block_size
229
- d = Digest::SHA256.new
230
- key = d.digest(key)
177
+ v = v.join(",") if v.is_a? Array
178
+ qs << "&#{camelize(k.to_s)}=#{CGI.escape(v.to_s)}"
179
+ end
180
+ url = "#{request_url}#{qs}"
181
+ sign_request(url)
231
182
  end
232
183
 
233
- ipad_bytes = ipad.bytes.map { |b| b }
234
- opad_bytes = opad.bytes.map { |b| b }
235
- key_bytes = key.bytes.map { |b| b }
236
- ipad_xor = ''
237
- opad_xor = ''
238
- for i in 0..key.size - 1
239
- ipad_xor << (ipad_bytes[i] ^ key_bytes[i])
240
- opad_xor << (opad_bytes[i] ^ key_bytes[i])
184
+ def self.camelize(string)
185
+ string.to_s
186
+ .gsub(%r{/(.?)}) { "::" + Regexp.last_match[1].upcase }
187
+ .gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
241
188
  end
242
189
 
243
- ipad = ipad_xor + ipad[key.size..-1]
244
- opad = opad_xor + opad[key.size..-1]
190
+ def self.hmac_sha256(message, key)
191
+ block_size = 64
192
+ ipad = "\x36" * block_size
193
+ opad = "\x5c" * block_size
194
+ if key.size > block_size
195
+ d = Digest::SHA256.new
196
+ key = d.digest(key)
197
+ end
245
198
 
246
- # inner hash
247
- d1 = Digest::SHA256.new
248
- d1.update(ipad)
249
- d1.update(message)
250
- msg_hash = d1.digest
199
+ ipad_bytes = ipad.bytes.map { |b| b }
200
+ opad_bytes = opad.bytes.map { |b| b }
201
+ key_bytes = key.bytes.map { |b| b }
202
+ ipad_xor = ""
203
+ opad_xor = ""
204
+ (0..key.size - 1).each do |i|
205
+ ipad_xor << (ipad_bytes[i] ^ key_bytes[i])
206
+ opad_xor << (opad_bytes[i] ^ key_bytes[i])
207
+ end
251
208
 
252
- # outer hash
253
- d2 = Digest::SHA256.new
254
- d2.update(opad)
255
- d2.update(msg_hash)
256
- d2.digest
257
- end
209
+ ipad = ipad_xor + ipad[key.size..-1]
210
+ opad = opad_xor + opad[key.size..-1]
258
211
 
259
- def self.sign_request(request)
260
- raise AmazonNotConfiguredError unless @@secret_access_key
212
+ # inner hash
213
+ d1 = Digest::SHA256.new
214
+ d1.update(ipad)
215
+ d1.update(message)
216
+ msg_hash = d1.digest
261
217
 
262
- # Step 0 : Split apart request string
263
- url_pattern = /http:\/\/([^\/]+)(\/[^\?]+)\?(.*$)/
264
- url_pattern =~ request
265
- host = Regexp.last_match[1]
266
- path = Regexp.last_match[2]
267
- param_string = Regexp.last_match[3]
218
+ # outer hash
219
+ d2 = Digest::SHA256.new
220
+ d2.update(opad)
221
+ d2.update(msg_hash)
222
+ d2.digest
223
+ end
268
224
 
269
- # Step 1: enter the timestamp
270
- t = Time.now.getutc # MUST be in UTC
271
- stamp = t.strftime('%Y-%m-%dT%H:%M:%SZ')
272
- param_string += "&Timestamp=#{stamp}"
225
+ def self.sign_request(request)
226
+ raise AmazonNotConfiguredError unless @@secret_access_key
273
227
 
274
- # Step 2 : URL-encode
275
- param_string = param_string.gsub(',', '%2C').gsub(':', '%3A')
276
- # NOTE : take care not to double-encode
228
+ # Step 0 : Split apart request string
229
+ url_pattern = %r{http://([^/]+)(/[^?]+)\?(.*$)}
230
+ url_pattern =~ request
231
+ host = Regexp.last_match[1]
232
+ path = Regexp.last_match[2]
233
+ param_string = Regexp.last_match[3]
277
234
 
278
- # Step 3 : Split the parameter/value pairs
279
- params = param_string.split('&')
235
+ # Step 1: enter the timestamp
236
+ t = Time.now.getutc # MUST be in UTC
237
+ stamp = t.strftime("%Y-%m-%dT%H:%M:%SZ")
238
+ param_string += "&Timestamp=#{stamp}"
280
239
 
281
- # Step 4 : Sort params
282
- params.sort!
240
+ # Step 2 : URL-encode
241
+ param_string = param_string.gsub(",", "%2C").gsub(":", "%3A")
242
+ # NOTE : take care not to double-encode
283
243
 
284
- # Step 5 : Rejoin the param string
285
- canonical_param_string = params.join('&')
244
+ # Step 3 : Split the parameter/value pairs
245
+ params = param_string.split("&")
286
246
 
287
- # Steps 6 & 7: Prepend HTTP request info
288
- string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
247
+ # Step 4 : Sort params
248
+ params.sort!
289
249
 
290
- # puts string_to_sign
250
+ # Step 5 : Rejoin the param string
251
+ canonical_param_string = params.join("&")
291
252
 
292
- # Step 8 : Calculate RFC 2104-compliant HMAC with SHA256 hash algorithm
293
- sig = hmac_sha256(string_to_sign, @@secret_access_key)
294
- base64_sig = [sig].pack('m').strip
253
+ # Steps 6 & 7: Prepend HTTP request info
254
+ string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
295
255
 
296
- # Step 9 : URL-encode + and = in sig
297
- base64_sig = CGI.escape(base64_sig)
256
+ # Step 8 : Calculate RFC 2104-compliant HMAC with SHA256 hash algorithm
257
+ sig = hmac_sha256(string_to_sign, @@secret_access_key)
258
+ base64_sig = [sig].pack("m").strip
298
259
 
299
- # Step 10 : Add the URL encoded signature to your request
300
- "http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
301
- end
302
- end
260
+ # Step 9 : URL-encode + and = in sig
261
+ base64_sig = CGI.escape(base64_sig)
303
262
 
304
- # Internal wrapper class to provide convenient method to access Hpricot element value.
305
- class Element
306
- # Pass Hpricot::Elements object
307
- def initialize(element)
308
- @element = element
263
+ # Step 10 : Add the URL encoded signature to your request
264
+ "http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
265
+ end
309
266
  end
310
267
 
311
- # Returns Hpricot::Elments object
312
- def elem
313
- @element
314
- end
268
+ # Internal wrapper class to provide convenient method to access Hpricot element value.
269
+ class Element
270
+ # Pass Hpricot::Elements object
271
+ def initialize(element)
272
+ @element = element
273
+ end
315
274
 
316
- # Find Hpricot::Elements matching the given path. Example: element/"author".
317
- def /(path)
318
- elements = @element / path
319
- return nil if elements.empty?
275
+ # Returns Hpricot::Elments object
276
+ def elem
277
+ @element
278
+ end
320
279
 
321
- elements
322
- end
280
+ # Find Hpricot::Elements matching the given path. Example: element/"author".
281
+ def /(path)
282
+ elements = @element / path
283
+ return nil if elements.empty?
323
284
 
324
- # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
325
- # Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
326
- def search_and_convert(path)
327
- elements = self./(path)
328
- return unless elements
285
+ elements
286
+ end
329
287
 
330
- elements = elements.map { |element| Element.new(element) }
331
- return elements.first if elements.size == 1
288
+ # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
289
+ # Returns an array Amazon::Elements if more than Hpricot::Elements size is
290
+ # greater than 1.
291
+ def search_and_convert(path)
292
+ elements = self./(path)
293
+ return unless elements
332
294
 
333
- elements
334
- end
295
+ elements = elements.map { |element| Element.new(element) }
296
+ return elements.first if elements.size == 1
335
297
 
336
- # Get the text value of the given path, leave empty to retrieve current element value.
337
- def get(path = '')
338
- Element.get(@element, path)
339
- end
298
+ elements
299
+ end
340
300
 
341
- # Get the unescaped HTML text of the given path.
342
- def get_unescaped(path = '')
343
- Element.get_unescaped(@element, path)
344
- end
301
+ # Get the text value of the given path, leave empty to retrieve current element value.
302
+ def get(path = "")
303
+ Element.get(@element, path)
304
+ end
345
305
 
346
- # Get the array values of the given path.
347
- def get_array(path = '')
348
- Element.get_array(@element, path)
349
- end
306
+ # Get the unescaped HTML text of the given path.
307
+ def get_unescaped(path = "")
308
+ Element.get_unescaped(@element, path)
309
+ end
350
310
 
351
- # Get the children element text values in hash format with the element names as the hash keys.
352
- def get_hash(path = '')
353
- Element.get_hash(@element, path)
354
- end
311
+ # Get the array values of the given path.
312
+ def get_array(path = "")
313
+ Element.get_array(@element, path)
314
+ end
315
+
316
+ # Get the children element text values in hash format with the element
317
+ # names as the hash keys.
318
+ def get_hash(path = "")
319
+ Element.get_hash(@element, path)
320
+ end
355
321
 
356
- # Similar to #get, except an element object must be passed-in.
357
- def self.get(element, path = '')
358
- return unless element
322
+ # Similar to #get, except an element object must be passed-in.
323
+ def self.get(element, path = "")
324
+ return unless element
359
325
 
360
- result = element.at(path)
361
- ## inner_html doesn't decode entities, hence bug #21659
362
- # result = result.inner_html if result
363
- result = result.inner_text if result
364
- result
365
- end
326
+ result = element.at(path)
327
+ ## inner_html doesn't decode entities, hence bug #21659
328
+ # result = result.inner_html if result
329
+ result = result.inner_text if result
330
+ result
331
+ end
366
332
 
367
- # Similar to #get_unescaped, except an element object must be passed-in.
368
- def self.get_unescaped(element, path = '')
369
- result = get(element, path)
370
- CGI.unescapeHTML(result) if result
371
- end
333
+ # Similar to #get_unescaped, except an element object must be passed-in.
334
+ def self.get_unescaped(element, path = "")
335
+ result = get(element, path)
336
+ CGI.unescapeHTML(result) if result
337
+ end
372
338
 
373
- # Similar to #get_array, except an element object must be passed-in.
374
- def self.get_array(element, path = '')
375
- return unless element
376
-
377
- result = element / path
378
- if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
379
- parsed_result = []
380
- result.each { |item|
381
- parsed_result << Element.get(item)
382
- }
383
- parsed_result
384
- else
385
- [Element.get(result)]
339
+ # Similar to #get_array, except an element object must be passed-in.
340
+ def self.get_array(element, path = "")
341
+ return unless element
342
+
343
+ result = element / path
344
+ if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
345
+ parsed_result = []
346
+ result.each do |item|
347
+ parsed_result << Element.get(item)
348
+ end
349
+ parsed_result
350
+ else
351
+ [Element.get(result)]
352
+ end
386
353
  end
387
- end
388
354
 
389
- # Similar to #get_hash, except an element object must be passed-in.
390
- def self.get_hash(element, path = '')
391
- return unless element
355
+ # Similar to #get_hash, except an element object must be passed-in.
356
+ def self.get_hash(element, path = "")
357
+ result = element&.at(path)
358
+ return unless result
392
359
 
393
- result = element.at(path)
394
- if result
395
360
  hash = {}
396
361
  result = result.children
397
362
  result.each do |item|
@@ -399,10 +364,10 @@ module Amazon
399
364
  end
400
365
  hash
401
366
  end
402
- end
403
367
 
404
- def to_s
405
- elem&.to_s
368
+ def to_s
369
+ elem&.to_s
370
+ end
406
371
  end
407
372
  end
408
373
  end