Skip to content

Commit

Permalink
Add_sort_images (ePages-de#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
gersanco authored Jan 18, 2022
1 parent 2e272d8 commit a264d85
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### v0.21.0.pre

* bug-fixes
* Fix `Products#sort_images` method

* features
* Add `Variations#sort_images` method
### v0.20.0.pre

* features
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
beyond_api (0.19.0.pre)
beyond_api (0.21.0.pre)
faraday (~> 1.8.0)

GEM
Expand Down Expand Up @@ -112,4 +112,4 @@ DEPENDENCIES
yard (~> 0.9)

BUNDLED WITH
2.2.31
2.3.0
6 changes: 4 additions & 2 deletions lib/beyond_api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class << self
end

[:post, :put, :patch].each do |method|
define_method(method) do |session, path, body = {}, params = {}|
define_method(method) do |session, path, body = {}, params = {}, content_type = 'application/json'|
response = BeyondApi::Connection.default.send(method) do |request|
request.url(session.api_url + path)
request.headers["Authorization"] = "Bearer #{session.access_token}" unless session.access_token.nil?
request.headers["Content-Type"] = content_type
request.params = params.to_h.camelize_keys
request.body = body.camelize_keys.to_json

request.body = body.respond_to?(:camelize_keys) ? body.camelize_keys.to_json : body
end

[response.body.blank? ? nil : JSON.parse(response.body), response.status]
Expand Down
2 changes: 1 addition & 1 deletion lib/beyond_api/resources/products/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def set_image_as_default(product_id, image_id)
#
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)
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
Expand Down
32 changes: 32 additions & 0 deletions lib/beyond_api/resources/variations/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,38 @@ def images(product_id, variation_id, params = {})
handle_response(response, status)
end

# A +PUT+ request is used to sort the variation images. This is done by passing the self-links of the images to the desired variation. The request must contain URIs for all images of the given page.
#
# $ curl 'https://api-shop.beyondshop.cloud/api/products/8a8a7002-f864-4011-9991-d6ffb1bd1085/variations/60c2c2a5-ece0-4d04-b90d-f38ee710961c/images' -i -X PUT \
# -H 'Content-Type: text/uri-list' \
# -H 'Authorization: Bearer <Access token>' \
# -d 'https://api-shop.beyondshop.cloud/api/products/8a8a7002-f864-4011-9991-d6ffb1bd1085/variations/60c2c2a5-ece0-4d04-b90d-f38ee710961c/images/a12cae49-3efb-4874-989e-37df6981a4db
# https://api-shop.beyondshop.cloud/api/products/8a8a7002-f864-4011-9991-d6ffb1bd1085/variations/60c2c2a5-ece0-4d04-b90d-f38ee710961c/images/4f562165-968c-42fd-a245-1dcc045f8151
# https://api-shop.beyondshop.cloud/api/products/8a8a7002-f864-4011-9991-d6ffb1bd1085/variations/60c2c2a5-ece0-4d04-b90d-f38ee710961c/images/93cd0802-15db-4772-b524-e1c4c6c27b77'
#
# @beyond_api.scopes +prod:u+
#
# @param product_id [String] the product UUID
# @param variation_id [String] the variation UUID
# @param images [Array] the image UUIDS
#
# @return true
#
# @example
# body = [
# "c9082802-a0d0-416e-9039-02fa465a027e",
# "78e9993d-8db3-45d8-8f76-6b8f2aea9c45",
# "9233ee97-5dbb-4c00-a7b2-e1512c69a938"
# ]
# session.variations.sort_images("3f4b2b56-c22d-4d80-b4ed-d5b33ed161eb", body)
#
def sort_images(product_id, variation_id, image_ids)
body = image_ids.map { |image_id| "#{@session.api_url}/products/#{product_id}/variations/#{variation_id}/images/#{image_id}" }
response, status = BeyondApi::Request.put(@session, "/products/#{product_id}/variations/#{variation_id}/images", body.join("\n"), {}, 'text/uri-list')

handle_response(response, status, respond_with_true: true)
end

#
# A +POST+ request is used to upload an image to the image storage and assign the URL of the image to the variation. The body of the request must contain the content of the image.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/beyond_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module BeyondApi
VERSION = "0.20.0.pre"
VERSION = "0.21.0.pre"
end
23 changes: 23 additions & 0 deletions spec/beyond_api/resources/products_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,28 @@
expect(image).not_to be nil
expect(image).to eq true
end

it "sort product images" do
files = [
"#{file_path}image1.png",
"#{file_path}image2.png"
]

session.products.upload_multiple_images(product.id,
files,
["image1.png", "image2.png"])

images = session.products.images(product.id)

images_sorted = images.embedded.images.sort_by(&:position).reverse.map(&:id)

sorted = session.products.sort_images(product.id, images_sorted)

images = session.products.images(product.id)

expect(sorted).not_to be nil
expect(sorted).to eq true
expect(images.embedded.images.map(&:id)).to eq images_sorted
end
end
end
25 changes: 25 additions & 0 deletions spec/beyond_api/resources/variations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,30 @@

expect(response).not_to be nil
end

it "sort variation images" do
files = [
"#{file_path}image1.png",
"#{file_path}image2.png"
]

default_image_property
response = session.variations.upload_multiple_images(product.id,
variation.id,
files,
["image1.png", "image2.png"])

images = session.variations.images(product.id, variation.id)

images_sorted = images.embedded.images.sort_by(&:position).reverse.map(&:id)

sorted = session.variations.sort_images(product.id, variation.id, images_sorted)

images = session.variations.images(product.id, variation.id)

expect(sorted).not_to be nil
expect(sorted).to eq true
expect(images.embedded.images.map(&:id)).to eq images_sorted
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
BeyondApi.setup do |config|
config.client_id = ENV["CLIENT_ID"]
config.client_secret = ENV["CLIENT_SECRET"]
config.remove_response_links = true
config.remove_response_links = false
config.remove_response_key_underscores = true
config.object_struct_responses = true
end
Expand Down

0 comments on commit a264d85

Please sign in to comment.