alexandria-book-collection-manager 0.7.1 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. checksums.yaml +5 -5
  2. data/.github/dependabot.yml +9 -0
  3. data/.gitignore +5 -2
  4. data/.hound.yml +2 -0
  5. data/.rubocop.yml +113 -45
  6. data/.rubocop_todo.yml +82 -170
  7. data/.simplecov +5 -1
  8. data/.travis.yml +45 -0
  9. data/.yardopts +1 -1
  10. data/CHANGELOG.md +60 -0
  11. data/ChangeLog.0 +33 -35
  12. data/Gemfile +6 -5
  13. data/INSTALL.md +164 -0
  14. data/README.md +52 -42
  15. data/Rakefile +95 -109
  16. data/TODO.md +9 -1
  17. data/alexandria-book-collection-manager.gemspec +52 -45
  18. data/bin/alexandria +31 -53
  19. data/doc/AUTHORS +61 -0
  20. data/doc/BUGS +31 -0
  21. data/doc/FAQ +365 -0
  22. data/doc/HACKING +19 -0
  23. data/doc/NEWS +341 -0
  24. data/doc/alexandria.1 +120 -0
  25. data/doc/cuecat_support.rdoc +67 -0
  26. data/doc/dependency_decisions.yml +80 -0
  27. data/lib/alexandria.rb +29 -37
  28. data/lib/alexandria/about.rb +52 -51
  29. data/lib/alexandria/book_providers.rb +94 -101
  30. data/lib/alexandria/book_providers/adlibris.rb +45 -85
  31. data/lib/alexandria/book_providers/amazon_aws.rb +105 -113
  32. data/lib/alexandria/book_providers/amazon_ecs_util.rb +293 -324
  33. data/lib/alexandria/book_providers/barnes_and_noble.rb +54 -53
  34. data/lib/alexandria/book_providers/douban.rb +29 -51
  35. data/lib/alexandria/book_providers/proxis.rb +42 -59
  36. data/lib/alexandria/book_providers/pseudomarc.rb +79 -99
  37. data/lib/alexandria/book_providers/siciliano.rb +68 -70
  38. data/lib/alexandria/book_providers/thalia.rb +46 -45
  39. data/lib/alexandria/book_providers/web.rb +17 -33
  40. data/lib/alexandria/book_providers/worldcat.rb +74 -102
  41. data/lib/alexandria/book_providers/z3950.rb +170 -174
  42. data/lib/alexandria/config.rb +5 -3
  43. data/lib/alexandria/console.rb +10 -21
  44. data/lib/alexandria/default_preferences.rb +37 -0
  45. data/lib/alexandria/execution_queue.rb +17 -15
  46. data/lib/alexandria/export_format.rb +47 -0
  47. data/lib/alexandria/export_library.rb +188 -302
  48. data/lib/alexandria/import_library.rb +114 -155
  49. data/lib/alexandria/import_library_csv.rb +46 -96
  50. data/lib/alexandria/library_collection.rb +79 -0
  51. data/lib/alexandria/library_sort_order.rb +45 -0
  52. data/lib/alexandria/library_store.rb +233 -0
  53. data/lib/alexandria/logging.rb +15 -19
  54. data/lib/alexandria/models/book.rb +15 -20
  55. data/lib/alexandria/models/library.rb +81 -363
  56. data/lib/alexandria/net.rb +7 -6
  57. data/lib/alexandria/preferences.rb +73 -91
  58. data/lib/alexandria/scanners.rb +4 -2
  59. data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +24 -20
  60. data/lib/alexandria/scanners/keyboard.rb +10 -8
  61. data/lib/alexandria/smart_library.rb +135 -171
  62. data/lib/alexandria/ui.rb +17 -15
  63. data/lib/alexandria/ui/about_dialog.rb +49 -0
  64. data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +129 -152
  65. data/lib/alexandria/ui/alert_dialog.rb +64 -0
  66. data/lib/alexandria/ui/bad_isbns_dialog.rb +41 -0
  67. data/lib/alexandria/ui/{dialogs/barcode_animation.rb → barcode_animation.rb} +18 -15
  68. data/lib/alexandria/ui/{dialogs/book_properties_dialog.rb → book_properties_dialog.rb} +44 -61
  69. data/lib/alexandria/ui/{dialogs/book_properties_dialog_base.rb → book_properties_dialog_base.rb} +84 -89
  70. data/lib/alexandria/ui/builder_base.rb +9 -27
  71. data/lib/alexandria/ui/callbacks.rb +188 -186
  72. data/lib/alexandria/ui/columns.rb +2 -0
  73. data/lib/alexandria/ui/completion_models.rb +12 -23
  74. data/lib/alexandria/ui/confirm_erase_dialog.rb +33 -0
  75. data/lib/alexandria/ui/conflict_while_copying_dialog.rb +34 -0
  76. data/lib/alexandria/ui/dndable.rb +10 -8
  77. data/lib/alexandria/ui/error_dialog.rb +25 -0
  78. data/lib/alexandria/ui/export_dialog.rb +139 -0
  79. data/lib/alexandria/ui/icons.rb +49 -65
  80. data/lib/alexandria/ui/iconview.rb +15 -13
  81. data/lib/alexandria/ui/iconview_tooltips.rb +43 -58
  82. data/lib/alexandria/ui/import_dialog.rb +157 -0
  83. data/lib/alexandria/ui/init.rb +23 -33
  84. data/lib/alexandria/ui/keep_bad_isbn_dialog.rb +36 -0
  85. data/lib/alexandria/ui/libraries_combo.rb +18 -14
  86. data/lib/alexandria/ui/listview.rb +77 -88
  87. data/lib/alexandria/ui/main_app.rb +26 -26
  88. data/lib/alexandria/ui/misc_dialogs.rb +10 -0
  89. data/lib/alexandria/ui/multi_drag_treeview.rb +30 -41
  90. data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +168 -215
  91. data/lib/alexandria/ui/new_book_dialog_manual.rb +139 -0
  92. data/lib/alexandria/ui/new_provider_dialog.rb +100 -0
  93. data/lib/alexandria/ui/new_smart_library_dialog.rb +74 -0
  94. data/lib/alexandria/ui/preferences_dialog.rb +313 -0
  95. data/lib/alexandria/ui/provider_preferences_base_dialog.rb +95 -0
  96. data/lib/alexandria/ui/provider_preferences_dialog.rb +35 -0
  97. data/lib/alexandria/ui/really_delete_dialog.rb +53 -0
  98. data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +62 -72
  99. data/lib/alexandria/ui/skip_entry_dialog.rb +33 -0
  100. data/lib/alexandria/ui/smart_library_properties_dialog.rb +60 -0
  101. data/lib/alexandria/ui/{dialogs/smart_library_properties_dialog_base.rb → smart_library_properties_dialog_base.rb} +96 -172
  102. data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
  103. data/lib/alexandria/ui/sound.rb +13 -13
  104. data/lib/alexandria/ui/ui_manager.rb +262 -283
  105. data/lib/alexandria/undo_manager.rb +3 -0
  106. data/lib/alexandria/version.rb +6 -19
  107. data/lib/alexandria/web_themes.rb +24 -21
  108. data/po/Makefile +2 -2
  109. data/po/cs.po +993 -880
  110. data/po/cy.po +957 -874
  111. data/po/de.po +990 -869
  112. data/po/el.po +989 -869
  113. data/po/es.po +985 -865
  114. data/po/fr.po +986 -870
  115. data/po/ga.po +907 -823
  116. data/po/gl.po +981 -865
  117. data/po/it.po +986 -868
  118. data/po/ja.po +969 -853
  119. data/po/mk.po +983 -863
  120. data/po/nb.po +979 -863
  121. data/po/nl.po +983 -864
  122. data/po/pl.po +1020 -969
  123. data/po/pt.po +988 -861
  124. data/po/pt_BR.po +984 -868
  125. data/po/ru.po +992 -873
  126. data/po/sk.po +987 -869
  127. data/po/sv.po +977 -861
  128. data/po/uk.po +975 -865
  129. data/po/zh_TW.po +976 -860
  130. data/schemas/alexandria.schemas +25 -3
  131. data/share/alexandria/glade/acquire_dialog__builder.glade +15 -12
  132. data/share/alexandria/glade/book_properties_dialog__builder.glade +171 -299
  133. data/share/alexandria/glade/main_app__builder.glade +24 -33
  134. data/share/alexandria/glade/new_book_dialog__builder.glade +27 -59
  135. data/share/alexandria/glade/preferences_dialog__builder.glade +250 -290
  136. data/share/gnome/help/alexandria/C/introduction.xml +0 -8
  137. data/share/gnome/help/alexandria/C/searching.xml +1 -1
  138. data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
  139. data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
  140. data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
  141. data/share/gnome/help/alexandria/ja/introduction.xml +0 -8
  142. data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
  143. data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
  144. data/spec/alexandria/book_providers_spec.rb +77 -210
  145. data/spec/alexandria/book_spec.rb +16 -12
  146. data/spec/alexandria/console_spec.rb +27 -0
  147. data/spec/alexandria/export_library_spec.rb +130 -0
  148. data/spec/alexandria/library_spec.rb +130 -172
  149. data/spec/alexandria/library_store_spec.rb +37 -0
  150. data/spec/alexandria/preferences_spec.rb +46 -17
  151. data/spec/alexandria/scanners/cue_cat_spec.rb +52 -0
  152. data/spec/alexandria/smart_library_spec.rb +32 -25
  153. data/spec/alexandria/ui/about_dialog_spec.rb +14 -0
  154. data/spec/alexandria/ui/acquire_dialog_spec.rb +14 -0
  155. data/spec/alexandria/ui/alert_dialog_spec.rb +16 -0
  156. data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +14 -0
  157. data/spec/alexandria/ui/book_properties_dialog_spec.rb +17 -0
  158. data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +14 -0
  159. data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +16 -0
  160. data/spec/alexandria/ui/error_dialog_spec.rb +14 -0
  161. data/spec/alexandria/ui/export_dialog_spec.rb +15 -0
  162. data/spec/alexandria/ui/icons_spec.rb +26 -0
  163. data/spec/alexandria/ui/iconview_spec.rb +9 -21
  164. data/spec/alexandria/ui/import_dialog_spec.rb +41 -0
  165. data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +17 -0
  166. data/spec/alexandria/ui/main_app_spec.rb +8 -33
  167. data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +15 -0
  168. data/spec/alexandria/ui/new_book_dialog_spec.rb +22 -0
  169. data/spec/alexandria/ui/new_provider_dialog_spec.rb +30 -0
  170. data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +39 -0
  171. data/spec/alexandria/ui/preferences_dialog_spec.rb +14 -0
  172. data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +34 -0
  173. data/spec/alexandria/ui/really_delete_dialog_spec.rb +16 -0
  174. data/spec/alexandria/ui/sidepane_manager_spec.rb +15 -0
  175. data/spec/alexandria/ui/skip_entry_dialog_spec.rb +14 -0
  176. data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +32 -0
  177. data/spec/alexandria/ui/sound_spec.rb +4 -2
  178. data/spec/alexandria/ui/ui_manager_spec.rb +45 -20
  179. data/spec/end_to_end/basic_run_spec.rb +57 -0
  180. data/spec/spec_helper.rb +66 -33
  181. data/tasks/setup.rb +5 -3
  182. data/tasks/spec.rake +18 -3
  183. data/util/rake/fileinstall.rb +38 -40
  184. data/util/rake/gettextgenerate.rb +15 -70
  185. data/util/rake/omfgenerate.rb +10 -10
  186. metadata +176 -60
  187. data/INSTALL.rdoc +0 -148
  188. data/dogtail/basic_run_test.py +0 -9
  189. data/lib/alexandria/book_providers/bol_it.rb +0 -160
  190. data/lib/alexandria/book_providers/deastore.rb +0 -273
  191. data/lib/alexandria/book_providers/ibs_it.rb +0 -147
  192. data/lib/alexandria/book_providers/mcu.rb +0 -169
  193. data/lib/alexandria/book_providers/renaud.rb +0 -140
  194. data/lib/alexandria/book_providers/webster_it.rb +0 -167
  195. data/lib/alexandria/ui/dialogs/about_dialog.rb +0 -59
  196. data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -70
  197. data/lib/alexandria/ui/dialogs/bad_isbns_dialog.rb +0 -43
  198. data/lib/alexandria/ui/dialogs/export_dialog.rb +0 -171
  199. data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -196
  200. data/lib/alexandria/ui/dialogs/misc_dialogs.rb +0 -85
  201. data/lib/alexandria/ui/dialogs/new_book_dialog_manual.rb +0 -154
  202. data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -74
  203. data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -578
  204. data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -57
  205. data/spec/alexandria/scanners/cuecat_spec.rb +0 -65
  206. data/spec/alexandria/ui/dialogs_spec.rb +0 -94
  207. data/spec/alexandria/ui/sidepane_spec.rb +0 -27
  208. data/spec/alexandria/ui/ui_utilities_spec.rb +0 -60
  209. data/spec/alexandria/utilities_spec.rb +0 -50
  210. data/tasks/dogtail.rake +0 -4
@@ -1,393 +1,362 @@
1
- #--
2
- # Copyright (c) 2006 Herryanto Siatono, Pluit Solutions
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
1
+ # frozen_string_literal: true
2
+
3
+ # This file is part of Alexandria.
14
4
  #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
- # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
- # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
- # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
- # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- #++
23
-
24
- # Modified by Cathal Mc Ginley 2008-02-18
25
- # added Amazon::Ecs.transport - to enable Alexandria's proxy support
26
- # Modified by Cathal Mc Ginley 2008-08-26
27
- # Amazon::Element.get now uses inner_text, not inner_html, fixing #21659
28
- # Modified by Cathal Mc Ginley 2009-08-13
29
- # Added sign_request and hmac_sha256 methods for Authentication support
30
-
31
- require 'net/http'
32
- require 'hpricot'
33
- require 'cgi'
34
-
35
- require 'digest/sha2'
36
-
37
- module Amazon
38
- class RequestError < StandardError; end
39
-
40
- class Ecs
41
- include Alexandria::Logging
42
-
43
- SERVICE_URLS = { us: 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService',
44
- uk: 'http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService',
45
- ca: 'http://webservices.amazon.ca/onca/xml?Service=AWSECommerceService',
46
- de: 'http://webservices.amazon.de/onca/xml?Service=AWSECommerceService',
47
- jp: 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService',
48
- fr: 'http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService' }.freeze
49
-
50
- @@options = {}
51
- @@debug = false
52
-
53
- @@secret_access_key = ''
54
-
55
- # Default search options
56
- def self.options
57
- @@options
58
- end
5
+ # See the file README.md for authorship and licensing information.
59
6
 
60
- def self.secret_access_key=(key)
61
- @@secret_access_key = key
62
- end
7
+ require "net/http"
8
+ require "hpricot"
9
+ require "cgi"
63
10
 
64
- # Set default search options
65
- def self.options=(opts)
66
- @@options = opts
67
- end
11
+ require "digest/sha2"
68
12
 
69
- # Get debug flag.
70
- def self.debug
71
- @@debug
72
- end
13
+ module Alexandria
14
+ module Amazon
15
+ class RequestError < StandardError; end
73
16
 
74
- # Set debug flag to true or false.
75
- def self.debug=(dbg)
76
- @@debug = dbg
77
- end
17
+ class Ecs
18
+ include Logging
78
19
 
79
- def self.configure(&_proc)
80
- raise ArgumentError, 'Block is required.' unless block_given?
81
- yield @@options
82
- end
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
- # Search amazon items with search terms. Default search index option is 'Books'.
85
- # For other search type other than keywords, please specify :type => [search type param name].
86
- def self.item_search(terms, opts = {})
87
- opts[:operation] = 'ItemSearch'
88
- opts[:search_index] = opts[:search_index] || 'Books'
89
-
90
- type = opts.delete(:type)
91
- if type
92
- opts[type.to_sym] = terms
93
- else
94
- opts[:keywords] = terms
95
- end
29
+ @@options = {}
30
+ @@debug = false
96
31
 
97
- send_request(opts)
98
- end
32
+ @@secret_access_key = ""
99
33
 
100
- # Search an item by ASIN no.
101
- def self.item_lookup(item_id, opts = {})
102
- opts[:operation] = 'ItemLookup'
103
- opts[:item_id] = item_id
34
+ # Default search options
35
+ def self.options
36
+ @@options
37
+ end
104
38
 
105
- send_request(opts)
106
- end
39
+ def self.secret_access_key=(key)
40
+ @@secret_access_key = key
41
+ end
107
42
 
108
- # HACK : copied from book_providers.rb
109
- def self.transport
110
- config = Alexandria::Preferences.instance.http_proxy_config
111
- config ? Net::HTTP.Proxy(*config) : Net::HTTP
112
- end
43
+ # Set default search options
44
+ def self.options=(opts)
45
+ @@options = opts
46
+ end
113
47
 
114
- # Generic send request to ECS REST service. You have to specify the :operation parameter.
115
- def self.send_request(opts)
116
- opts = options.merge(opts) if options
117
- request_url = prepare_url(opts)
118
- log.debug { "Request URL: #{request_url}" }
48
+ # Get debug flag.
49
+ def self.debug
50
+ @@debug
51
+ end
119
52
 
120
- res = transport.get_response(URI.parse(request_url))
121
- unless res.is_a? Net::HTTPSuccess
122
- raise Amazon::RequestError, "HTTP Response: #{res.code} #{res.message}"
53
+ # Set debug flag to true or false.
54
+ def self.debug=(dbg)
55
+ @@debug = dbg
123
56
  end
124
- Response.new(res.body)
125
- end
126
57
 
127
- # Response object returned after a REST call to Amazon service.
128
- class Response
129
- # XML input is in string format
130
- def initialize(xml)
131
- @doc = Hpricot(xml)
58
+ def self.configure(&_proc)
59
+ yield @@options
132
60
  end
133
61
 
134
- # Return Hpricot object.
135
- attr_reader :doc
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
136
75
 
137
- # Return true if request is valid.
138
- def is_valid_request?
139
- (@doc / 'isvalid').inner_html == 'True'
76
+ send_request(opts)
140
77
  end
141
78
 
142
- # Return true if response has an error.
143
- def has_error?
144
- !(error.nil? || error.empty?)
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
83
+
84
+ send_request(opts)
145
85
  end
146
86
 
147
- # Return error message.
148
- def error
149
- Element.get(@doc, 'error/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
150
91
  end
151
92
 
152
- # Return an array of Amazon::Element item objects.
153
- def items
154
- unless @items
155
- @items = (@doc / 'item').map { |item| Element.new(item) }
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)
156
104
  end
157
- @items
158
- end
159
105
 
160
- # Return the first item (Amazon::Element)
161
- def first_item
162
- items.first
106
+ Response.new(res.body)
163
107
  end
164
108
 
165
- # Return current page no if :item_page option is when initiating the request.
166
- def item_page
167
- unless @item_page
168
- @item_page = (@doc / 'itemsearchrequest/itempage').inner_html.to_i
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)
169
114
  end
170
- @item_page
171
- end
172
115
 
173
- # Return total results.
174
- def total_results
175
- unless @total_results
176
- @total_results = (@doc / 'totalresults').inner_html.to_i
116
+ # Return Hpricot object.
117
+ attr_reader :doc
118
+
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
177
138
  end
178
- @total_results
179
- end
180
139
 
181
- # Return total pages.
182
- def total_pages
183
- unless @total_pages
184
- @total_pages = (@doc / 'totalpages').inner_html.to_i
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
156
+
157
+ # Return total pages.
158
+ def total_pages
159
+ @total_pages ||= (@doc / "totalpages").inner_html.to_i
160
+ @total_pages
185
161
  end
186
- @total_pages
187
162
  end
188
- end
189
163
 
190
- # protected
191
- # def self.log(s)
192
- # return unless self.debug
193
- # if defined? RAILS_DEFAULT_LOGGER
194
- # RAILS_DEFAULT_LOGGER.error(s)
195
- # elsif defined? LOGGER
196
- # LOGGER.error(s)
197
- # else
198
- # puts s
199
- # end
200
- # end
201
-
202
- def self.prepare_url(opts)
203
- country = opts.delete(:country)
204
- country = country.nil? ? 'us' : country
205
- request_url = SERVICE_URLS[country.to_sym]
206
- raise Amazon::RequestError, "Invalid country '#{country}'" unless request_url
207
-
208
- qs = ''
209
- opts.each { |k, v|
210
- next unless v
211
- v = v.join(',') if v.is_a? Array
212
- qs << "&#{camelize(k.to_s)}=#{URI.encode(v.to_s)}"
213
- }
214
- url = "#{request_url}#{qs}"
215
- # puts ">>> base url >> #{url}"
216
- signed_url = sign_request(url)
217
- # puts ">>> SIGNED >> #{signed_url}"
218
- signed_url
219
- 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
220
172
 
221
- def self.camelize(s)
222
- s.to_s.gsub(/\/(.?)/) { '::' + Regexp.last_match[1].upcase }.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase }
223
- end
173
+ qs = ""
174
+ opts.each do |k, v|
175
+ next unless v
224
176
 
225
- def self.hmac_sha256(message, key)
226
- block_size = 64
227
- ipad = "\x36" * block_size
228
- opad = "\x5c" * block_size
229
- if key.size > block_size
230
- d = Digest::SHA256.new
231
- 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)
232
182
  end
233
183
 
234
- ipad_bytes = ipad.bytes.map { |b| b }
235
- opad_bytes = opad.bytes.map { |b| b }
236
- key_bytes = key.bytes.map { |b| b }
237
- ipad_xor = ''
238
- opad_xor = ''
239
- for i in 0..key.size - 1
240
- ipad_xor << (ipad_bytes[i] ^ key_bytes[i])
241
- 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 }
242
188
  end
243
189
 
244
- ipad = ipad_xor + ipad[key.size..-1]
245
- 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
246
198
 
247
- # inner hash
248
- d1 = Digest::SHA256.new
249
- d1.update(ipad)
250
- d1.update(message)
251
- 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
252
208
 
253
- # outer hash
254
- d2 = Digest::SHA256.new
255
- d2.update(opad)
256
- d2.update(msg_hash)
257
- d2.digest
258
- end
209
+ ipad = ipad_xor + ipad[key.size..-1]
210
+ opad = opad_xor + opad[key.size..-1]
259
211
 
260
- def self.sign_request(request)
261
- raise AmazonNotConfiguredError unless @@secret_access_key
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]
212
+ # inner hash
213
+ d1 = Digest::SHA256.new
214
+ d1.update(ipad)
215
+ d1.update(message)
216
+ msg_hash = d1.digest
268
217
 
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}"
218
+ # outer hash
219
+ d2 = Digest::SHA256.new
220
+ d2.update(opad)
221
+ d2.update(msg_hash)
222
+ d2.digest
223
+ end
273
224
 
274
- # Step 2 : URL-encode
275
- param_string = param_string.gsub(',', '%2C').gsub(':', '%3A')
276
- # NOTE : take care not to double-encode
225
+ def self.sign_request(request)
226
+ raise AmazonNotConfiguredError unless @@secret_access_key
277
227
 
278
- # Step 3 : Split the parameter/value pairs
279
- params = param_string.split('&')
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]
280
234
 
281
- # Step 4 : Sort params
282
- params.sort!
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}"
283
239
 
284
- # Step 5 : Rejoin the param string
285
- canonical_param_string = params.join('&')
240
+ # Step 2 : URL-encode
241
+ param_string = param_string.gsub(",", "%2C").gsub(":", "%3A")
242
+ # NOTE : take care not to double-encode
286
243
 
287
- # Steps 6 & 7: Prepend HTTP request info
288
- string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
244
+ # Step 3 : Split the parameter/value pairs
245
+ params = param_string.split("&")
289
246
 
290
- # puts string_to_sign
247
+ # Step 4 : Sort params
248
+ params.sort!
291
249
 
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
250
+ # Step 5 : Rejoin the param string
251
+ canonical_param_string = params.join("&")
295
252
 
296
- # Step 9 : URL-encode + and = in sig
297
- base64_sig = CGI.escape(base64_sig)
253
+ # Steps 6 & 7: Prepend HTTP request info
254
+ string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
298
255
 
299
- # Step 10 : Add the URL encoded signature to your request
300
- "http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
301
- end
302
- end
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
303
259
 
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
309
- end
260
+ # Step 9 : URL-encode + and = in sig
261
+ base64_sig = CGI.escape(base64_sig)
310
262
 
311
- # Returns Hpricot::Elments object
312
- def elem
313
- @element
263
+ # Step 10 : Add the URL encoded signature to your request
264
+ "http://#{host}#{path}?#{param_string}&Signature=#{base64_sig}"
265
+ end
314
266
  end
315
267
 
316
- # Find Hpricot::Elements matching the given path. Example: element/"author".
317
- def /(path)
318
- elements = @element / path
319
- return nil if elements.empty?
320
- elements
321
- 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
322
274
 
323
- # Find Hpricot::Elements matching the given path, and convert to Amazon::Element.
324
- # Returns an array Amazon::Elements if more than Hpricot::Elements size is greater than 1.
325
- def search_and_convert(path)
326
- elements = self./(path)
327
- return unless elements
328
- elements = elements.map { |element| Element.new(element) }
329
- return elements.first if elements.size == 1
330
- elements
331
- end
275
+ # Returns Hpricot::Elments object
276
+ def elem
277
+ @element
278
+ end
332
279
 
333
- # Get the text value of the given path, leave empty to retrieve current element value.
334
- def get(path = '')
335
- Element.get(@element, path)
336
- 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?
337
284
 
338
- # Get the unescaped HTML text of the given path.
339
- def get_unescaped(path = '')
340
- Element.get_unescaped(@element, path)
341
- end
285
+ elements
286
+ end
342
287
 
343
- # Get the array values of the given path.
344
- def get_array(path = '')
345
- Element.get_array(@element, path)
346
- end
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
347
294
 
348
- # Get the children element text values in hash format with the element names as the hash keys.
349
- def get_hash(path = '')
350
- Element.get_hash(@element, path)
351
- end
295
+ elements = elements.map { |element| Element.new(element) }
296
+ return elements.first if elements.size == 1
352
297
 
353
- # Similar to #get, except an element object must be passed-in.
354
- def self.get(element, path = '')
355
- return unless element
356
- result = element.at(path)
357
- ## inner_html doesn't decode entities, hence bug #21659
358
- # result = result.inner_html if result
359
- result = result.inner_text if result
360
- result
361
- end
298
+ elements
299
+ end
362
300
 
363
- # Similar to #get_unescaped, except an element object must be passed-in.
364
- def self.get_unescaped(element, path = '')
365
- result = get(element, path)
366
- CGI.unescapeHTML(result) if result
367
- 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
368
305
 
369
- # Similar to #get_array, except an element object must be passed-in.
370
- def self.get_array(element, path = '')
371
- return unless element
372
-
373
- result = element / path
374
- if (result.is_a? Hpricot::Elements) || (result.is_a? Array)
375
- parsed_result = []
376
- result.each { |item|
377
- parsed_result << Element.get(item)
378
- }
379
- parsed_result
380
- else
381
- [Element.get(result)]
306
+ # Get the unescaped HTML text of the given path.
307
+ def get_unescaped(path = "")
308
+ Element.get_unescaped(@element, path)
382
309
  end
383
- end
384
310
 
385
- # Similar to #get_hash, except an element object must be passed-in.
386
- def self.get_hash(element, path = '')
387
- return unless element
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
321
+
322
+ # Similar to #get, except an element object must be passed-in.
323
+ def self.get(element, path = "")
324
+ return unless element
325
+
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
332
+
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
338
+
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
353
+ end
354
+
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
388
359
 
389
- result = element.at(path)
390
- if result
391
360
  hash = {}
392
361
  result = result.children
393
362
  result.each do |item|
@@ -395,10 +364,10 @@ module Amazon
395
364
  end
396
365
  hash
397
366
  end
398
- end
399
367
 
400
- def to_s
401
- elem.to_s if elem
368
+ def to_s
369
+ elem&.to_s
370
+ end
402
371
  end
403
372
  end
404
373
  end