Module: BeyondApi::ProductImages

Included in:
Products
Defined in:
lib/beyond_api/resources/products/images.rb

Instance Method Summary collapse

Instance Method Details

#add_image(product_id, body) ⇒ OpenStruct

A POST request is used to create an image and add it to a product.

$ curl 'https://api-shop.beyondshop.cloud/api/products/7a7d1f18-f760-46a9-b794-dbe5a88c6b44/images' -i -X POST \
    -H 'Content-Type: application/hal+json' \
    -H 'Accept: application/hal+json' \
    -H 'Authorization: Bearer <Access token>' \
    -d '{
        "dataUri" : "photostore-2.JPG?hash=8a627f655c68f56dfbbf217ab7d5563281225998",
        "width" : 600,
        "height" : 300
    }'

Examples:

body = {
  "data_uri" => "photostore-2.JPG?hash=8a627f655c68f56dfbbf217ab7d5563281225998",
  "width" => 600,
  "height" => 300
}
@image = session.products.add_image("7a7d1f18-f760-46a9-b794-dbe5a88c6b44", body)

Parameters:

  • product_id (String)

    the product UUID

  • image_uri (String)

    the image url

Returns:

  • (OpenStruct)

Scopes:

  • prod:u



35
36
37
38
39
# File 'lib/beyond_api/resources/products/images.rb', line 35

def add_image(product_id, body)
  response, status = BeyondApi::Request.post(@session, "/products/#{product_id}/images", body)

  handle_response(response, status)
end

#delete_image(product_id, image_id) ⇒ Object

A DELETE request is used to delete a product image.

$ curl 'https://api-shop.beyondshop.cloud/api/products/8f5e979e-4a47-47ca-84ce-7c026d623974/images/ac318d53-df29-4f43-9356-d91aed8bdb39' -i -X DELETE \
    -H 'Content-Type: application/hal+json' \
    -H 'Accept: application/hal+json' \
    -H 'Authorization: Bearer <Access token>'

Examples:

session.products.delete_image("8f5e979e-4a47-47ca-84ce-7c026d623974", "ac318d53-df29-4f43-9356-d91aed8bdb39")

Parameters:

  • product_id (String)

    the product UUID

  • image_id (String)

    the image UUID

Returns:

  • true

Scopes:

  • prod:u



59
60
61
62
63
# File 'lib/beyond_api/resources/products/images.rb', line 59

def delete_image(product_id, image_id)
  response, status = BeyondApi::Request.delete(@session, "/products/#{product_id}/images/#{image_id}")

  handle_response(response, status, respond_with_true: true)
end

#image(product_id, image_id) ⇒ OpenStruct

A GET request is used to retrieve a single image of a product.

$ curl 'https://api-shop.beyondshop.cloud/api/products/124c5c94-4e62-410a-8599-e5b29dae3491/images/715f5154-9fde-4213-bcab-41ceaaf8b70e' -i -X GET \
    -H 'Content-Type: application/hal+json' \
    -H 'Accept: application/hal+json' \
    -H 'Authorization: Bearer <Access token>'

Examples:

@image = session.products.image("124c5c94-4e62-410a-8599-e5b29dae3491", "715f5154-9fde-4213-bcab-41ceaaf8b70e")

Parameters:

  • product_id (String)

    the product UUID

  • image_id (String)

    the image UUID

Returns:

  • (OpenStruct)

Scopes:

  • prod:r



107
108
109
110
111
# File 'lib/beyond_api/resources/products/images.rb', line 107

def image(product_id, image_id)
  response, status = BeyondApi::Request.get(@session, "/products/#{product_id}/images/#{image_id}")

  handle_response(response, status)
end

#images(product_id, params = {}) ⇒ OpenStruct

A GET request is used to retrieve the images of a product.

$ curl 'https://api-shop.beyondshop.cloud/api/products/7f32696a-df56-4380-a91b-fffb97f025b4/images' -i -X GET \
    -H 'Content-Type: application/hal+json' \
    -H 'Accept: application/hal+json' \
    -H 'Authorization: Bearer <Access token>'

Examples:

@images = session.products.images("7f32696a-df56-4380-a91b-fffb97f025b4", { size: 20, page: 0 })

Parameters:

  • product_id (String)

    the product UUID

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :size (Integer)

    the page size

  • :page (Integer)

    the page number

Returns:

  • (OpenStruct)

Scopes:

  • prod:r



84
85
86
87
88
# File 'lib/beyond_api/resources/products/images.rb', line 84

def images(product_id, params = {})
  response, status = BeyondApi::Request.get(@session, "/products/#{product_id}/images", params)

  handle_response(response, status)
end

#set_image_as_default(product_id, image_id) ⇒ Object

A PUT request is used to assign a product image as the default image. The request contains a single URI of the image to assign.

$ curl 'https://api-shop.beyondshop.cloud/api/products/150015b9-0b9b-45a2-bcfb-f9006b16a8b8/default-image' -i -X PUT \
    -H 'Content-Type: text/uri-list' \
    -H 'Accept: application/json' \
    -H 'Authorization: Bearer <Access token>' \
    -d 'http://localhost/products/images/8ef3591c-d05f-4aa1-acf6-950ba51ec4f7'

Examples:

session.products.set_image_as_default("150015b9-0b9b-45a2-bcfb-f9006b16a8b8", "8ef3591c-d05f-4aa1-acf6-950ba51ec4f7")

Parameters:

  • product_id (String)

    the product UUID

  • image_id (String)

    the image UUID

Returns:

  • true

Scopes:

  • prod:u



131
132
133
134
135
136
# File 'lib/beyond_api/resources/products/images.rb', line 131

def set_image_as_default(product_id, image_id)
  response, status = BeyondApi::Request.put(@session, "/products/#{product_id}",
                                            "#{@session.api_url}/images/#{image_id}")

  handle_response(response, status, respond_with_true: true)
end

#sort_images(product_id, image_ids) ⇒ Object

A PUT request is used to sort the product images. This is done by passing the self-links of the images to the desired product. The request must contain URIs for all images of the given page.

$ curl 'https://api-shop.beyondshop.cloud/api/products/3f4b2b56-c22d-4d80-b4ed-d5b33ed161eb/images' -i -X PUT \
    -H 'Content-Type: text/uri-list' \
    -H 'Accept: application/hal+json' \
    -H 'Authorization: Bearer <Access token>' \
    -d 'http://localhost/products/3f4b2b56-c22d-4d80-b4ed-d5b33ed161eb/images/c9082802-a0d0-416e-9039-02fa465a027e
        http://localhost/products/3f4b2b56-c22d-4d80-b4ed-d5b33ed161eb/images/78e9993d-8db3-45d8-8f76-6b8f2aea9c45
        http://localhost/products/3f4b2b56-c22d-4d80-b4ed-d5b33ed161eb/images/9233ee97-5dbb-4c00-a7b2-e1512c69a938'

Examples:

body = [
  "c9082802-a0d0-416e-9039-02fa465a027e",
  "78e9993d-8db3-45d8-8f76-6b8f2aea9c45",
  "9233ee97-5dbb-4c00-a7b2-e1512c69a938"
]
session.products.sort_images("3f4b2b56-c22d-4d80-b4ed-d5b33ed161eb", body)

Parameters:

  • product_id (String)

    the product UUID

  • images (Array)

    the image UUIDS

Returns:

  • true

Scopes:

  • prod:u



163
164
165
166
167
168
# File 'lib/beyond_api/resources/products/images.rb', line 163

def sort_images(product_id, image_ids)
  body = image_ids.map { |image_id| "#{@session.api_url}/products/#{product_id}/images/#{image_id}" }
  response, status = BeyondApi::Request.put(@session, "/products/#{product_id}/images", body.join("\n"), {}, 'text/uri-list')

  handle_response(response, status, respond_with_true: true)
end

#upload_image(product_id, image_path, image_name) ⇒ Object

A POST request is used to upload an image and add it to a product. The body of the request must contain the content of the image.

$ curl --data-binary '@/home/epages/file.png' 'https://api-shop.beyondshop.cloud/api/products/4125b993-49fc-47c8-b9b3-76d8871e4e06/images?fileName=file.png' -X POST \
    -H 'Content-Type: image/png' \
    -H 'Authorization: Bearer <Access token>'

Examples:

session.products.upload_image("4125b993-49fc-47c8-b9b3-76d8871e4e06", "/home/epages/file.png", "file.png")

Parameters:

  • product_id (String)

    the product UUID

  • image_path (String)

    the image path

  • image_name (String)

    the image name

Returns:

  • true

Scopes:

  • prod:u



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/beyond_api/resources/products/images.rb', line 187

def upload_image(product_id, image_path, image_name)
  content_type = file_content_type(image_path)
  image_binary = File.binread(image_path)

  response, status = BeyondApi::Request.upload(@session,
                                               "/products/#{product_id}/images",
                                               image_binary,
                                               content_type,
                                               { file_name: image_name })

  handle_response(response, status, respond_with_true: true)
end

#upload_multiple_images(product_id, images_path, images_name) ⇒ Object

A POST request is used to upload up to 10 images and add them to a product. The body of the request must contain the content of the images.

$ curl 'https://api-shop.beyondshop.cloud/api/products/4125b993-49fc-47c8-b9b3-76d8871e4e06/images?fileName=file.png&fileName=file2.png' -i -X POST \
    -H 'Content-Type: multipart/form-data' \
    -H 'Authorization: Bearer <Access token>' \
    -F 'image=@/home/epages/file.png' \
    -F 'image=@/home/epages/file2.png'

Examples:

session.products.upload_multiple_images("4125b993-49fc-47c8-b9b3-76d8871e4e06",
                                        ["/home/epages/file.png", "/home/epages/file2.png"], ["file.png", "file2.png"])

Parameters:

  • product_id (String)

    the product UUID

  • images_path (Array)

    the images path

  • images_name (Array)

    the images name

Returns:

  • true

Scopes:

  • prod:u



220
221
222
223
224
225
226
227
# File 'lib/beyond_api/resources/products/images.rb', line 220

def upload_multiple_images(product_id, images_path, images_name)
  response, status = BeyondApi::Request.upload_by_form(@session,
                                                       "/products/#{product_id}/images",
                                                       images_path,
                                                       file_name: images_name.map { |e| URI.encode_www_form([e]) })

  handle_response(response, status)
end