C0 code coverage information
Generated on Tue Oct 16 11:40:47 -0400 2007 with rcov 0.8.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Copyright (C) 2004-2006 Laurent Sansonetti
2 #
3 # Alexandria is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as
5 # published by the Free Software Foundation; either version 2 of the
6 # License, or (at your option) any later version.
7 #
8 # Alexandria is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public
14 # License along with Alexandria; see the file COPYING. If not,
15 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17
18 # http://en.wikipedia.org/wiki/Amazon
19
20 require 'amazon/search'
21
22 module Alexandria
23 class BookProviders
24 class AmazonProvider < GenericProvider
25 include GetText
26 GetText.bindtextdomain(Alexandria::TEXTDOMAIN, nil, nil, "UTF-8")
27
28 CACHE_DIR = File.join(Alexandria::Library::DIR, '.amazon_cache')
29
30 def initialize
31 super("Amazon", "Amazon (Usa)")
32 prefs.add("locale", _("Locale"), "us",
33 Amazon::Search::LOCALES.keys)
34 prefs.add("dev_token", _("Development token"),
35 "142TF8CHT48WYPPS6J82")
36 prefs.add("associate", _("Associate ID"), "calibanorg-20", nil,
37 false)
38
39 # Backward compatibility hack - the previous developer token has
40 # been revoked.
41 prefs.read
42 token = prefs.variable_named("dev_token")
43 if token and token.value == "D23XFCO2UKJY82"
44 token.new_value = "142TF8CHT48WYPPS6J82"
45 end
46 end
47
48 def search(criterion, type)
49 prefs.read
50
51 if config = Alexandria::Preferences.instance.http_proxy_config
52 host, port, user, pass = config
53 url = "http://"
54 url += user + ":" + pass + "@" if user and pass
55 url += host + ":" + port.to_s
56 ENV['http_proxy'] = url
57 end
58
59 req = Amazon::Search::Request.new(prefs["dev_token"])
60 req.cache = Amazon::Search::Cache.new(CACHE_DIR)
61 locales = Amazon::Search::LOCALES.keys
62 locales.delete prefs["locale"]
63 locales.unshift prefs["locale"]
64 locales.reverse!
65
66 begin
67 req.locale = locales.pop
68 products = []
69 case type
70 when SEARCH_BY_ISBN
71 criterion = Library.canonicalise_isbn(criterion)
72 req.asin_search(criterion) do |product|
73 products << product
74 end
75 # shouldn't happen
76 raise TooManyResultsError if products.length > 1
77
78 when SEARCH_BY_TITLE
79 req.keyword_search(criterion) do |product|
80 if /#{criterion}/i.match(product.product_name)
81 products << product
82 end
83 end
84
85 when SEARCH_BY_AUTHORS
86 req.author_search(criterion) do |product|
87 products << product
88 end
89
90 when SEARCH_BY_KEYWORD
91 req.keyword_search(criterion) do |product|
92 products << product
93 end
94
95 else
96 raise InvalidSearchTypeError
97 end
98 raise NoResultsError if products.empty?
99 rescue Amazon::Search::Request::SearchError
100 retry unless locales.empty?
101 raise NoResultsError
102 end
103
104 results = []
105 products.each do |product|
106 next unless product.catalog == 'Book'
107 title = product.product_name.squeeze(' ')
108
109 # Work around Amazon US encoding bug. Amazon US apparently
110 # interprets UTF-8 titles as ISO-8859 titles and then converts
111 # the garbled titles to UTF-8. This tries to convert back into
112 # valid UTF-8. It does not always work - see isbn 2259196098
113 # (from the mailing list) for an example.
114 #if req.locale == 'us'
115 # title = title.convert('ISO-8859-1','UTF-8')
116 #end
117
118 media = product.media.squeeze(' ')
119 media = nil if media == 'Unknown Binding'
120
121 isbn = product.isbn.squeeze(' ')
122 if Library.valid_isbn?(isbn)
123 isbn = Library.canonicalise_ean(isbn)
124 else
125 isbn = nil # it may be an ASIN which is not an ISBN
126 end
127 # hack, extract year by regexp (not Y10K compatible :-)
128 /([1-9][0-9]{3})/ =~ product.release_date
129 publishing_year = $1 ? $1.to_i : nil
130 book = Book.new(title,
131 (product.authors.map { |x| x.squeeze(' ') } \
132 rescue [ ]),
133 isbn,
134 (product.manufacturer.squeeze(' ') \
135 rescue nil),
136 publishing_year,
137 media)
138
139 results << [ book, product.image_url_medium ]
140 end
141 type == SEARCH_BY_ISBN ? results.first : results
142 end
143
144 def url(book)
145 url = case prefs["locale"]
146 when "fr"
147 "http://www.amazon.fr/exec/obidos/ASIN/%s"
148 when "uk"
149 "http://www.amazon.co.uk/exec/obidos/ASIN/%s"
150 when "de"
151 "http://www.amazon.de/exec/obidos/ASIN/%s"
152 when "ca"
153 "http://www.amazon.ca/exec/obidos/ASIN/%s"
154 when "jp"
155 "http://www.amazon.jp/exec/obidos/ASIN/%s"
156 when "us"
157 "http://www.amazon.com/exec/obidos/ASIN/%s"
158 end
159 url % Library.canonicalise_isbn(book.isbn)
160 end
161 end
162 end
163 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.