Skip to content

Commit

Permalink
when a product is a type of merchandise that is NEVER available in ot…
Browse files Browse the repository at this point in the history
…her colors, no color elements are returned and the colorId on the productImage elements is set to 0... I think. http://developer.cafepress.com/forum/read/106987
  • Loading branch information
benjaminleesmith committed Mar 7, 2011
1 parent ae06c9f commit d82488c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lib/cafepress_api.rb
Expand Up @@ -69,7 +69,9 @@ def self.get_store_products(cafepress_store_id, app_key = ENV['cp_app_key'])

# The CafePress API (at the time of this code) doesn't correctly return
# product images only for the colors which are available for the product.
# as such, I am filtering out "invalid" colors from the productUrl(s)
# as such, I am filtering out "invalid" colors from the productUrl(s).
# Except in the case where the API returns NO colors... then I am accepting
# all images with a color id of "0", whatever that means...
valid_color_ids = []
product.get_elements('color').each do |color|
color_id = color.attributes['id']
Expand All @@ -88,7 +90,7 @@ def self.get_store_products(cafepress_store_id, app_key = ENV['cp_app_key'])
image_urls = []
product.get_elements("productImage").each do |product_image|
# only parse if it is a product image for an available color
if valid_color_ids.include?(product_image.attributes['colorId'])
if valid_color_ids.include?(product_image.attributes['colorId']) || valid_color_ids.length == 0
if product_image.attributes['productUrl'].include?('_Front')
image_urls << {:color_id => product_image.attributes['colorId'], :url => product_image.attributes['productUrl'], :view => FRONT_PRODUCT_VIEW, :size => product_image.attributes['imageSize']}
elsif product_image.attributes['productUrl'].include?('_Back')
Expand All @@ -100,14 +102,13 @@ def self.get_store_products(cafepress_store_id, app_key = ENV['cp_app_key'])
end
end

# for whatever reason, some products don't have a default color
begin
default_color_id = product.get_elements("color[@default='true']").first.attributes['id']
rescue
# For some reason, there are some products without colors...
if valid_color_ids.length == 0
default_color_id = nil
else
default_color_id = product.get_elements("color[@default='true']").first.attributes['id']
end


products << {
:name => product.attributes['name'],
:default_caption => product.attributes['defaultCaption'],
Expand Down
2 changes: 1 addition & 1 deletion lib/cafepress_api/version.rb
Expand Up @@ -15,5 +15,5 @@
# along with CafePressAPI. If not, see <http://www.gnu.org/licenses/>.

module CafePressAPI
VERSION = "0.2.1"
VERSION = "0.2.2"
end

0 comments on commit d82488c

Please sign in to comment.