Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't hit API again when Images already included with Listing
  • Loading branch information
magni- committed Mar 25, 2017
1 parent ecdad91 commit 98f649a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/etsy/listing.rb
Expand Up @@ -125,7 +125,7 @@ def self.find_all_active_by_category(category, options = {})
# The collection of images associated with this listing.
#
def images
@images ||= Image.find_all_by_listing_id(id, oauth)
@images ||= listing_images
end

# The primary image for this listing.
Expand Down Expand Up @@ -275,5 +275,13 @@ def self.bought_listings(user_id, options = {})
def oauth
oauth = (token && secret) ? {:access_token => token, :access_secret => secret} : {}
end

def listing_images
if result && result["Images"]
result["Images"].map { |hash| Image.new(hash) }
else
Image.find_all_by_listing_id(id, oauth)
end
end
end
end
1 change: 1 addition & 0 deletions test/fixtures/listing/getListing.single.includeImages.json
@@ -0,0 +1 @@
{"count":1,"results":[{"listing_id":59759273,"state":"active","user_id":7301949,"title":"Vinyl Wall Art Decal -- Multi-colored Flowers Set","description":"The listing is for a beautiful set of multi-colored flowers, colors as shown in the picture.\r\n\r\nExclusive design only from OrqueShaw's walldecors! you won't find this anywhere else on etsy!\r\n\r\nHeights of the 4 large flowers: 52in\/37in\/29in\/26in.\r\n\r\nAll of our decals are computer die-cut, there are no "edges" nor "backgrounds", all you will get is the lovely image! and after applying they will look like they are stenciled on with paint!\r\n\r\nuggested install areas: Walls, Mirrors, Metals, Woods, Plastic...Lightly textured surfaces would be just fine! \r\n\r\n\r\n\r\n\r\nABOUT US AND OUR BEAUTIFUL DECALS:\r\n\r\nWe want to transform your walls....and your lives.\r\nDid you know that you can save literally hundreds of dollars designing your own interior space? Seriously, got bored of painting walls,using stencils, applying borders, and putting up wallpapers?? Using our super convenient n' simply gorgeous wall art decals is a very cool and a great way to make a room shine (or to change a room) w\/out damaging your walls or the bank! great alternative to wall paper or a head board, and without any of the mess and hassle involved with painting. Especially nice if you rent and cannot paint your walls, You can even take your wall decals to work and redesign your office or cubical. Easy to apply even for the creative or somewhat domestically challenged . We carry a variety of colors, styles and themes which you could use to decorate every room for anyone in the family!\r\n\r\nOur wall art decals are hand made, they are easy and fun to install.These wall stickers affix on most smooth surfaces and can be removed without leaving any residues. Complete and simple step-by-step instructions are included with your order. \r\n\r\n\r\nSHIPPING:\r\n\r\n*We combine shipping for multiple purchases!\r\n\r\n*Orders usually ship within 2 business days after the clear of payment. \r\n\r\n*All of our shipments are packed in 3 ply spiral wound mailing tubes to ensure the maximum protection during deliveries.\r\n\r\n*Shipping rates: $9.75 for US and Canada $16.75 for other countries.\r\n\r\n*2-7 business days for shipping in Canada, 6-12 business days for shipping to US and other countries, we are not responsible for the delays caused by the custom clearance.\r\n\r\n*Faster delivery options are available upon request.\r\n\r\n*We ship worldwide except the following places: Africa, China, South & North Korea, Italy, Republic Of Serbia, Turkmenistan and Republic Of Montenegro.\r\n\r\n\r\nPAYMENT:\r\n\r\n*Please remit payment within 3 days. Your product will not be shipped until payment is received. Thank you!\r\n\r\n*If you are not 100% satisfied with your purchase, please return the item in its original condition(unopened) within 30 days of delivery of your shipment. Please note that we cannot accept the returns of opened items or items returned more than 30 days past delivery, shipping cost is not refundable and buyers are responsible for the return shipping fees at any case. \r\n","creation_tsz":1288738828,"ending_tsz":1299042000,"original_creation_tsz":1287867280,"last_modified_tsz":1288738828,"price":"42.00","currency_code":"USD","quantity":1,"tags":["children","wallpaper_graphic","nursery_playroom","red_orange_pink","green_lime_color","furniture","office_room","bedroom_bathroom","nature_garden","girl_boy_baby","everything_else","cheersandtears","front_page","soft_colors"],"materials":["self_adhesive_vinyl","decal_transfer_paper"],"shop_section_id":6489554,"featured_rank":null,"state_tsz":1288738828,"hue":0,"saturation":0,"brightness":100,"is_black_and_white":false,"url":"http:\/\/www.etsy.com\/listing\/59759273\/vinyl-wall-art-decal-multi-colored","views":178,"num_favorers":5,"Images":[{"listing_image_id":1132761, "hex_code":"6B6868", "red":107, "green":104, "blue":104, "hue":0, "saturation":2, "brightness":41, "is_black_and_white":false, "creation_tsz":1485797197, "listing_id":59759273, "rank":1, "url_75x75":"https://img0.etsystatic.com/176/0/12065520/il_75x75.1132761110_kggi.jpg", "url_170x135":"https://img0.etsystatic.com/176/0/12065520/il_170x135.1132761110_kggi.jpg", "url_570xN":"https://img0.etsystatic.com/176/0/12065520/il_570xN.1132761110_kggi.jpg", "url_fullxfull":"https://img0.etsystatic.com/176/0/12065520/il_fullxfull.1132761110_kggi.jpg", "full_height":540, "full_width":720}, {"listing_image_id":1179359, "hex_code":"6E6A6B", "red":110, "green":106, "blue":107, "hue":345, "saturation":3, "brightness":43, "is_black_and_white":false, "creation_tsz":1485797197, "listing_id":59759273, "rank":2, "url_75x75":"https://img1.etsystatic.com/155/0/12065520/il_75x75.1179359629_mhmc.jpg", "url_170x135":"https://img1.etsystatic.com/155/0/12065520/il_170x135.1179359629_mhmc.jpg", "url_570xN":"https://img1.etsystatic.com/155/0/12065520/il_570xN.1179359629_mhmc.jpg", "url_fullxfull":"https://img1.etsystatic.com/155/0/12065520/il_fullxfull.1179359629_mhmc.jpg", "full_height":540, "full_width":720}, {"listing_image_id":1179359, "hex_code":"6C6869", "red":108, "green":104, "blue":105, "hue":345, "saturation":3, "brightness":42, "is_black_and_white":false, "creation_tsz":1485797197, "listing_id":59759273, "rank":3, "url_75x75":"https://img1.etsystatic.com/144/0/12065520/il_75x75.1179359767_d5qd.jpg", "url_170x135":"https://img1.etsystatic.com/144/0/12065520/il_170x135.1179359767_d5qd.jpg", "url_570xN":"https://img1.etsystatic.com/144/0/12065520/il_570xN.1179359767_d5qd.jpg", "url_fullxfull":"https://img1.etsystatic.com/144/0/12065520/il_fullxfull.1179359767_d5qd.jpg", "full_height":540, "full_width":720}, {"listing_image_id":1132761, "hex_code":"6D6A6B", "red":109, "green":106, "blue":107, "hue":340, "saturation":2, "brightness":42, "is_black_and_white":false, "creation_tsz":1485797197, "listing_id":59759273, "rank":4, "url_75x75":"https://img0.etsystatic.com/169/0/12065520/il_75x75.1132761176_n7b7.jpg", "url_170x135":"https://img0.etsystatic.com/169/0/12065520/il_170x135.1132761176_n7b7.jpg", "url_570xN":"https://img0.etsystatic.com/169/0/12065520/il_570xN.1132761176_n7b7.jpg", "url_fullxfull":"https://img0.etsystatic.com/169/0/12065520/il_fullxfull.1132761176_n7b7.jpg", "full_height":540, "full_width":720}, {"listing_image_id":1179359, "hex_code":"6B6868", "red":107, "green":104, "blue":104, "hue":0, "saturation":2, "brightness":41, "is_black_and_white":false, "creation_tsz":1485797197, "listing_id":59759273, "rank":5, "url_75x75":"https://img1.etsystatic.com/169/0/12065520/il_75x75.1179359817_215n.jpg", "url_170x135":"https://img1.etsystatic.com/169/0/12065520/il_170x135.1179359817_215n.jpg", "url_570xN":"https://img1.etsystatic.com/169/0/12065520/il_570xN.1179359817_215n.jpg", "url_fullxfull":"https://img1.etsystatic.com/169/0/12065520/il_fullxfull.1179359817_215n.jpg", "full_height":540, "full_width":720}]}],"params":{"listing_id":"59759273", "includes":"Images"},"type":"Listing"}
9 changes: 9 additions & 0 deletions test/unit/etsy/listing_test.rb
Expand Up @@ -230,6 +230,15 @@ class ListingTest < Test::Unit::TestCase
end
end

context "with included images" do
should "not hit the API to get images" do
data = read_fixture('listing/getListing.single.includeImages.json')
listing = Listing.new(data.first)
Request.expects(:get).never

listing.images
end
end

should "have a default image" do
listing = Listing.new
Expand Down

0 comments on commit 98f649a

Please sign in to comment.