Skip to content

Commit 874297f

Browse files
authored
CUR-19507 Adding batch_photo_delete to Listing (#128)
* CUR-19507 Add batch photo delete. * Small tweaks to work with the finished API. And bumping the version number. * Add newline to end of .gitignore * Fixing batch_photo_delete Expect a string of IDs instead of an array. * Addressing PR feedback * Added a unit test * Updated listing.rb to have a safe and dangerous version of batch_photo_delete * Bumped version number one more. * Fixing up more PR feedback. * Updated naming from batch_photo_delete to delete_photos * Updated the dangerous delete_photos to return true. * Updated unit test to actually assert something. * DRYing things up a bit * Picking nits.
1 parent 87248eb commit 874297f

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.10
1+
1.4.11

lib/spark_api/models/listing.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def reorder_photos(arguments={})
188188
false
189189
end
190190
def reorder_photos!(arguments={})
191-
results = connection.put "#{self.class.path}/#{self.Id}/photos", arguments
191+
results = connection.put subresource_path("photos"), arguments
192192
true
193193
end
194194

@@ -207,7 +207,7 @@ def reorder_photo(photo_id, index)
207207
false
208208
end
209209
def reorder_photo!(photo_id, index)
210-
connection.put "#{self.class.path}/#{self.Id}/photos/#{photo_id}", "Photos" => [{"Order"=>index}]
210+
connection.put subresource_path("photos") + "/#{photo_id}", "Photos" => [{"Order"=>index}]
211211
true
212212
end
213213

@@ -235,6 +235,26 @@ def respond_to?(method_symbol, include_all=false)
235235
end
236236
end
237237

238+
def delete_photos!(photoIds, args={})
239+
connection.delete subresource_path("photos") + "/#{photoIds}", args
240+
true
241+
end
242+
243+
def delete_photos(photoIds, args={})
244+
unless photoIds.is_a? String
245+
raise ArgumentError, "Batch photo delete failed. '#{photoIds}' is not a string."
246+
end
247+
248+
begin
249+
return delete_photos!(photoIds, args)
250+
rescue BadResourceRequest => e
251+
SparkApi.logger.warn { "Failed to delete photos from resource #{self}: #{e.message}" }
252+
rescue NotFound => e
253+
SparkApi.logger.error { "Failed to delete photos from resource #{self}: #{e.message}" }
254+
end
255+
false
256+
end
257+
238258
private
239259

240260
# TODO trim this down so we're only overriding the StandardFields access
@@ -272,6 +292,10 @@ def setup_attribute(attributes, collection, klass)
272292
end
273293
end
274294

295+
def subresource_path(subresource)
296+
"#{self.class.path}/#{self.Id}/#{subresource}"
297+
end
298+
275299
end
276300
end
277301
end

spec/unit/spark_api/models/listing_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@
351351
l.editable?(:PriceChange).should eq(true)
352352
l.editable?(:Photos).should eq(false)
353353
end
354+
355+
on_delete_it "should bulk delete listing photos" do
356+
list_id = "20060725224713296297000000"
357+
stub_api_get("/listings/#{list_id}", 'listings/with_photos.json', { :_expand => "Photos" })
358+
l = Listing.find(list_id, :_expand => "Photos")
359+
photo_id1 = l.photos[0].Id
360+
photo_id2 = l.photos[1].Id
361+
stub_api_delete("/listings/#{list_id}/photos/#{photo_id1},#{photo_id2}", 'success.json')
362+
l.delete_photos(photo_id1 + "," + photo_id2).should be(true)
363+
end
354364
end
355365
end
356366

0 commit comments

Comments
 (0)