class Base::Endpoints::Images

This endpoint contains methods for uploading and managing images.

Public Class Methods

new(access_token:, url:) click to toggle source

Initializes this endpoint.

Calls superclass method Base::Endpoint::new
# File lib/base/endpoints/images.rb, line 8
def initialize(access_token:, url:)
  @path = 'images'
  super
end

Public Instance Methods

create(path:, type:, filename:) click to toggle source

Uploads the given image and returns its metadata.

Only images with ImageMagick understands can be uploaded otherwise it will raise an error.

# File lib/base/endpoints/images.rb, line 27
def create(path:, type:, filename:)
  request do
    io =
      Faraday::UploadIO.new(path, type, filename)

    response =
      connection.post('', 'image' => io)

    parse(response.body)
  end
end
delete(id) click to toggle source

Deletes the image with the given ID.

# File lib/base/endpoints/images.rb, line 93
def delete(id)
  request do
    response =
      connection.delete id

    parse(response.body)
  end
end
download(id, quality: nil, resize: nil, format: nil, crop: nil) click to toggle source

Downloads the image with the given ID.

It is possible to crop and resize the image and change its format and quality.

# File lib/base/endpoints/images.rb, line 62
def download(id,
             quality: nil,
             resize: nil,
             format: nil,
             crop: nil)
  url =
    image_url(id, quality: quality,
                  resize: resize,
                  format: format,
                  crop: crop)

  response =
    Faraday.new(url) do |conn|
      conn.use RaiseError
      conn.use Faraday::Adapter::NetHttp
    end.get

  io(response.body)
end
get(id) click to toggle source

Returns the metadata of the image with the given ID.

# File lib/base/endpoints/images.rb, line 83
def get(id)
  request do
    response =
      connection.get id

    parse(response.body)
  end
end
image_url(id, quality: nil, resize: nil, format: nil, crop: nil) click to toggle source

Returns the image url of the image with the given ID.

It is possible to crop and resize the image and change its format and quality.

# File lib/base/endpoints/images.rb, line 43
def image_url(id,
              quality: nil,
              resize: nil,
              format: nil,
              crop: nil)
  params = {}

  quality && params[:quality] = quality.to_s
  format && params[:format] = format.to_s
  resize && params[:resize] = resize.to_s
  crop && params[:crop] = crop.to_s

  "#{connection.url_prefix}#{id}/version?#{URI.encode_www_form(params)}"
end
list(page: 1, per_page: 10) click to toggle source

Lists the files of a project

# File lib/base/endpoints/images.rb, line 14
def list(page: 1, per_page: 10)
  request do
    response =
      connection.get('', per_page: per_page, page: page)

    parse(response.body)
  end
end