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
123 changes: 123 additions & 0 deletions lib/beyond_api/services/shop/location.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# frozen_string_literal: true

module BeyondApi
module Shop
# @example How to instantiate a client
# @client = BeyondApi::Shop::Location.new(api_url: 'https://example.com/api', access_token: 'your_token')
class Location < BaseService
# Create a location.
#
# @see https://developer.epages.com/beyond-docs/#create_location
#
# param body [Hash] the request body
#
# return [hash]
#
# @example
# body = {
# "languageCode": "de",
# "storeCode": "MLC-St-Ives",
# "companyName": "My little Cornershop - St.Ives",
# "primaryPhone": "(800) 555-0102",
# "address": {
# "postalCode": "90999",
# "street": "Hudson Way",
# "houseNumber": "27",
# "city": "St.Ives",
# "country": "GB",
# "dependentLocality": "",
# "state": "Cornwall"
# },
# "googleStatus": "ACTIVE",
# "googlePrimaryCategory": {
# "categoryId": "gcid:storefood",
# "displayName": "Food"
# },
# "googleAdditionalCategories": [ {
# "displayName": "Drinks",
# "categoryId": "gcid:storedrinks"
# } ],
# "regularHours": {
# "periods": [ {
# "openDay": "MONDAY",
# "openTime": "08:00",
# "closeDay": "MONDAY",
# "closeTime": "17:00"
# }, {
# "openDay": "SATURDAY",
# "openTime": "10:00",
# "closeDay": "SATURDAY",
# "closeTime": "16:00"
# } ]
# },
# "latLng": {
# "latitude": 53.5847424,
# "longitude": 9.968901
# }
# }
# @client.create(body)
def create(body)
post('shop/locations', body)
end

# Retrieve a list of all locations for the current shop
#
# @see https://developer.epages.com/beyond-docs/#list_locations
#
# @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 = {})
get('shop/locations', params)
end

# Retrieve details of a location
#
# @see https://developer.epages.com/beyond-docs/#show_location_details
#
# @param id [String] the location UUID
#
# @return [Hash]
#
# @example
# @client.find('869fe0f1-e5ce-491c-914e-5160a4b1cf2f')
def find(location_id)
get("shop/locations/#{location_id}")
end

# Update a location
#
# @see https://developer.epages.com/beyond-docs/#update_location
#
# @param id [String] The language UUID
# param body [Hash] the request body
#
# @return [Hash]
#
# @example
# @client.update('869fe0f1-e5ce-491c-914e-5160a4b1cf2f', body)
def update(location_id, body)
put("shop/locations/#{location_id}", body)
end

# Delete a location
#
# @see https://developer.epages.com/beyond-docs/#delete_location
#
# @param id [String] The language UUID
#
# @return [Hash] an empty hash
#
# @example
# @client.delete('869fe0f1-e5ce-491c-914e-5160a4b1cf2f')
def delete(location_id)
super("shop/locations/#{location_id}")
end
end
end
end
10 changes: 5 additions & 5 deletions spec/beyond_api/services/product_management/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
@category = client.create(build(:category_data))
end

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

describe '.create' do
it 'creates a new category' do
expect(@category).not_to be nil
Expand Down Expand Up @@ -51,10 +56,5 @@
expect(response).to eq({})
end
end

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

RSpec.describe BeyondApi::Shop::Location, 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 locations' do
response = client.all

expect(response).to be_present
expect(response.dig(:embedded, :locations)).to be_kind_of(Array)
expect(response[:page]).to be_kind_of(Hash)
end
end

context 'with location' do
before(:each) do
@location = client.create(build(:location_data))
end

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

describe '.create' do
it 'creates a new shop location' do
expect(@location).to be_present
expect(@location[:store_code]).to eq('MLC-St-Ives')
end
end

describe '.find' do
it 'returns a shop location' do
response = client.find(@location[:id])
expect(response[:store_code]).to eq('MLC-St-Ives')
end
end

describe '.update' do
it 'updates a shop location' do
updated_location_data = FactoryBot.build(:location_data, :alternative_name)
response = client.update(@location[:id], updated_location_data)

expect(response).to be_present
expect(response[:company_name]).to eq('Updated Cornershop')
end
end

describe '.delete' do
it 'deletes a shop location' do
response = client.delete(@location[:id])
expect(response).to eq({})
end
end
end
end
72 changes: 72 additions & 0 deletions spec/factories/location_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

FactoryBot.define do
factory :location_data, class: Hash do
language_code { 'en' }
store_code { 'MLC-St-Ives' }
company_name { 'My little Cornershop - St.Ives' }
primary_phone { '(800) 555-0102' }

address do
{
postal_code: '90999',
street: 'Hudson Way',
house_number: '27',
city: 'St.Ives',
country: 'GB',
dependent_locality: '',
state: 'Cornwall'
}
end

google_status { 'ACTIVE' }

google_primary_category do
{
category_id: 'gcid:storefood',
display_name: 'Food'
}
end

google_additional_categories do
[
{
display_name: 'Drinks',
category_id: 'gcid:storedrinks'
}
]
end

regular_hours do
{
periods: [
{
open_day: 'MONDAY',
open_time: '08:00',
close_day: 'MONDAY',
close_time: '17:00'
},
{
open_day: 'SATURDAY',
open_time: '10:00',
close_day: 'SATURDAY',
close_time: '16:00'
}
]
}
end

lat_lng do
{
latitude: 53.5847424,
longitude: 9.968901
}
end

trait :alternative_name do
company_name { 'Updated Cornershop' }
end

initialize_with { attributes }
end
end
Loading