Skip to content

Commit a264d85

Browse files
authored
Add_sort_images (#45)
1 parent 2e272d8 commit a264d85

File tree

9 files changed

+96
-7
lines changed

9 files changed

+96
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### v0.21.0.pre
2+
3+
* bug-fixes
4+
* Fix `Products#sort_images` method
5+
6+
* features
7+
* Add `Variations#sort_images` method
18
### v0.20.0.pre
29

310
* features

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
beyond_api (0.19.0.pre)
4+
beyond_api (0.21.0.pre)
55
faraday (~> 1.8.0)
66

77
GEM
@@ -112,4 +112,4 @@ DEPENDENCIES
112112
yard (~> 0.9)
113113

114114
BUNDLED WITH
115-
2.2.31
115+
2.3.0

lib/beyond_api/request.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ class << self
2020
end
2121

2222
[:post, :put, :patch].each do |method|
23-
define_method(method) do |session, path, body = {}, params = {}|
23+
define_method(method) do |session, path, body = {}, params = {}, content_type = 'application/json'|
2424
response = BeyondApi::Connection.default.send(method) do |request|
2525
request.url(session.api_url + path)
2626
request.headers["Authorization"] = "Bearer #{session.access_token}" unless session.access_token.nil?
27+
request.headers["Content-Type"] = content_type
2728
request.params = params.to_h.camelize_keys
28-
request.body = body.camelize_keys.to_json
29+
30+
request.body = body.respond_to?(:camelize_keys) ? body.camelize_keys.to_json : body
2931
end
3032

3133
[response.body.blank? ? nil : JSON.parse(response.body), response.status]

lib/beyond_api/resources/products/images.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def set_image_as_default(product_id, image_id)
162162
#
163163
def sort_images(product_id, image_ids)
164164
body = image_ids.map { |image_id| "#{@session.api_url}/products/#{product_id}/images/#{image_id}" }
165-
response, status = BeyondApi::Request.put(@session, "/products/#{product_id}/images", body)
165+
response, status = BeyondApi::Request.put(@session, "/products/#{product_id}/images", body.join("\n"), {}, 'text/uri-list')
166166

167167
handle_response(response, status, respond_with_true: true)
168168
end

lib/beyond_api/resources/variations/images.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,38 @@ def images(product_id, variation_id, params = {})
129129
handle_response(response, status)
130130
end
131131

132+
# 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.
133+
#
134+
# $ curl 'https://api-shop.beyondshop.cloud/api/products/8a8a7002-f864-4011-9991-d6ffb1bd1085/variations/60c2c2a5-ece0-4d04-b90d-f38ee710961c/images' -i -X PUT \
135+
# -H 'Content-Type: text/uri-list' \
136+
# -H 'Authorization: Bearer <Access token>' \
137+
# -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
138+
# https://api-shop.beyondshop.cloud/api/products/8a8a7002-f864-4011-9991-d6ffb1bd1085/variations/60c2c2a5-ece0-4d04-b90d-f38ee710961c/images/4f562165-968c-42fd-a245-1dcc045f8151
139+
# https://api-shop.beyondshop.cloud/api/products/8a8a7002-f864-4011-9991-d6ffb1bd1085/variations/60c2c2a5-ece0-4d04-b90d-f38ee710961c/images/93cd0802-15db-4772-b524-e1c4c6c27b77'
140+
#
141+
# @beyond_api.scopes +prod:u+
142+
#
143+
# @param product_id [String] the product UUID
144+
# @param variation_id [String] the variation UUID
145+
# @param images [Array] the image UUIDS
146+
#
147+
# @return true
148+
#
149+
# @example
150+
# body = [
151+
# "c9082802-a0d0-416e-9039-02fa465a027e",
152+
# "78e9993d-8db3-45d8-8f76-6b8f2aea9c45",
153+
# "9233ee97-5dbb-4c00-a7b2-e1512c69a938"
154+
# ]
155+
# session.variations.sort_images("3f4b2b56-c22d-4d80-b4ed-d5b33ed161eb", body)
156+
#
157+
def sort_images(product_id, variation_id, image_ids)
158+
body = image_ids.map { |image_id| "#{@session.api_url}/products/#{product_id}/variations/#{variation_id}/images/#{image_id}" }
159+
response, status = BeyondApi::Request.put(@session, "/products/#{product_id}/variations/#{variation_id}/images", body.join("\n"), {}, 'text/uri-list')
160+
161+
handle_response(response, status, respond_with_true: true)
162+
end
163+
132164
#
133165
# 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.
134166
#

lib/beyond_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module BeyondApi
4-
VERSION = "0.20.0.pre"
4+
VERSION = "0.21.0.pre"
55
end

spec/beyond_api/resources/products_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,28 @@
5151
expect(image).not_to be nil
5252
expect(image).to eq true
5353
end
54+
55+
it "sort product images" do
56+
files = [
57+
"#{file_path}image1.png",
58+
"#{file_path}image2.png"
59+
]
60+
61+
session.products.upload_multiple_images(product.id,
62+
files,
63+
["image1.png", "image2.png"])
64+
65+
images = session.products.images(product.id)
66+
67+
images_sorted = images.embedded.images.sort_by(&:position).reverse.map(&:id)
68+
69+
sorted = session.products.sort_images(product.id, images_sorted)
70+
71+
images = session.products.images(product.id)
72+
73+
expect(sorted).not_to be nil
74+
expect(sorted).to eq true
75+
expect(images.embedded.images.map(&:id)).to eq images_sorted
76+
end
5477
end
5578
end

spec/beyond_api/resources/variations_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,30 @@
6464

6565
expect(response).not_to be nil
6666
end
67+
68+
it "sort variation images" do
69+
files = [
70+
"#{file_path}image1.png",
71+
"#{file_path}image2.png"
72+
]
73+
74+
default_image_property
75+
response = session.variations.upload_multiple_images(product.id,
76+
variation.id,
77+
files,
78+
["image1.png", "image2.png"])
79+
80+
images = session.variations.images(product.id, variation.id)
81+
82+
images_sorted = images.embedded.images.sort_by(&:position).reverse.map(&:id)
83+
84+
sorted = session.variations.sort_images(product.id, variation.id, images_sorted)
85+
86+
images = session.variations.images(product.id, variation.id)
87+
88+
expect(sorted).not_to be nil
89+
expect(sorted).to eq true
90+
expect(images.embedded.images.map(&:id)).to eq images_sorted
91+
end
6792
end
6893
end

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
BeyondApi.setup do |config|
4242
config.client_id = ENV["CLIENT_ID"]
4343
config.client_secret = ENV["CLIENT_SECRET"]
44-
config.remove_response_links = true
44+
config.remove_response_links = false
4545
config.remove_response_key_underscores = true
4646
config.object_struct_responses = true
4747
end

0 commit comments

Comments
 (0)