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.
- checksums.yaml +5 -5
- data/.github/dependabot.yml +9 -0
- data/.gitignore +5 -2
- data/.hound.yml +2 -0
- data/.rubocop.yml +113 -45
- data/.rubocop_todo.yml +82 -170
- data/.simplecov +5 -1
- data/.travis.yml +45 -0
- data/.yardopts +1 -1
- data/CHANGELOG.md +60 -0
- data/ChangeLog.0 +33 -35
- data/Gemfile +6 -5
- data/INSTALL.md +164 -0
- data/README.md +52 -42
- data/Rakefile +95 -109
- data/TODO.md +9 -1
- data/alexandria-book-collection-manager.gemspec +52 -45
- data/bin/alexandria +31 -53
- data/doc/AUTHORS +61 -0
- data/doc/BUGS +31 -0
- data/doc/FAQ +365 -0
- data/doc/HACKING +19 -0
- data/doc/NEWS +341 -0
- data/doc/alexandria.1 +120 -0
- data/doc/cuecat_support.rdoc +67 -0
- data/doc/dependency_decisions.yml +80 -0
- data/lib/alexandria.rb +29 -37
- data/lib/alexandria/about.rb +52 -51
- data/lib/alexandria/book_providers.rb +94 -101
- data/lib/alexandria/book_providers/adlibris.rb +45 -85
- data/lib/alexandria/book_providers/amazon_aws.rb +105 -113
- data/lib/alexandria/book_providers/amazon_ecs_util.rb +293 -324
- data/lib/alexandria/book_providers/barnes_and_noble.rb +54 -53
- data/lib/alexandria/book_providers/douban.rb +29 -51
- data/lib/alexandria/book_providers/proxis.rb +42 -59
- data/lib/alexandria/book_providers/pseudomarc.rb +79 -99
- data/lib/alexandria/book_providers/siciliano.rb +68 -70
- data/lib/alexandria/book_providers/thalia.rb +46 -45
- data/lib/alexandria/book_providers/web.rb +17 -33
- data/lib/alexandria/book_providers/worldcat.rb +74 -102
- data/lib/alexandria/book_providers/z3950.rb +170 -174
- data/lib/alexandria/config.rb +5 -3
- data/lib/alexandria/console.rb +10 -21
- data/lib/alexandria/default_preferences.rb +37 -0
- data/lib/alexandria/execution_queue.rb +17 -15
- data/lib/alexandria/export_format.rb +47 -0
- data/lib/alexandria/export_library.rb +188 -302
- data/lib/alexandria/import_library.rb +114 -155
- data/lib/alexandria/import_library_csv.rb +46 -96
- data/lib/alexandria/library_collection.rb +79 -0
- data/lib/alexandria/library_sort_order.rb +45 -0
- data/lib/alexandria/library_store.rb +233 -0
- data/lib/alexandria/logging.rb +15 -19
- data/lib/alexandria/models/book.rb +15 -20
- data/lib/alexandria/models/library.rb +81 -363
- data/lib/alexandria/net.rb +7 -6
- data/lib/alexandria/preferences.rb +73 -91
- data/lib/alexandria/scanners.rb +4 -2
- data/lib/alexandria/scanners/{cuecat.rb → cue_cat.rb} +24 -20
- data/lib/alexandria/scanners/keyboard.rb +10 -8
- data/lib/alexandria/smart_library.rb +135 -171
- data/lib/alexandria/ui.rb +17 -15
- data/lib/alexandria/ui/about_dialog.rb +49 -0
- data/lib/alexandria/ui/{dialogs/acquire_dialog.rb → acquire_dialog.rb} +129 -152
- data/lib/alexandria/ui/alert_dialog.rb +64 -0
- data/lib/alexandria/ui/bad_isbns_dialog.rb +41 -0
- data/lib/alexandria/ui/{dialogs/barcode_animation.rb → barcode_animation.rb} +18 -15
- data/lib/alexandria/ui/{dialogs/book_properties_dialog.rb → book_properties_dialog.rb} +44 -61
- data/lib/alexandria/ui/{dialogs/book_properties_dialog_base.rb → book_properties_dialog_base.rb} +84 -89
- data/lib/alexandria/ui/builder_base.rb +9 -27
- data/lib/alexandria/ui/callbacks.rb +188 -186
- data/lib/alexandria/ui/columns.rb +2 -0
- data/lib/alexandria/ui/completion_models.rb +12 -23
- data/lib/alexandria/ui/confirm_erase_dialog.rb +33 -0
- data/lib/alexandria/ui/conflict_while_copying_dialog.rb +34 -0
- data/lib/alexandria/ui/dndable.rb +10 -8
- data/lib/alexandria/ui/error_dialog.rb +25 -0
- data/lib/alexandria/ui/export_dialog.rb +139 -0
- data/lib/alexandria/ui/icons.rb +49 -65
- data/lib/alexandria/ui/iconview.rb +15 -13
- data/lib/alexandria/ui/iconview_tooltips.rb +43 -58
- data/lib/alexandria/ui/import_dialog.rb +157 -0
- data/lib/alexandria/ui/init.rb +23 -33
- data/lib/alexandria/ui/keep_bad_isbn_dialog.rb +36 -0
- data/lib/alexandria/ui/libraries_combo.rb +18 -14
- data/lib/alexandria/ui/listview.rb +77 -88
- data/lib/alexandria/ui/main_app.rb +26 -26
- data/lib/alexandria/ui/misc_dialogs.rb +10 -0
- data/lib/alexandria/ui/multi_drag_treeview.rb +30 -41
- data/lib/alexandria/ui/{dialogs/new_book_dialog.rb → new_book_dialog.rb} +168 -215
- data/lib/alexandria/ui/new_book_dialog_manual.rb +139 -0
- data/lib/alexandria/ui/new_provider_dialog.rb +100 -0
- data/lib/alexandria/ui/new_smart_library_dialog.rb +74 -0
- data/lib/alexandria/ui/preferences_dialog.rb +313 -0
- data/lib/alexandria/ui/provider_preferences_base_dialog.rb +95 -0
- data/lib/alexandria/ui/provider_preferences_dialog.rb +35 -0
- data/lib/alexandria/ui/really_delete_dialog.rb +53 -0
- data/lib/alexandria/ui/{sidepane.rb → sidepane_manager.rb} +62 -72
- data/lib/alexandria/ui/skip_entry_dialog.rb +33 -0
- data/lib/alexandria/ui/smart_library_properties_dialog.rb +60 -0
- data/lib/alexandria/ui/{dialogs/smart_library_properties_dialog_base.rb → smart_library_properties_dialog_base.rb} +96 -172
- data/lib/alexandria/ui/smart_library_rule_box.rb +119 -0
- data/lib/alexandria/ui/sound.rb +13 -13
- data/lib/alexandria/ui/ui_manager.rb +262 -283
- data/lib/alexandria/undo_manager.rb +3 -0
- data/lib/alexandria/version.rb +6 -19
- data/lib/alexandria/web_themes.rb +24 -21
- data/po/Makefile +2 -2
- data/po/cs.po +993 -880
- data/po/cy.po +957 -874
- data/po/de.po +990 -869
- data/po/el.po +989 -869
- data/po/es.po +985 -865
- data/po/fr.po +986 -870
- data/po/ga.po +907 -823
- data/po/gl.po +981 -865
- data/po/it.po +986 -868
- data/po/ja.po +969 -853
- data/po/mk.po +983 -863
- data/po/nb.po +979 -863
- data/po/nl.po +983 -864
- data/po/pl.po +1020 -969
- data/po/pt.po +988 -861
- data/po/pt_BR.po +984 -868
- data/po/ru.po +992 -873
- data/po/sk.po +987 -869
- data/po/sv.po +977 -861
- data/po/uk.po +975 -865
- data/po/zh_TW.po +976 -860
- data/schemas/alexandria.schemas +25 -3
- data/share/alexandria/glade/acquire_dialog__builder.glade +15 -12
- data/share/alexandria/glade/book_properties_dialog__builder.glade +171 -299
- data/share/alexandria/glade/main_app__builder.glade +24 -33
- data/share/alexandria/glade/new_book_dialog__builder.glade +27 -59
- data/share/alexandria/glade/preferences_dialog__builder.glade +250 -290
- data/share/gnome/help/alexandria/C/introduction.xml +0 -8
- data/share/gnome/help/alexandria/C/searching.xml +1 -1
- data/share/gnome/help/alexandria/C/smart-libraries.xml +2 -2
- data/share/gnome/help/alexandria/C/working-with-libraries.xml +1 -1
- data/share/gnome/help/alexandria/fr/alexandria.xml +1 -1
- data/share/gnome/help/alexandria/ja/introduction.xml +0 -8
- data/share/gnome/help/alexandria/ja/smart-libraries.xml +1 -1
- data/spec/alexandria/book_providers/world_cat_provider_spec.rb +160 -0
- data/spec/alexandria/book_providers_spec.rb +77 -210
- data/spec/alexandria/book_spec.rb +16 -12
- data/spec/alexandria/console_spec.rb +27 -0
- data/spec/alexandria/export_library_spec.rb +130 -0
- data/spec/alexandria/library_spec.rb +130 -172
- data/spec/alexandria/library_store_spec.rb +37 -0
- data/spec/alexandria/preferences_spec.rb +46 -17
- data/spec/alexandria/scanners/cue_cat_spec.rb +52 -0
- data/spec/alexandria/smart_library_spec.rb +32 -25
- data/spec/alexandria/ui/about_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/acquire_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/alert_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/bad_isbns_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/book_properties_dialog_spec.rb +17 -0
- data/spec/alexandria/ui/confirm_erase_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/conflict_while_copying_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/error_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/export_dialog_spec.rb +15 -0
- data/spec/alexandria/ui/icons_spec.rb +26 -0
- data/spec/alexandria/ui/iconview_spec.rb +9 -21
- data/spec/alexandria/ui/import_dialog_spec.rb +41 -0
- data/spec/alexandria/ui/keep_bad_isbn_dialog_spec.rb +17 -0
- data/spec/alexandria/ui/main_app_spec.rb +8 -33
- data/spec/alexandria/ui/new_book_dialog_manual_spec.rb +15 -0
- data/spec/alexandria/ui/new_book_dialog_spec.rb +22 -0
- data/spec/alexandria/ui/new_provider_dialog_spec.rb +30 -0
- data/spec/alexandria/ui/new_smart_library_dialog_spec.rb +39 -0
- data/spec/alexandria/ui/preferences_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/provider_preferences_dialog_spec.rb +34 -0
- data/spec/alexandria/ui/really_delete_dialog_spec.rb +16 -0
- data/spec/alexandria/ui/sidepane_manager_spec.rb +15 -0
- data/spec/alexandria/ui/skip_entry_dialog_spec.rb +14 -0
- data/spec/alexandria/ui/smart_library_properties_dialog_spec.rb +32 -0
- data/spec/alexandria/ui/sound_spec.rb +4 -2
- data/spec/alexandria/ui/ui_manager_spec.rb +45 -20
- data/spec/end_to_end/basic_run_spec.rb +57 -0
- data/spec/spec_helper.rb +66 -33
- data/tasks/setup.rb +5 -3
- data/tasks/spec.rake +18 -3
- data/util/rake/fileinstall.rb +38 -40
- data/util/rake/gettextgenerate.rb +15 -70
- data/util/rake/omfgenerate.rb +10 -10
- metadata +176 -60
- data/INSTALL.rdoc +0 -148
- data/dogtail/basic_run_test.py +0 -9
- data/lib/alexandria/book_providers/bol_it.rb +0 -160
- data/lib/alexandria/book_providers/deastore.rb +0 -273
- data/lib/alexandria/book_providers/ibs_it.rb +0 -147
- data/lib/alexandria/book_providers/mcu.rb +0 -169
- data/lib/alexandria/book_providers/renaud.rb +0 -140
- data/lib/alexandria/book_providers/webster_it.rb +0 -167
- data/lib/alexandria/ui/dialogs/about_dialog.rb +0 -59
- data/lib/alexandria/ui/dialogs/alert_dialog.rb +0 -70
- data/lib/alexandria/ui/dialogs/bad_isbns_dialog.rb +0 -43
- data/lib/alexandria/ui/dialogs/export_dialog.rb +0 -171
- data/lib/alexandria/ui/dialogs/import_dialog.rb +0 -196
- data/lib/alexandria/ui/dialogs/misc_dialogs.rb +0 -85
- data/lib/alexandria/ui/dialogs/new_book_dialog_manual.rb +0 -154
- data/lib/alexandria/ui/dialogs/new_smart_library_dialog.rb +0 -74
- data/lib/alexandria/ui/dialogs/preferences_dialog.rb +0 -578
- data/lib/alexandria/ui/dialogs/smart_library_properties_dialog.rb +0 -57
- data/spec/alexandria/scanners/cuecat_spec.rb +0 -65
- data/spec/alexandria/ui/dialogs_spec.rb +0 -94
- data/spec/alexandria/ui/sidepane_spec.rb +0 -27
- data/spec/alexandria/ui/ui_utilities_spec.rb +0 -60
- data/spec/alexandria/utilities_spec.rb +0 -50
- data/tasks/dogtail.rake +0 -4
@@ -1,393 +1,362 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
#
|
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
|
-
|
61
|
-
|
62
|
-
|
7
|
+
require "net/http"
|
8
|
+
require "hpricot"
|
9
|
+
require "cgi"
|
63
10
|
|
64
|
-
|
65
|
-
def self.options=(opts)
|
66
|
-
@@options = opts
|
67
|
-
end
|
11
|
+
require "digest/sha2"
|
68
12
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
end
|
13
|
+
module Alexandria
|
14
|
+
module Amazon
|
15
|
+
class RequestError < StandardError; end
|
73
16
|
|
74
|
-
|
75
|
-
|
76
|
-
@@debug = dbg
|
77
|
-
end
|
17
|
+
class Ecs
|
18
|
+
include Logging
|
78
19
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
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
|
-
|
85
|
-
|
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
|
-
|
98
|
-
end
|
32
|
+
@@secret_access_key = ""
|
99
33
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
34
|
+
# Default search options
|
35
|
+
def self.options
|
36
|
+
@@options
|
37
|
+
end
|
104
38
|
|
105
|
-
|
106
|
-
|
39
|
+
def self.secret_access_key=(key)
|
40
|
+
@@secret_access_key = key
|
41
|
+
end
|
107
42
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
end
|
43
|
+
# Set default search options
|
44
|
+
def self.options=(opts)
|
45
|
+
@@options = opts
|
46
|
+
end
|
113
47
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
log.debug { "Request URL: #{request_url}" }
|
48
|
+
# Get debug flag.
|
49
|
+
def self.debug
|
50
|
+
@@debug
|
51
|
+
end
|
119
52
|
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
128
|
-
|
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
|
-
#
|
135
|
-
|
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
|
-
|
138
|
-
def is_valid_request?
|
139
|
-
(@doc / 'isvalid').inner_html == 'True'
|
76
|
+
send_request(opts)
|
140
77
|
end
|
141
78
|
|
142
|
-
#
|
143
|
-
def
|
144
|
-
|
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
|
-
#
|
148
|
-
def
|
149
|
-
|
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
|
-
#
|
153
|
-
|
154
|
-
|
155
|
-
|
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
|
-
|
161
|
-
def first_item
|
162
|
-
items.first
|
106
|
+
Response.new(res.body)
|
163
107
|
end
|
164
108
|
|
165
|
-
#
|
166
|
-
|
167
|
-
|
168
|
-
|
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
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
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
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
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
|
-
|
222
|
-
|
223
|
-
|
173
|
+
qs = ""
|
174
|
+
opts.each do |k, v|
|
175
|
+
next unless v
|
224
176
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
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
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
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
|
-
|
245
|
-
|
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
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
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
|
-
|
254
|
-
|
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
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
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
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
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
|
-
|
275
|
-
|
276
|
-
# NOTE : take care not to double-encode
|
225
|
+
def self.sign_request(request)
|
226
|
+
raise AmazonNotConfiguredError unless @@secret_access_key
|
277
227
|
|
278
|
-
|
279
|
-
|
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
|
-
|
282
|
-
|
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
|
-
|
285
|
-
|
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
|
-
|
288
|
-
|
244
|
+
# Step 3 : Split the parameter/value pairs
|
245
|
+
params = param_string.split("&")
|
289
246
|
|
290
|
-
|
247
|
+
# Step 4 : Sort params
|
248
|
+
params.sort!
|
291
249
|
|
292
|
-
|
293
|
-
|
294
|
-
base64_sig = [sig].pack('m').strip
|
250
|
+
# Step 5 : Rejoin the param string
|
251
|
+
canonical_param_string = params.join("&")
|
295
252
|
|
296
|
-
|
297
|
-
|
253
|
+
# Steps 6 & 7: Prepend HTTP request info
|
254
|
+
string_to_sign = "GET\n#{host}\n#{path}\n#{canonical_param_string}"
|
298
255
|
|
299
|
-
|
300
|
-
|
301
|
-
|
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
|
-
|
305
|
-
|
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
|
-
|
312
|
-
|
313
|
-
|
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
|
-
#
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
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
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
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
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
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
|
-
|
339
|
-
|
340
|
-
Element.get_unescaped(@element, path)
|
341
|
-
end
|
285
|
+
elements
|
286
|
+
end
|
342
287
|
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
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
|
-
|
349
|
-
|
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
|
-
|
354
|
-
|
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
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
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
|
-
|
370
|
-
|
371
|
-
|
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
|
-
|
386
|
-
|
387
|
-
|
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
|
-
|
401
|
-
|
368
|
+
def to_s
|
369
|
+
elem&.to_s
|
370
|
+
end
|
402
371
|
end
|
403
372
|
end
|
404
373
|
end
|