Class: Craigslist::API::Resources::Images
- Defined in:
- lib/craigslist/api/resources/images.rb
Overview
Images on a live posting.
Note the API's verb choices are inverted from the usual convention: PUT uploads a new image, POST reorders the existing ones. The method names here describe the effect rather than the verb.
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
Assumed when the caller does not say otherwise.
"image/jpeg"
Constants inherited from Base
Base::BASE_PATH, Base::RESERVED
Instance Method Summary collapse
-
#list(posting_id) ⇒ Array<ImageInfo>
Ordered as they appear on the posting.
-
#remove(posting_id, image_id) ⇒ true
Detaches an image from the posting.
-
#reorder(posting_id, image_ids) ⇒ true
Sets the order of the posting's images.
-
#upload(posting_id, source, filename: nil, content_type: DEFAULT_CONTENT_TYPE, insert_position: nil, replace_position: nil) ⇒ ImageInfo
Uploads an image and attaches it to the posting.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Craigslist::API::Resources::Base
Instance Method Details
#list(posting_id) ⇒ Array<ImageInfo>
Returns ordered as they appear on the posting.
20 21 22 23 |
# File 'lib/craigslist/api/resources/images.rb', line 20 def list(posting_id) data = transport.get(path("postings", posting_id, "images")).data || {} Array(data["imageInfo"]).map { |info| ImageInfo.from(info) } end |
#remove(posting_id, image_id) ⇒ true
Detaches an image from the posting.
The image itself is not destroyed — it stays publicly retrievable and can still be referenced by other postings.
78 79 80 81 |
# File 'lib/craigslist/api/resources/images.rb', line 78 def remove(posting_id, image_id) transport.delete(path("postings", posting_id, "images", image_id)) true end |
#reorder(posting_id, image_ids) ⇒ true
Sets the order of the posting's images.
62 63 64 65 66 67 68 |
# File 'lib/craigslist/api/resources/images.rb', line 62 def reorder(posting_id, image_ids) transport.post( path("postings", posting_id, "images"), form: {"imageIdList" => Array(image_ids).join(",")} ) true end |
#upload(posting_id, source, filename: nil, content_type: DEFAULT_CONTENT_TYPE, insert_position: nil, replace_position: nil) ⇒ ImageInfo
Uploads an image and attaches it to the posting.
With neither position given the image is appended. Positions are zero-based.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/craigslist/api/resources/images.rb', line 38 def upload(posting_id, source, filename: nil, content_type: DEFAULT_CONTENT_TYPE, insert_position: nil, replace_position: nil) if !insert_position.nil? && !replace_position.nil? raise ValidationError, ["pass insert_position or replace_position, not both"] end fields = {} fields["insert_position"] = insert_position.to_s unless insert_position.nil? fields["replace_position"] = replace_position.to_s unless replace_position.nil? envelope = transport.upload( path("postings", posting_id, "images"), part: file_part(source, filename, content_type), fields: fields ) ImageInfo.from((envelope.data || {})["imageInfo"] || {}) end |