Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/beyond_api/concerns/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def delete(path, params = {})
handle_request { agent.delete(path, parse_request(params)) }
end

def upload_file(path, file_path, params = {})
def upload_file(path, file_path, content_type, params = {})
handle_request do
agent.post(path) do |request|
request.headers['Content-Type'] = Utils.file_content_type(file_path)
request.headers['Content-Type'] = content_type
request.params = parse_request(params)
request.body = File.binread(file_path)
end
Expand Down
15 changes: 9 additions & 6 deletions lib/beyond_api/services/product_management/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ def all(id, params = {})
#
# @see https://developer.epages.com/beyond-docs/#upload_product_image
#
# @param id [String] the product UUID
# @param id [String] the image path
# @param id [String] the image file name
# @param product_id [String] the product UUID
# @param image_path [String] the image path
# @param image_name [String] the image file name
#
# @return [Hash]
#
# @example
# client.upload('4125b993-49fc-47c8-b9b3-76d8871e4e06', '/home/epages/file.png', 'file.png')
# @client.upload('4125b993-49fc-47c8-b9b3-76d8871e4e06', '/home/epages/file.png', 'file.png')
def upload(product_id, image_path, image_name)
upload_file("products/#{product_id}/images", image_path, file_name: image_name)
upload_file("products/#{product_id}/images",
image_path,
Utils.file_content_type(image_path),
file_name: image_name)
end

# Upload up to 10 images and add them to a product. The body of the request must contain the content of the images.
Expand All @@ -51,7 +54,7 @@ def upload(product_id, image_path, image_name)
# @example
# image_paths = ['/home/epages/file1.png', '/home/epages/file2.png']
# image_names = ['file1.png', 'file2.png']
# client.upload('4125b993-49fc-47c8-b9b3-76d8871e4e06', image_paths, image_names)
# @client.upload('4125b993-49fc-47c8-b9b3-76d8871e4e06', image_paths, image_names)
def upload_multiple(product_id, image_paths, image_names)
upload_files("products/#{product_id}/images",
{ image: Utils.faraday_file_parts(image_paths) }, # body
Expand Down
85 changes: 85 additions & 0 deletions lib/beyond_api/services/shop/attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

module BeyondApi
module Shop
# @example How to instantiate a client
# @client = BeyondApi::Shop::Attribute.new(api_url: 'https://example.com/api', access_token: 'your_token')
class Attribute < BaseService
# Create a shop attribute.
#
# @see https://developer.epages.com/beyond-docs/#create_shop_attribute
#
# @param name [String] the attribute name
# @param value [String] the attribute value
# @param public [Boolean] flag to indicate if the attribute is public
#
# @return [Hash]
#
# @example
# @client.create('createSku', 'autogenerateSku', true)
def create(name, value, public)
post('shop/attributes', { name:, value:, public: })
end

# List of all shop attributes.
#
# @see https://developer.epages.com/beyond-docs/#list_shop_attributes
#
# @option params [Boolean] :paginated
# @option params [Integer] :size the page size
# @option params [Integer] :page the page number
#
# @return [Hash]
#
# @example
# @client.all(size: 100, page: 0)
def all(params = {})
fetch_all_pages('shop/attributes', params)
end

# Retrieve a particular shop attribute by its name.
#
# @see https://developer.epages.com/beyond-docs/#show_shop_attribute_details
#
# @param name [String] the attribute name
#
# @return [Hash]
#
# @example
# @client.find('createSku')
def find(name)
get("shop/attributes/#{name}")
end

# Update a shop attribute. This operation is idempotent and will create a new shop attribute if required.
#
# @see https://developer.epages.com/beyond-docs/#update_shop_attribute
#
# @param name [String] the attribute name
# @param value [String] the attribute value
# @param public [Boolean] flag to indicate if the attribute is public
#
# @return [Hash]
#
# @example
# @client.update('createSku', 'autogenerateSku', false)
def update(name, value, public)
put("shop/attributes/#{name}", { value:, public: })
end

# Delete a shop attribute.
#
# @see https://developer.epages.com/beyond-docs/#delete_shop_attribute
#
# @param name [String] the attribute name
#
# @return [Hash] an empty hash
#
# @example
# @client.delete('createSku')
def delete(name)
super("shop/attributes/#{name}")
end
end
end
end
100 changes: 100 additions & 0 deletions lib/beyond_api/services/shop/image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# frozen_string_literal: true

module BeyondApi
module Shop
# @example How to instantiate a client
# @client = BeyondApi::Shop::Image.new(api_url: 'https://example.com/api', access_token: 'your_token')
class Image < BaseService
# Create a shop image.
#
# @see https://developer.epages.com/beyond-docs/#create_shop_image
#
# @param body [String] the request body
#
# @return [Hash]
#
# @example
# @client.create(data_uri: 'file.png?hash=212-2323-4343', width: 250, height: 300, label: 'logo')
def create(body)
post('shop/images', body)
end

# Retrieve the images of a shop.
#
# @see https://developer.epages.com/beyond-docs/#list_shop_images
#
# @option params [Boolean] :paginated
# @option params [Integer] :size the page size
# @option params [Integer] :page the page number
#
# @return [Hash]
#
# @example
# @client.all(size: 100, page: 0)
def all(params = {})
fetch_all_pages('shop/images', params)
end

# Search for shop images by label.
#
# @see https://developer.epages.com/beyond-docs/#find_shop_images_by_label
#
# @param label [String] the label to search for
#
# @return [Hash]
#
# @example
# @client.find_by_label('logo')
def find_by_label(label)
get('shop/images/search/find-by-label', label:)
end

# Retrieve a single shop image.
#
# @see https://developer.epages.com/beyond-docs/#show_shop_image_details
#
# @param id [String] the image UUID
#
# @return [Hash]
#
# @example
# @client.find('de5eaacf-8e85-46cf-8bea-d629e52cf987')
def find(id)
get("shop/images/#{id}")
end

# Upload a shop image. The request body must contain the content of the shop image. The maximum image size for shop images is 8 MB.
#
# @see https://developer.epages.com/beyond-docs/#upload_shop_image
#
# @param image_path [String] the image path
# @param image_name [String] the image file name
# @param label [String] the image label
#
# @return [Hash]
#
# @example
# @client.upload('/home/epages/file.png', 'file.png', 'logo')
def upload(image_path, image_name, label)
upload_file('shop/images',
image_path,
'multipart/form-data',
{ file_name: image_name, label: })
end

# Delete a shop image.
#
# @see https://developer.epages.com/beyond-docs/#delete_shop_image
#
# @param id [String] the image UUID
#
# @return [Hash] an empty hash
#
# @example
# @client.delete('ec535cb3-3155-4c8f-b3cc-2bb94db7c530')
def delete(id)
super("shop/images/#{id}")
end
end
end
end
58 changes: 58 additions & 0 deletions spec/beyond_api/services/shop/attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

RSpec.describe BeyondApi::Shop::Attribute, vcr: true do
let(:client) { described_class.new(api_url: ENV.fetch('API_URL', nil), access_token: beyond_access_token) }

describe '.all' do
it 'returns all shop attributes' do
response = client.all

expect(response).not_to be nil
expect(response.dig(:embedded, :attributes)).to be_kind_of(Array)
expect(response[:page]).to be_kind_of(Hash)
end
end

context 'with shop attribute' do
before(:each) do
@shop_attribute = client.create('createSku', 'autogenerateSku', true)
end

describe '.create' do
it 'creates a new shop attribute' do
expect(@shop_attribute).not_to be nil
expect(@shop_attribute[:name]).to eq('createSku')
expect(@shop_attribute[:value]).to eq('autogenerateSku')
expect(@shop_attribute[:public]).to eq(true)
end
end

describe '.find' do
it 'returns a shop attribute' do
response = client.find('createSku')
expect(response[:value]).to eq('autogenerateSku')
end
end

describe '.update' do
it 'updates an existing shop attribute' do
response = client.update('createSku', 'autogenerateSku', false)
expect(response).not_to be nil
expect(response[:name]).to eq('createSku')
expect(response[:value]).to eq('autogenerateSku')
expect(response[:public]).to eq(false)
end
end

describe '.delete' do
it 'deletes a shop attribute' do
expect(client.delete('createSku')).to eq({})
end
end

after(:each) do
client.delete('createSku')
rescue BeyondApi::Error # rubocop:disable Lint/SuppressedException
end
end
end
59 changes: 59 additions & 0 deletions spec/beyond_api/services/shop/image_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

RSpec.describe BeyondApi::Shop::Image, vcr: { match_requests_on: [:method, :uri] } do
let(:client) { described_class.new(api_url: ENV.fetch('API_URL', nil), access_token: beyond_access_token) }

describe '.all' do
it 'returns all shop images' do
response = client.all
puts response.dig(:embedded, :images).first[:id]

expect(response).not_to be nil
puts response.dig(:embedded, :images)
expect(response.dig(:embedded, :images)).to be_kind_of(Array)
expect(response[:page]).to be_kind_of(Hash)
end
end

describe '.find_by_label' do
it 'finds a shop image by label' do
response = client.find_by_label('logo')

expect(response).not_to be nil
expect(response.dig(:embedded, :images)).to be_kind_of(Array)
end
end

context 'with a shop image' do
before(:each) do
@image = client.upload('spec/files/image1.png', 'new-image.png', 'logo')
end

describe '.upload' do
it 'uploads an image' do
expect(@image).not_to be nil
expect(@image.dig(:links, :data, :href)).to include('new-image.png')
end
end

describe '.find' do
it 'finds a shop image' do
response = client.find(@image[:id])

expect(response).not_to be nil
expect(response[:id]).to eq(@image[:id])
end
end

describe '.delete' do
it 'deletes a shop image' do
expect(client.delete(@image[:id])).to eq({})
end
end

after(:each) do
client.delete(@image[:id])
rescue BeyondApi::Error # rubocop:disable Lint/SuppressedException
end
end
end
Loading