Skip to content

Commit

Permalink
Refactoring in acts_as_amazon_product.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
dce committed Sep 6, 2008
1 parent 5cd11d1 commit dd4f973
Showing 1 changed file with 8 additions and 32 deletions.
40 changes: 8 additions & 32 deletions lib/acts_as_amazon_product.rb
@@ -1,7 +1,4 @@
# ActsAsAmazonProduct

require 'rubygems'
#require 'active_support'
require 'active_record'
require 'amazon/ecs'
require 'amazon_product'
Expand All @@ -15,7 +12,6 @@ def self.included(base)
end

module ClassMethods

def acts_as_amazon_product(options = {})
defaults = {
:asin => 'asin',
Expand All @@ -36,63 +32,43 @@ def acts_as_amazon_product(options = {})

has_one :amazon_product, :as => :amazonable #, :dependent => :delete
include Netphase::Acts::Amazonable::InstanceMethods
extend Netphase::Acts::Amazonable::SingletonMethods
end

end

# This module contains class methods
module SingletonMethods

end

# This module contains instance methods
module InstanceMethods

def amazon
if self.amazon_product.nil?
asin = (self.respond_to?('amazon_asin')) ? self.send(self.amazon_asin) : nil
name = (self.respond_to?('amazon_name')) ? self.send(self.amazon_name) : nil
search_index = (self.respond_to?('amazon_search_index')) ? self.amazon_search_index : 'Books'
asin = self.send(self.amazon_asin)
name = self.send(self.amazon_name)

if !asin.nil? && asin.length > 0
# puts "Looking up #{asin}"
if !asin.blank?
options = { :response_group => 'Medium' }
unless self.amazon_asin == 'asin'
options[:id_type] = self.amazon_asin.upcase
options[:search_index] = self.amazon_search_index
end
res = Amazon::Ecs.item_lookup(self.send(self.amazon_asin), options)

self.amazon_product =
AmazonProduct.new(:xml => res.doc.to_html, :asin => res.doc.at('asin').inner_html)
self.amazon_product.save
elsif !name.nil? && name.length > 0
# puts "Searching for #{name}"
res = Amazon::Ecs.item_search(self.send(self.amazon_name), :search_index => self.amazon_search_index, :response_group => 'Medium') #, :sort => 'salesrank'
self.create_amazon_product(:xml => res.doc.to_html, :asin => res.doc.at('asin').inner_html)
elsif !name.blank?
res = Amazon::Ecs.item_search(self.send(self.amazon_name), :search_index => self.amazon_search_index, :response_group => 'Medium')
res = res.doc.at('items/item')
self.amazon_product =
AmazonProduct.new(:xml => res.to_html, :asin => (res.at('itemattributes/isbn').nil? ? res.at('asin').inner_html : res.at('itemattributes/isbn').inner_html))
self.amazon_product.save
self.create_amazon_product(:xml => res.to_html,
:asin => (res.at('itemattributes/isbn').nil? ? res.at('asin').inner_html : res.at('itemattributes/isbn').inner_html))
else
logger.error "No known attributes to search by"
end
end
self.amazon_product
end

#def method_missing(method, *args)
#end

def after_save
unless self.amazon_product.nil?
self.amazon_product.destroy
self.reload
end
end

end

end
end
end
Expand Down

0 comments on commit dd4f973

Please sign in to comment.