alexandria-book-collection-manager 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +9 -0
  3. data/.gitignore +4 -1
  4. data/.rubocop.yml +51 -29
  5. data/.rubocop_todo.yml +33 -155
  6. data/.simplecov +5 -2
  7. data/.travis.yml +10 -4
  8. data/CHANGELOG.md +19 -0
  9. data/INSTALL.md +23 -11
  10. data/README.md +36 -35
  11. data/Rakefile +7 -5
  12. data/alexandria-book-collection-manager.gemspec +8 -3
  13. data/doc/dependency_decisions.yml +22 -3
  14. data/lib/alexandria.rb +2 -2
  15. data/lib/alexandria/book_providers.rb +47 -49
  16. data/lib/alexandria/book_providers/adlibris.rb +8 -13
  17. data/lib/alexandria/book_providers/amazon_aws.rb +47 -60
  18. data/lib/alexandria/book_providers/amazon_ecs_util.rb +283 -298
  19. data/lib/alexandria/book_providers/barnes_and_noble.rb +8 -8
  20. data/lib/alexandria/book_providers/douban.rb +2 -2
  21. data/lib/alexandria/book_providers/proxis.rb +12 -11
  22. data/lib/alexandria/book_providers/pseudomarc.rb +60 -70
  23. data/lib/alexandria/book_providers/siciliano.rb +5 -6
  24. data/lib/alexandria/book_providers/thalia.rb +8 -9
  25. data/lib/alexandria/book_providers/worldcat.rb +25 -31
  26. data/lib/alexandria/book_providers/z3950.rb +62 -69
  27. data/lib/alexandria/default_preferences.rb +37 -0
  28. data/lib/alexandria/execution_queue.rb +13 -12
  29. data/lib/alexandria/export_library.rb +4 -8
  30. data/lib/alexandria/import_library.rb +38 -62
  31. data/lib/alexandria/import_library_csv.rb +16 -16
  32. data/lib/alexandria/library_sort_order.rb +3 -1
  33. data/lib/alexandria/library_store.rb +16 -16
  34. data/lib/alexandria/logging.rb +4 -8
  35. data/lib/alexandria/models/book.rb +2 -2
  36. data/lib/alexandria/models/library.rb +18 -14
  37. data/lib/alexandria/net.rb +1 -2
  38. data/lib/alexandria/preferences.rb +27 -31
  39. data/lib/alexandria/scanners.rb +2 -2
  40. data/lib/alexandria/scanners/cue_cat.rb +5 -5
  41. data/lib/alexandria/scanners/keyboard.rb +1 -1
  42. data/lib/alexandria/smart_library.rb +22 -26
  43. data/lib/alexandria/ui.rb +7 -7
  44. data/lib/alexandria/ui/about_dialog.rb +1 -1
  45. data/lib/alexandria/ui/acquire_dialog.rb +9 -10
  46. data/lib/alexandria/ui/alert_dialog.rb +34 -19
  47. data/lib/alexandria/ui/bad_isbns_dialog.rb +13 -9
  48. data/lib/alexandria/ui/barcode_animation.rb +5 -5
  49. data/lib/alexandria/ui/book_properties_dialog.rb +2 -2
  50. data/lib/alexandria/ui/book_properties_dialog_base.rb +23 -17
  51. data/lib/alexandria/ui/callbacks.rb +141 -120
  52. data/lib/alexandria/ui/completion_models.rb +1 -1
  53. data/lib/alexandria/ui/confirm_erase_dialog.rb +1 -1
  54. data/lib/alexandria/ui/conflict_while_copying_dialog.rb +1 -1
  55. data/lib/alexandria/ui/error_dialog.rb +1 -1
  56. data/lib/alexandria/ui/export_dialog.rb +13 -15
  57. data/lib/alexandria/ui/icons.rb +34 -40
  58. data/lib/alexandria/ui/iconview_tooltips.rb +40 -53
  59. data/lib/alexandria/ui/import_dialog.rb +48 -47
  60. data/lib/alexandria/ui/init.rb +3 -2
  61. data/lib/alexandria/ui/keep_bad_isbn_dialog.rb +2 -2
  62. data/lib/alexandria/ui/libraries_combo.rb +10 -9
  63. data/lib/alexandria/ui/listview.rb +5 -6
  64. data/lib/alexandria/ui/main_app.rb +2 -2
  65. data/lib/alexandria/ui/multi_drag_treeview.rb +4 -6
  66. data/lib/alexandria/ui/new_book_dialog.rb +52 -52
  67. data/lib/alexandria/ui/new_provider_dialog.rb +12 -11
  68. data/lib/alexandria/ui/new_smart_library_dialog.rb +39 -27
  69. data/lib/alexandria/ui/preferences_dialog.rb +23 -82
  70. data/lib/alexandria/ui/provider_preferences_base_dialog.rb +9 -5
  71. data/lib/alexandria/ui/provider_preferences_dialog.rb +5 -5
  72. data/lib/alexandria/ui/really_delete_dialog.rb +1 -1
  73. data/lib/alexandria/ui/sidepane_manager.rb +35 -37
  74. data/lib/alexandria/ui/skip_entry_dialog.rb +3 -2
  75. data/lib/alexandria/ui/smart_library_properties_dialog.rb +35 -36
  76. data/lib/alexandria/ui/smart_library_properties_dialog_base.rb +59 -138
  77. data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
  78. data/lib/alexandria/ui/sound.rb +4 -6
  79. data/lib/alexandria/ui/ui_manager.rb +62 -64
  80. data/lib/alexandria/version.rb +2 -2
  81. data/lib/alexandria/web_themes.rb +15 -15
  82. data/po/cs.po +991 -874
  83. data/po/cy.po +957 -870
  84. data/po/de.po +991 -866
  85. data/po/el.po +987 -863
  86. data/po/es.po +986 -862
  87. data/po/fr.po +988 -868
  88. data/po/ga.po +910 -822
  89. data/po/gl.po +983 -863
  90. data/po/it.po +984 -862
  91. data/po/ja.po +969 -849
  92. data/po/mk.po +983 -859
  93. data/po/nb.po +982 -862
  94. data/po/nl.po +992 -869
  95. data/po/pl.po +1018 -963
  96. data/po/pt.po +983 -852
  97. data/po/pt_BR.po +983 -863
  98. data/po/ru.po +992 -869
  99. data/po/sk.po +986 -864
  100. data/po/sv.po +980 -860
  101. data/po/uk.po +975 -861
  102. data/po/zh_TW.po +974 -854
  103. data/share/alexandria/glade/main_app__builder.glade +6 -21
  104. data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
  105. data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
  106. data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
  107. data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
  108. data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
  109. data/spec/alexandria/book_providers_spec.rb +73 -129
  110. data/spec/alexandria/console_spec.rb +0 -5
  111. data/spec/alexandria/export_library_spec.rb +27 -38
  112. data/spec/alexandria/library_spec.rb +56 -44
  113. data/spec/alexandria/preferences_spec.rb +29 -3
  114. data/spec/alexandria/scanners/cue_cat_spec.rb +1 -1
  115. data/spec/alexandria/ui/about_dialog_spec.rb +1 -1
  116. data/spec/alexandria/ui/acquire_dialog_spec.rb +1 -1
  117. data/spec/alexandria/ui/alert_dialog_spec.rb +1 -1
  118. data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +1 -1
  119. data/spec/alexandria/ui/book_properties_dialog_spec.rb +1 -1
  120. data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +1 -1
  121. data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +1 -1
  122. data/spec/alexandria/ui/error_dialog_spec.rb +1 -1
  123. data/spec/alexandria/ui/export_dialog_spec.rb +1 -1
  124. data/spec/alexandria/ui/icons_spec.rb +26 -0
  125. data/spec/alexandria/ui/iconview_spec.rb +1 -1
  126. data/spec/alexandria/ui/import_dialog_spec.rb +30 -3
  127. data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +1 -1
  128. data/spec/alexandria/ui/main_app_spec.rb +1 -1
  129. data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +1 -1
  130. data/spec/alexandria/ui/new_provider_dialog_spec.rb +19 -3
  131. data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +28 -3
  132. data/spec/alexandria/ui/preferences_dialog_spec.rb +1 -1
  133. data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +23 -8
  134. data/spec/alexandria/ui/really_delete_dialog_spec.rb +1 -1
  135. data/spec/alexandria/ui/sidepane_manager_spec.rb +2 -2
  136. data/spec/alexandria/ui/skip_entry_dialog_spec.rb +1 -1
  137. data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +21 -7
  138. data/spec/alexandria/ui/ui_manager_spec.rb +39 -2
  139. data/spec/spec_helper.rb +46 -3
  140. data/tasks/spec.rake +3 -5
  141. data/util/rake/fileinstall.rb +12 -11
  142. metadata +82 -10
  143. data/spec/alexandria/ui/ui_utilities_spec.rb +0 -62
  144. data/spec/alexandria/utilities_spec.rb +0 -52
@@ -17,7 +17,7 @@ require "alexandria/book_providers/web"
17
17
  module Alexandria
18
18
  class BookProviders
19
19
  class AdLibrisProvider < WebsiteBasedProvider
20
- include Alexandria::Logging
20
+ include Logging
21
21
 
22
22
  SITE = "http://www.adlibris.com/se/"
23
23
 
@@ -81,9 +81,6 @@ module Alexandria
81
81
  end
82
82
 
83
83
  def parse_search_result_data(html)
84
- # adlibris site presents data in ISO-8859-1, so change it to UTF-8
85
- # html = Iconv.conv("UTF-8", "ISO-8859-1", html)
86
- # doc = Hpricot(html)
87
84
  doc = html_to_doc(html)
88
85
  book_search_results = []
89
86
 
@@ -110,12 +107,10 @@ module Alexandria
110
107
  def parse_result_data(html)
111
108
  doc = html_to_doc(html)
112
109
  begin
113
- title = nil
114
- if (h1 = doc.at("div.productTitleFormat h1"))
115
- title = text_of(h1)
116
- else
117
- raise NoResultsError, "title not found on page"
118
- end
110
+ h1 = doc.at("div.productTitleFormat h1")
111
+ raise NoResultsError, _("title not found on page") unless h1
112
+
113
+ title = text_of(h1)
119
114
 
120
115
  product = doc.at("div.product")
121
116
  ul_info = doc.at("ul.info") # NOTE, two of these
@@ -136,7 +131,7 @@ module Alexandria
136
131
  binding = nil
137
132
  if (format = doc.search("div.productTitleFormat span").first)
138
133
  binding = text_of(format)
139
- binding = Regexp.last_match[1] if binding =~ /\(([^\)]+)\)/
134
+ binding = Regexp.last_match[1] if binding =~ /\(([^)]+)\)/
140
135
  end
141
136
 
142
137
  year = nil
@@ -153,7 +148,7 @@ module Alexandria
153
148
  next unless isbn =~ /[0-9x]{10,13}/i
154
149
 
155
150
  isbn.gsub(/(\n|\r)/, " ")
156
- isbn = Regexp.last_match[1] if isbn =~ /:[\s]*([0-9x]+)/i
151
+ isbn = Regexp.last_match[1] if isbn =~ /:\s*([0-9x]+)/i
157
152
  isbns << isbn
158
153
  end
159
154
  isbn = isbns.first
@@ -164,7 +159,7 @@ module Alexandria
164
159
  cover_img =
165
160
  doc.search('span.imageWithShadow img[@id$="ProductImageNotLinked"]').first
166
161
  if cover_img
167
- image_url = if %r{^http\://}.match?(cover_img["src"])
162
+ image_url = if cover_img["src"].start_with?("http://")
168
163
  cover_img["src"]
169
164
  else
170
165
  "#{SITE}/#{cover_img['src']}" # HACK: use html base
@@ -1,23 +1,8 @@
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
 
@@ -49,22 +34,21 @@ module Alexandria
49
34
 
50
35
  if token
51
36
  token.new_value = token.value.strip if token.value != token.value.strip
52
- end
53
- if token && ((token.value.size != 20) || (token.value == "0J356Z09CN88KB743582"))
54
- token.new_value = ""
37
+ if (token.value.size != 20) || (token.value == "0J356Z09CN88KB743582")
38
+ token.new_value = ""
39
+ end
55
40
  end
56
41
 
57
42
  secret = prefs.variable_named("secret_key")
58
- if secret
59
- secret.new_value = secret.value.strip if secret.value != secret.value.strip
43
+ if secret && (secret.value != secret.value.strip)
44
+ secret.new_value = secret.value.strip
60
45
  end
61
46
 
62
- associate = prefs.variable_named("associate_tag")
63
- if associate
64
- associate.new_value = "rubyalexa-20" if associate.value.strip.empty?
65
- if associate.value != associate.value.strip
66
- associate.new_value = associate.value.strip
67
- end
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
68
52
  end
69
53
  end
70
54
 
@@ -72,9 +56,7 @@ module Alexandria
72
56
  prefs.read
73
57
 
74
58
  if prefs["secret_key"].empty?
75
- raise(Amazon::RequestError,
76
- "Secret Access Key required for Authentication:" \
77
- " you must sign up for your own Amazon AWS account")
59
+ raise(Amazon::RequestError, _("Provide secret key for your Amazon AWS account."))
78
60
  end
79
61
 
80
62
  if (config = Alexandria::Preferences.instance.http_proxy_config)
@@ -106,6 +88,7 @@ module Alexandria
106
88
  res = Amazon::Ecs.item_search(criterion,
107
89
  response_group: "ItemAttributes,Images",
108
90
  country: request_locale)
91
+
109
92
  res.items.each do |item|
110
93
  products << item
111
94
  end
@@ -163,7 +146,7 @@ module Alexandria
163
146
  else
164
147
  raise InvalidSearchTypeError
165
148
  end
166
- raise Amazon::RequestError, "No products" if products.empty?
149
+ raise Amazon::RequestError, _("No products") if products.empty?
167
150
  # raise NoResultsError if products.empty?
168
151
  rescue Amazon::RequestError => ex
169
152
  log.debug { "Got Amazon::RequestError at #{request_locale}: #{ex}" }
@@ -201,42 +184,25 @@ module Alexandria
201
184
  if results.size == 1
202
185
  results.first
203
186
  else
204
- log.info { "Found multiple results for lookup: checking each" }
205
- query_isbn_canon = Library.canonicalise_ean(criterion)
206
- results.each do |rslt|
207
- book = rslt[0]
208
- book_isbn_canon = Library.canonicalise_ean(book.isbn)
209
- return rslt if query_isbn_canon == book_isbn_canon
210
-
211
- log.debug { "rejected possible result #{book}" }
212
- end
213
- # gone through all and no ISBN match, so just return first result
214
- log.info do
215
- "no more results to check. Returning first result, just an approximation"
216
- end
217
- results.first
187
+ exact_match_or_first(criterion, results)
218
188
  end
219
189
  else
220
190
  results
221
191
  end
222
192
  end
223
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
+
224
203
  def url(book)
225
204
  isbn = Library.canonicalise_isbn(book.isbn)
226
- url = case prefs["locale"]
227
- when "fr"
228
- "http://www.amazon.fr/exec/obidos/ASIN/%s"
229
- when "uk"
230
- "http://www.amazon.co.uk/exec/obidos/ASIN/%s"
231
- when "de"
232
- "http://www.amazon.de/exec/obidos/ASIN/%s"
233
- when "ca"
234
- "http://www.amazon.ca/exec/obidos/ASIN/%s"
235
- when "jp"
236
- "http://www.amazon.jp/exec/obidos/ASIN/%s"
237
- when "us"
238
- "http://www.amazon.com/exec/obidos/ASIN/%s"
239
- end
205
+ url = LOCALE_URLS.fetch(prefs["locale"])
240
206
  url % isbn
241
207
  rescue StandardError => ex
242
208
  log.warn { "Cannot create url for book #{book}; #{ex.message}" }
@@ -247,6 +213,27 @@ module Alexandria
247
213
  str = str.squeeze(" ").strip unless str.nil?
248
214
  str
249
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
250
237
  end
251
238
  end
252
239
  end
@@ -10,368 +10,353 @@ require "cgi"
10
10
 
11
11
  require "digest/sha2"
12
12
 
13
- module Amazon
14
- class RequestError < StandardError; end
15
-
16
- class Ecs
17
- include Alexandria::Logging
18
-
19
- SERVICE_URLS = {
20
- us: "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService",
21
- uk: "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService",
22
- ca: "http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService",
23
- de: "http://webservices.amazon.de/onca/xml?Service=AWSECommerceService",
24
- jp: "http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService",
25
- fr: "http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService"
26
- }.freeze
27
-
28
- @@options = {}
29
- @@debug = false
30
-
31
- @@secret_access_key = ""
32
-
33
- # Default search options
34
- def self.options
35
- @@options
36
- end
37
-
38
- def self.secret_access_key=(key)
39
- @@secret_access_key = key
40
- end
41
-
42
- # Set default search options
43
- def self.options=(opts)
44
- @@options = opts
45
- end
46
-
47
- # Get debug flag.
48
- def self.debug
49
- @@debug
50
- end
13
+ module Alexandria
14
+ module Amazon
15
+ class RequestError < StandardError; end
16
+
17
+ class Ecs
18
+ include Logging
19
+
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
28
+
29
+ @@options = {}
30
+ @@debug = false
31
+
32
+ @@secret_access_key = ""
33
+
34
+ # Default search options
35
+ def self.options
36
+ @@options
37
+ end
51
38
 
52
- # Set debug flag to true or false.
53
- def self.debug=(dbg)
54
- @@debug = dbg
55
- end
39
+ def self.secret_access_key=(key)
40
+ @@secret_access_key = key
41
+ end
56
42
 
57
- def self.configure(&_proc)
58
- raise ArgumentError, "Block is required." unless block_given?
43
+ # Set default search options
44
+ def self.options=(opts)
45
+ @@options = opts
46
+ end
59
47
 
60
- yield @@options
61
- end
48
+ # Get debug flag.
49
+ def self.debug
50
+ @@debug
51
+ end
62
52
 
63
- # Search amazon items with search terms. Default search index option is 'Books'.
64
- # For other search type other than keywords, please specify
65
- # :type => [search type param name].
66
- def self.item_search(terms, opts = {})
67
- opts[:operation] = "ItemSearch"
68
- opts[:search_index] = opts[:search_index] || "Books"
69
-
70
- type = opts.delete(:type)
71
- if type
72
- opts[type.to_sym] = terms
73
- else
74
- opts[:keywords] = terms
53
+ # Set debug flag to true or false.
54
+ def self.debug=(dbg)
55
+ @@debug = dbg
75
56
  end
76
57
 
77
- send_request(opts)
78
- end
58
+ def self.configure(&_proc)
59
+ yield @@options
60
+ end
79
61
 
80
- # Search an item by ASIN no.
81
- def self.item_lookup(item_id, opts = {})
82
- opts[:operation] = "ItemLookup"
83
- opts[:item_id] = item_id
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
84
75
 
85
- send_request(opts)
86
- end
76
+ send_request(opts)
77
+ end
87
78
 
88
- # HACK : copied from book_providers.rb
89
- def self.transport
90
- config = Alexandria::Preferences.instance.http_proxy_config
91
- config ? Net::HTTP.Proxy(*config) : Net::HTTP
92
- end
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
93
83
 
94
- # Generic send request to ECS REST service. You have to specify the
95
- # :operation parameter.
96
- def self.send_request(opts)
97
- opts = options.merge(opts) if options
98
- request_url = prepare_url(opts)
99
- log.debug { "Request URL: #{request_url}" }
84
+ send_request(opts)
85
+ end
100
86
 
101
- res = transport.get_response(URI.parse(request_url))
102
- unless res.is_a? Net::HTTPSuccess
103
- raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
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
104
91
  end
105
92
 
106
- Response.new(res.body)
107
- 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
108
105
 
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)
106
+ Response.new(res.body)
114
107
  end
115
108
 
116
- # Return Hpricot object.
117
- attr_reader :doc
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
118
115
 
119
- # Return true if request is valid.
120
- def is_valid_request?
121
- (@doc / "isvalid").inner_html == "True"
122
- end
116
+ # Return Hpricot object.
117
+ attr_reader :doc
123
118
 
124
- # Return true if response has an error.
125
- def has_error?
126
- !(error.nil? || error.empty?)
127
- end
119
+ # Return true if request is valid.
120
+ def is_valid_request?
121
+ (@doc / "isvalid").inner_html == "True"
122
+ end
128
123
 
129
- # Return error message.
130
- def error
131
- Element.get(@doc, "error/message")
132
- end
124
+ # Return true if response has an error.
125
+ def has_error?
126
+ !(error.nil? || error.empty?)
127
+ end
133
128
 
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
129
+ # Return error message.
130
+ def error
131
+ Element.get(@doc, "error/message")
132
+ end
139
133
 
140
- # Return the first item (Amazon::Element)
141
- def first_item
142
- items.first
143
- end
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
144
139
 
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
140
+ # Return the first item (Amazon::Element)
141
+ def first_item
142
+ items.first
143
+ end
150
144
 
151
- # Return total results.
152
- def total_results
153
- @total_results ||= (@doc / "totalresults").inner_html.to_i
154
- @total_results
155
- end
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
156
150
 
157
- # Return total pages.
158
- def total_pages
159
- @total_pages ||= (@doc / "totalpages").inner_html.to_i
160
- @total_pages
161
- end
162
- end
151
+ # Return total results.
152
+ def total_results
153
+ @total_results ||= (@doc / "totalresults").inner_html.to_i
154
+ @total_results
155
+ end
163
156
 
164
- # protected
165
- # def self.log(s)
166
- # return unless self.debug
167
- # if defined? RAILS_DEFAULT_LOGGER
168
- # RAILS_DEFAULT_LOGGER.error(s)
169
- # elsif defined? LOGGER
170
- # LOGGER.error(s)
171
- # else
172
- # puts s
173
- # end
174
- # end
175
-
176
- def self.prepare_url(opts)
177
- country = opts.delete(:country)
178
- country = country.nil? ? "us" : country
179
- request_url = SERVICE_URLS[country.to_sym]
180
- raise Amazon::RequestError, "Invalid country '#{country}'" unless request_url
181
-
182
- qs = ""
183
- opts.each do |k, v|
184
- next unless v
185
-
186
- v = v.join(",") if v.is_a? Array
187
- qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
157
+ # Return total pages.
158
+ def total_pages
159
+ @total_pages ||= (@doc / "totalpages").inner_html.to_i
160
+ @total_pages
161
+ end
188
162
  end
189
- url = "#{request_url}#{qs}"
190
- # puts ">>> base url >> #{url}"
191
- signed_url = sign_request(url)
192
- # puts ">>> SIGNED >> #{signed_url}"
193
- signed_url
194
- end
195
163
 
196
- def self.camelize(s)
197
- s.to_s
198
- .gsub(%r{/(.?)}) { "::" + Regexp.last_match[1].upcase }
199
- .gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
200
- 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
201
172
 
202
- def self.hmac_sha256(message, key)
203
- block_size = 64
204
- ipad = "\x36" * block_size
205
- opad = "\x5c" * block_size
206
- if key.size > block_size
207
- d = Digest::SHA256.new
208
- key = d.digest(key)
173
+ qs = ""
174
+ opts.each do |k, v|
175
+ next unless v
176
+
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)
209
182
  end
210
183
 
211
- ipad_bytes = ipad.bytes.map { |b| b }
212
- opad_bytes = opad.bytes.map { |b| b }
213
- key_bytes = key.bytes.map { |b| b }
214
- ipad_xor = ""
215
- opad_xor = ""
216
- (0..key.size - 1).each do |i|
217
- ipad_xor << (ipad_bytes[i] ^ key_bytes[i])
218
- 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 }
219
188
  end
220
189
 
221
- ipad = ipad_xor + ipad[key.size..-1]
222
- 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
223
198
 
224
- # inner hash
225
- d1 = Digest::SHA256.new
226
- d1.update(ipad)
227
- d1.update(message)
228
- 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
229
208
 
230
- # outer hash
231
- d2 = Digest::SHA256.new
232
- d2.update(opad)
233
- d2.update(msg_hash)
234
- d2.digest
235
- end
209
+ ipad = ipad_xor + ipad[key.size..-1]
210
+ opad = opad_xor + opad[key.size..-1]
236
211
 
237
- def self.sign_request(request)
238
- 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
239
217
 
240
- # Step 0 : Split apart request string
241
- url_pattern = %r{http://([^/]+)(/[^\?]+)\?(.*$)}
242
- url_pattern =~ request
243
- host = Regexp.last_match[1]
244
- path = Regexp.last_match[2]
245
- 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
246
224
 
247
- # Step 1: enter the timestamp
248
- t = Time.now.getutc # MUST be in UTC
249
- stamp = t.strftime("%Y-%m-%dT%H:%M:%SZ")
250
- param_string += "&Timestamp=#{stamp}"
225
+ def self.sign_request(request)
226
+ raise AmazonNotConfiguredError unless @@secret_access_key
251
227
 
252
- # Step 2 : URL-encode
253
- param_string = param_string.gsub(",", "%2C").gsub(":", "%3A")
254
- # 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]
255
234
 
256
- # Step 3 : Split the parameter/value pairs
257
- 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}"
258
239
 
259
- # Step 4 : Sort params
260
- params.sort!
240
+ # Step 2 : URL-encode
241
+ param_string = param_string.gsub(",", "%2C").gsub(":", "%3A")
242
+ # NOTE : take care not to double-encode
261
243
 
262
- # Step 5 : Rejoin the param string
263
- canonical_param_string = params.join("&")
244
+ # Step 3 : Split the parameter/value pairs
245
+ params = param_string.split("&")
264
246
 
265
- # Steps 6 & 7: Prepend HTTP request info
266
- string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
247
+ # Step 4 : Sort params
248
+ params.sort!
267
249
 
268
- # puts string_to_sign
250
+ # Step 5 : Rejoin the param string
251
+ canonical_param_string = params.join("&")
269
252
 
270
- # Step 8 : Calculate RFC 2104-compliant HMAC with SHA256 hash algorithm
271
- sig = hmac_sha256(string_to_sign, @@secret_access_key)
272
- 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}"
273
255
 
274
- # Step 9 : URL-encode + and = in sig
275
- 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
276
259
 
277
- # Step 10 : Add the URL encoded signature to your request
278
- "http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
279
- end
280
- end
260
+ # Step 9 : URL-encode + and = in sig
261
+ base64_sig = CGI.escape(base64_sig)
281
262
 
282
- # Internal wrapper class to provide convenient method to access Hpricot element value.
283
- class Element
284
- # Pass Hpricot::Elements object
285
- def initialize(element)
286
- @element = element
263
+ # Step 10 : Add the URL encoded signature to your request
264
+ "http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
265
+ end
287
266
  end
288
267
 
289
- # Returns Hpricot::Elments object
290
- def elem
291
- @element
292
- 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
293
274
 
294
- # Find Hpricot::Elements matching the given path. Example: element/"author".
295
- def /(path)
296
- elements = @element / path
297
- return nil if elements.empty?
275
+ # Returns Hpricot::Elments object
276
+ def elem
277
+ @element
278
+ end
298
279
 
299
- elements
300
- 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?
301
284
 
302
- # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
303
- # Returns an array Amazon::Elements if more than Hpricot::Elements size is
304
- # greater than 1.
305
- def search_and_convert(path)
306
- elements = self./(path)
307
- return unless elements
285
+ elements
286
+ end
308
287
 
309
- elements = elements.map { |element| Element.new(element) }
310
- 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
311
294
 
312
- elements
313
- end
295
+ elements = elements.map { |element| Element.new(element) }
296
+ return elements.first if elements.size == 1
314
297
 
315
- # Get the text value of the given path, leave empty to retrieve current element value.
316
- def get(path = "")
317
- Element.get(@element, path)
318
- end
298
+ elements
299
+ end
319
300
 
320
- # Get the unescaped HTML text of the given path.
321
- def get_unescaped(path = "")
322
- Element.get_unescaped(@element, path)
323
- 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
324
305
 
325
- # Get the array values of the given path.
326
- def get_array(path = "")
327
- Element.get_array(@element, path)
328
- end
306
+ # Get the unescaped HTML text of the given path.
307
+ def get_unescaped(path = "")
308
+ Element.get_unescaped(@element, path)
309
+ end
329
310
 
330
- # Get the children element text values in hash format with the element
331
- # names as the hash keys.
332
- def get_hash(path = "")
333
- Element.get_hash(@element, path)
334
- end
311
+ # Get the array values of the given path.
312
+ def get_array(path = "")
313
+ Element.get_array(@element, path)
314
+ end
335
315
 
336
- # Similar to #get, except an element object must be passed-in.
337
- def self.get(element, path = "")
338
- return unless element
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
339
321
 
340
- result = element.at(path)
341
- ## inner_html doesn't decode entities, hence bug #21659
342
- # result = result.inner_html if result
343
- result = result.inner_text if result
344
- result
345
- end
322
+ # Similar to #get, except an element object must be passed-in.
323
+ def self.get(element, path = "")
324
+ return unless element
346
325
 
347
- # Similar to #get_unescaped, except an element object must be passed-in.
348
- def self.get_unescaped(element, path = "")
349
- result = get(element, path)
350
- CGI.unescapeHTML(result) if result
351
- 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
352
332
 
353
- # Similar to #get_array, except an element object must be passed-in.
354
- def self.get_array(element, path = "")
355
- return unless element
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
356
338
 
357
- result = element / path
358
- if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
359
- parsed_result = []
360
- result.each do |item|
361
- parsed_result << Element.get(item)
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)]
362
352
  end
363
- parsed_result
364
- else
365
- [Element.get(result)]
366
353
  end
367
- end
368
354
 
369
- # Similar to #get_hash, except an element object must be passed-in.
370
- def self.get_hash(element, path = "")
371
- 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
372
359
 
373
- result = element.at(path)
374
- if result
375
360
  hash = {}
376
361
  result = result.children
377
362
  result.each do |item|
@@ -379,10 +364,10 @@ module Amazon
379
364
  end
380
365
  hash
381
366
  end
382
- end
383
367
 
384
- def to_s
385
- elem&.to_s
368
+ def to_s
369
+ elem&.to_s
370
+ end
386
371
  end
387
372
  end
388
373
  end