Skip to content

Commit f8edb3d

Browse files
committed
FLEX-1553 extend Listing with single reorder_photo
* wasn't sure how to test the new order as the index doesn't return with * the put request.
1 parent 89dbdd7 commit f8edb3d

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

lib/spark_api/models/listing.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,26 @@ def reorder_photos!(arguments={})
191191
results = connection.put "#{self.class.path}/#{self.Id}/photos", arguments
192192
true
193193
end
194-
194+
195+
def reorder_photo(photo_id, index)
196+
unless Integer(index)
197+
raise ArgumentError, "Photo reorder failed. '#{index}' is not a number."
198+
end
199+
200+
begin
201+
return reorder_photo!(photo_id, index)
202+
rescue BadResourceRequest => e
203+
SparkApi.logger.warn { "Failed to save resource #{self}: #{e.message}" }
204+
rescue NotFound => e
205+
SparkApi.logger.error { "Failed to save resource #{self}: #{e.message}" }
206+
end
207+
false
208+
end
209+
def reorder_photo!(photo_id, index)
210+
connection.put "#{self.class.path}/#{self.Id}/photos/#{photo_id}", "Photos" => [{"Order"=>index}]
211+
true
212+
end
213+
195214
def editable?(editable_settings = [])
196215
settings = Array(editable_settings)
197216
editable = attributes.include?("Permissions") && self.Permissions["Editable"] == true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"D": {
3+
"Photos": [{"Order" : "2"}]
4+
}
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"D": {
3+
"Results": [
4+
{
5+
"Uri300": "http://photos.flexmls.com/fgo/20050505220032167405000000.jpg",
6+
"ResourceUri": "/v1/listings/20050505200220759069000000/photos/20050505220032167405000000",
7+
"Name": "Front",
8+
"Primary": true,
9+
"Uri800": "http://cdn.resize.flexmls.com/fgo/800x600/true/20050505220032167405000000-o.jpg",
10+
"Id": "20050505220032167405000000",
11+
"UriLarge": "http://photos.flexmls.com/fgo/20050505220032167405000000-o.jpg",
12+
"Uri1024": "http://cdn.resize.flexmls.com/fgo/1024x768/true/20050505220032167405000000-o.jpg",
13+
"Caption": "",
14+
"Uri1280": "http://cdn.resize.flexmls.com/fgo/1280x1024/true/20050505220032167405000000-o.jpg",
15+
"Uri640": "http://cdn.resize.flexmls.com/fgo/640x480/true/20050505220032167405000000-o.jpg",
16+
"UriThumb": "http://photos.flexmls.com/fgo/20050505220032167405000000-t.jpg"
17+
}
18+
]
19+
}
20+
}

spec/unit/spark_api/models/listing_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@
251251
l.errors.size.should eq(1)
252252
end
253253

254+
on_put_it "should reorder a photo" do
255+
list_id = "20060725224713296297000000"
256+
stub_api_get("/listings/#{list_id}", 'listings/with_photos.json')
257+
stub_api_put("/listings/#{list_id}/photos/20110826220032167405000000", 'listings/put_reorder_photo.json', 'listings/reorder_photo.json')
258+
l = Listing.find(list_id)
259+
l.reorder_photo("20110826220032167405000000", "2")
260+
l.photos.size.should eq(5)
261+
end
262+
263+
on_put_it "should raise an exception when an index is not an Integer" do
264+
list_id = "20060725224713296297000000"
265+
stub_api_get("/listings/#{list_id}", 'listings/with_photos.json')
266+
stub_api_put("/listings/#{list_id}/photos/2011082622003216740500000", 'listings/put_reorder_photo.json', 'listings/reorder_photo.json')
267+
l = Listing.find(list_id)
268+
expect{ l.reorder_photo("2011082622003216740500000", "asdf") }.to raise_error(ArgumentError)
269+
end
270+
254271
context "with pagination" do
255272
# This is really a bogus call, but we should make sure our
256273
# pagination collection impl still behaves sanely

0 commit comments

Comments
 (0)