lib/kindle-highlights.rb in kindle-highlights-0.0.3 vs lib/kindle-highlights.rb in kindle-highlights-0.0.4

- old
+ new

@@ -31,18 +31,29 @@ class KindleHighlight::Highlight attr_accessor :annotation_id, :asin, :author, :title, :content + @@amazon_items = Hash.new + def initialize(highlight) self.annotation_id = highlight.xpath("form/input[@id='annotation_id']").attribute("value").value self.asin = highlight.xpath("p/span[@class='hidden asin']").text self.content = highlight.xpath("span[@class='highlight']").text - @request = Request.new - @request.locale = 'us' - @response = ResponseGroup.new('Medium') - lookup = Amazon::AWS::ItemLookup.new('ASIN', {'ItemId' => self.asin, 'MerchantId' => 'Amazon'}) - item = @request.search(lookup, @response).item_lookup_response[0].items.item.first - self.author = item.item_attributes.author.first.to_s - self.title = item.item_attributes.title.first.to_s + amazon_item = lookup_or_get_from_cache(self.asin) + self.author = amazon_item.item_attributes.author.to_s + self.title = amazon_item.item_attributes.title.to_s + end + + def lookup_or_get_from_cache(asin) + unless @@amazon_items.has_key? (asin) + request = Request.new + request.locale = 'us' + response = ResponseGroup.new('Medium') + lookup = Amazon::AWS::ItemLookup.new('ASIN', {'ItemId' => asin, 'MerchantId' => 'Amazon'}) + puts amazon_item = request.search(lookup, response).item_lookup_response[0].items.inspect + amazon_item = request.search(lookup, response).item_lookup_response[0].items.item.first + @@amazon_items[asin] = amazon_item + end + @@amazon_items[asin] end end \ No newline at end of file