Class: Craigslist::API::Resources::Images

Inherits:
Base
  • Object
show all
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

Methods inherited from Base

#initialize

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.

Parameters:

  • posting_id (String, Integer)

Returns:

  • (Array<ImageInfo>)

    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.

Parameters:

  • posting_id (String, Integer)
  • image_id (String)

Returns:

  • (true)


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.

Parameters:

  • posting_id (String, Integer)
  • image_ids (Array<String>)

    every image id, in the desired order

Returns:

  • (true)


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.

Parameters:

  • posting_id (String, Integer)
  • source (String, Pathname, IO)

    the image

  • filename (String, nil) (defaults to: nil)

    defaults to the basename of a path

  • content_type (String) (defaults to: DEFAULT_CONTENT_TYPE)
  • insert_position (Integer, nil) (defaults to: nil)

    insert at this position

  • replace_position (Integer, nil) (defaults to: nil)

    replace the image here

Returns:

Raises:



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