Skip to content

Commit ec6036d

Browse files
authored
v0.19.0.pre
### v0.19.0.pre * features * Upload multiple images for products and variations
1 parent 44330fe commit ec6036d

File tree

21 files changed

+421
-48
lines changed

21 files changed

+421
-48
lines changed

.env.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CLIENT_ID=""
2+
CLIENT_SECRET=""
3+
SHOP_URL=""

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ require:
33
- rubocop-rspec
44

55
AllCops:
6+
Exclude:
7+
- spec/**/*.rb
68
NewCops: enable
79

810
Style/StringLiterals:

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### v0.19.0.pre
2+
3+
* features
4+
* Upload multiple images for products and variations
5+
16
### v0.18.2.pre
27

38
* bug-fixes

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ source "https://rubygems.org"
66
gemspec
77

88
gem "pry"
9+
10+
group :test do
11+
gem "factory_bot"
12+
end

Gemfile.lock

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
11
PATH
22
remote: .
33
specs:
4-
beyond_api (0.18.2.pre)
5-
faraday (~> 0.15)
4+
beyond_api (0.19.0.pre)
5+
faraday (~> 1.8.0)
66

77
GEM
88
remote: https://rubygems.org/
99
specs:
10+
activesupport (6.1.4.1)
11+
concurrent-ruby (~> 1.0, >= 1.0.2)
12+
i18n (>= 1.6, < 2)
13+
minitest (>= 5.1)
14+
tzinfo (~> 2.0)
15+
zeitwerk (~> 2.3)
1016
ast (2.4.2)
1117
coderay (1.1.3)
1218
concurrent-ruby (1.1.9)
1319
diff-lcs (1.4.4)
1420
dotenv (2.7.6)
15-
faker (2.18.0)
21+
factory_bot (6.2.0)
22+
activesupport (>= 5.0.0)
23+
faker (2.19.0)
1624
i18n (>= 1.6, < 2)
17-
faraday (0.17.4)
25+
faraday (1.8.0)
26+
faraday-em_http (~> 1.0)
27+
faraday-em_synchrony (~> 1.0)
28+
faraday-excon (~> 1.1)
29+
faraday-httpclient (~> 1.0.1)
30+
faraday-net_http (~> 1.0)
31+
faraday-net_http_persistent (~> 1.1)
32+
faraday-patron (~> 1.0)
33+
faraday-rack (~> 1.0)
1834
multipart-post (>= 1.2, < 3)
19-
i18n (1.8.10)
35+
ruby2_keywords (>= 0.0.4)
36+
faraday-em_http (1.0.0)
37+
faraday-em_synchrony (1.0.0)
38+
faraday-excon (1.1.0)
39+
faraday-httpclient (1.0.1)
40+
faraday-net_http (1.0.1)
41+
faraday-net_http_persistent (1.2.0)
42+
faraday-patron (1.0.0)
43+
faraday-rack (1.0.0)
44+
i18n (1.8.11)
2045
concurrent-ruby (~> 1.0)
2146
method_source (1.0.0)
47+
minitest (5.14.4)
2248
multipart-post (2.1.1)
23-
parallel (1.20.1)
24-
parser (3.0.2.0)
49+
parallel (1.21.0)
50+
parser (3.0.3.1)
2551
ast (~> 2.4.1)
2652
pry (0.14.1)
2753
coderay (~> 1.1)
@@ -42,26 +68,31 @@ GEM
4268
rspec-mocks (3.10.2)
4369
diff-lcs (>= 1.2.0, < 2.0)
4470
rspec-support (~> 3.10.0)
45-
rspec-support (3.10.2)
46-
rubocop (1.20.0)
71+
rspec-support (3.10.3)
72+
rubocop (1.23.0)
4773
parallel (~> 1.10)
4874
parser (>= 3.0.0.0)
4975
rainbow (>= 2.2.2, < 4.0)
5076
regexp_parser (>= 1.8, < 3.0)
5177
rexml
52-
rubocop-ast (>= 1.9.1, < 2.0)
78+
rubocop-ast (>= 1.12.0, < 2.0)
5379
ruby-progressbar (~> 1.7)
5480
unicode-display_width (>= 1.4.0, < 3.0)
55-
rubocop-ast (1.11.0)
81+
rubocop-ast (1.13.0)
5682
parser (>= 3.0.1.1)
5783
rubocop-ordered_methods (0.9)
5884
rubocop (>= 1.0)
59-
rubocop-rspec (2.4.0)
60-
rubocop (~> 1.0)
61-
rubocop-ast (>= 1.1.0)
85+
rubocop-rspec (2.6.0)
86+
rubocop (~> 1.19)
6287
ruby-progressbar (1.11.0)
63-
unicode-display_width (2.0.0)
64-
yard (0.9.26)
88+
ruby2_keywords (0.0.5)
89+
tzinfo (2.0.4)
90+
concurrent-ruby (~> 1.0)
91+
unicode-display_width (2.1.0)
92+
webrick (1.7.0)
93+
yard (0.9.27)
94+
webrick (~> 1.7.0)
95+
zeitwerk (2.5.1)
6596

6697
PLATFORMS
6798
ruby
@@ -70,6 +101,7 @@ DEPENDENCIES
70101
beyond_api!
71102
bundler (~> 2.0)
72103
dotenv (~> 2.7)
104+
factory_bot
73105
faker (~> 2.2)
74106
pry
75107
rake (~> 10.0)
@@ -80,4 +112,4 @@ DEPENDENCIES
80112
yard (~> 0.9)
81113

82114
BUNDLED WITH
83-
2.2.26
115+
2.2.31

beyond_api.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ Gem::Specification.new do |spec|
3131
spec.add_development_dependency "rubocop-rspec", "~> 2.4"
3232
spec.add_development_dependency "yard", "~> 0.9"
3333

34-
spec.add_dependency "faraday", "~> 0.15"
34+
spec.add_dependency "faraday", "~> 1.8.0"
3535
end

lib/beyond_api/connection.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ def self.token
3232
end
3333
faraday.headers['Accept'] = 'application/json'
3434
faraday.adapter(:net_http)
35-
faraday.basic_auth(BeyondApi.configuration.client_id,
36-
BeyondApi.configuration.client_secret)
35+
faraday.request :basic_auth, BeyondApi.configuration.client_id, BeyondApi.configuration.client_secret
36+
end
37+
end
38+
39+
def self.multipart
40+
Faraday.new(ssl: { verify: true }) do |faraday|
41+
faraday.options[:open_timeout] = BeyondApi.configuration.open_timeout.to_i
42+
faraday.options[:timeout] = BeyondApi.configuration.timeout.to_i
43+
faraday.request :multipart, { flat_encode: true }
44+
faraday.adapter Faraday.default_adapter
3745
end
3846
end
3947
end

lib/beyond_api/request.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require "json"
4+
require "faraday"
5+
require "beyond_api/utils"
46

57
module BeyondApi
68
class Request
@@ -51,5 +53,20 @@ def self.token(url, params)
5153

5254
[response.body.blank? ? nil : JSON.parse(response.body), response.status]
5355
end
56+
57+
def self.upload_by_form(session, path, files, params)
58+
response = BeyondApi::Connection.multipart.post do |request|
59+
request.url(session.api_url + path)
60+
request.headers["Authorization"] = "Bearer #{session.access_token}" unless session.access_token.nil?
61+
request.options[:params_encoder] = Faraday::FlatParamsEncoder
62+
request.params = params.to_h.camelize_keys
63+
files = files.split unless files.is_a? Array
64+
upload_files = files.map{ |file| Faraday::FilePart.new(File.open(file),
65+
BeyondApi::Utils.file_content_type(file)) }
66+
request.body = { image: upload_files }
67+
end
68+
69+
[response.body.blank? ? nil : JSON.parse(response.body), response.status]
70+
end
5471
end
5572
end

lib/beyond_api/resources/products/images.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,7 @@ def sort_images(product_id, image_ids)
185185
# session.products.upload_image("4125b993-49fc-47c8-b9b3-76d8871e4e06", "/home/epages/file.png", "file.png")
186186
#
187187
def upload_image(product_id, image_path, image_name)
188-
content_type = case File.extname(image_path)
189-
when ".png"
190-
"image/png"
191-
when ".jpg", ".jpeg"
192-
"image/jpeg"
193-
when ".gif"
194-
"image/gif"
195-
end
188+
content_type = file_content_type(image_path)
196189
image_binary = File.binread(image_path)
197190

198191
response, status = BeyondApi::Request.upload(@session,
@@ -203,5 +196,34 @@ def upload_image(product_id, image_path, image_name)
203196

204197
handle_response(response, status, respond_with_true: true)
205198
end
199+
200+
# 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.
201+
#
202+
# $ curl 'https://api-shop.beyondshop.cloud/api/products/4125b993-49fc-47c8-b9b3-76d8871e4e06/images?fileName=file.png&fileName=file2.png' -i -X POST \
203+
# -H 'Content-Type: multipart/form-data' \
204+
# -H 'Authorization: Bearer <Access token>' \
205+
# -F 'image=@/home/epages/file.png' \
206+
# -F 'image=@/home/epages/file2.png'
207+
#
208+
# @beyond_api.scopes +prod:u+
209+
#
210+
# @param product_id [String] the product UUID
211+
# @param images_path [Array] the images path
212+
# @param images_name [Array] the images name
213+
#
214+
# @return true
215+
#
216+
# @example
217+
# session.products.upload_multiple_images("4125b993-49fc-47c8-b9b3-76d8871e4e06",
218+
# ["/home/epages/file.png", "/home/epages/file2.png"], ["file.png", "file2.png"])
219+
#
220+
def upload_multiple_images(product_id, images_path, images_name)
221+
response, status = BeyondApi::Request.upload_by_form(@session,
222+
"/products/#{product_id}/images",
223+
images_path,
224+
file_name: images_name)
225+
226+
handle_response(response, status)
227+
end
206228
end
207229
end

lib/beyond_api/resources/variations/images.rb

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ def images(product_id, variation_id, params = {})
140140
#
141141
# @param product_id [String] the product UUID
142142
# @param variation_id [String] the variation UUID
143-
# @param image_path [String] the image path
144-
# @param image_name [String] the image name
143+
# @param images_path [Array] the images path
144+
# @param images_name [Array] the images name
145145
#
146146
# @return [OpenStruct]
147147
#
@@ -154,15 +154,9 @@ def images(product_id, variation_id, params = {})
154154
# session.variations.upload_image("4125b993-49fc-47c8-b9b3-76d8871e4e06", "d7fecf94-2e57-4122-8c94-a0acd840c111", "/home/epages/file.png", "file.png")
155155
#
156156
def upload_image(product_id, variation_id, image_path, image_name)
157+
content_type = file_content_type(image_path)
157158
path = "/products/#{product_id}/variations/#{variation_id}/images"
158-
content_type = case File.extname(image_path)
159-
when ".png"
160-
"image/png"
161-
when ".jpg", ".jpeg"
162-
"image/jpeg"
163-
when ".gif"
164-
"image/gif"
165-
end
159+
166160
image_binary = File.binread(image_path)
167161

168162
response, status = BeyondApi::Request.upload(@session,
@@ -173,5 +167,36 @@ def upload_image(product_id, variation_id, image_path, image_name)
173167

174168
handle_response(response, status, respond_with_true: true)
175169
end
170+
171+
#
172+
# A +POST+ request is used to upload up to 10 images to the image storage and assign the URL of the images to up to 30 variations. The body of the request must contain the content of the images.
173+
#
174+
# $ curl 'https://api-shop.beyondshop.cloud/api/products/4125b993-49fc-47c8-b9b3-76d8871e4e06/variations/images?fileName=file.png&fileName=file2.png&variation=ca53ae26-e7c6-44a4-8070-9fca08cc87ed&variation=ab63fa3a-c2ac-4074-aaa3-b547217b042d' -i -X POST \
175+
# -H 'Content-Type: multipart/form-data' \
176+
# -H 'Authorization: Bearer <Access token>' \
177+
# -F 'image=@/home/epages/file.png' \
178+
# -F 'image=@/home/epages/file2.png'
179+
#
180+
# @beyond_api.scopes +prod:u+
181+
#
182+
# @param product_id [String] the product UUID
183+
# @param variation_id [String] the variation UUID
184+
# @param images_path [String] the image path
185+
# @param images_name [String] the image name
186+
#
187+
# @return [OpenStruct]
188+
#
189+
# @example
190+
# session.variations.upload_multiple_images("4125b993-49fc-47c8-b9b3-76d8871e4e06", "d7fecf94-2e57-4122-8c94-a0acd840c111",
191+
# ["/home/epages/file.png", "/home/epages/file2.png"], ["file.png", "file2.png"])
192+
#
193+
def upload_multiple_images(product_id, variation_id, images_path, images_name)
194+
response, status = BeyondApi::Request.upload_by_form(@session,
195+
"/products/#{product_id}/variations/#{variation_id}/images",
196+
images_path,
197+
file_name: images_name)
198+
199+
handle_response(response, status)
200+
end
176201
end
177202
end

0 commit comments

Comments
 (0)