solidus_foudra 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +24 -0
- data/.rspec +2 -0
- data/.rubocop.yml +12 -0
- data/Gemfile +25 -0
- data/LICENSE +201 -0
- data/README.md +241 -0
- data/Rakefile +7 -0
- data/bin/console +17 -0
- data/bin/rails +7 -0
- data/bin/rails-engine +13 -0
- data/bin/rails-sandbox +16 -0
- data/bin/rake +7 -0
- data/bin/sandbox +85 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +5 -0
- data/lib/solidus_foudra/client.rb +234 -0
- data/lib/solidus_foudra/configuration.rb +87 -0
- data/lib/solidus_foudra/engine.rb +23 -0
- data/lib/solidus_foudra/markdown.rb +83 -0
- data/lib/solidus_foudra/redact.rb +18 -0
- data/lib/solidus_foudra/testing_support/factories.rb +4 -0
- data/lib/solidus_foudra/version.rb +8 -0
- data/lib/solidus_foudra/webhook_signature.rb +48 -0
- data/lib/solidus_foudra.rb +45 -0
- data/solidus_foudra.gemspec +40 -0
- metadata +131 -0
data/bin/sandbox
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
set -e
|
|
4
|
+
test -z "${DEBUG+empty_string}" || set -x
|
|
5
|
+
|
|
6
|
+
test "$DB" = "sqlite" && export DB="sqlite3"
|
|
7
|
+
|
|
8
|
+
if [ -z "$PAYMENT_METHOD" ]
|
|
9
|
+
then
|
|
10
|
+
PAYMENT_METHOD="none"
|
|
11
|
+
fi
|
|
12
|
+
|
|
13
|
+
if [ -z "$SOLIDUS_BRANCH" ]
|
|
14
|
+
then
|
|
15
|
+
echo "~~> Use 'export SOLIDUS_BRANCH=[main|v4.0|...]' to control the Solidus branch"
|
|
16
|
+
SOLIDUS_BRANCH="main"
|
|
17
|
+
fi
|
|
18
|
+
echo "~~> Using branch $SOLIDUS_BRANCH of solidus"
|
|
19
|
+
|
|
20
|
+
extension_name="solidus_foudra"
|
|
21
|
+
|
|
22
|
+
# Stay away from the bundler env of the containing extension.
|
|
23
|
+
function unbundled {
|
|
24
|
+
ruby -rbundler -e'
|
|
25
|
+
Bundler.with_unbundled_env {system *ARGV}' -- \
|
|
26
|
+
env BUNDLE_SUPPRESS_INSTALL_USING_MESSAGES=true $@
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
echo "~~~> Removing the old sandbox"
|
|
30
|
+
rm -rf ./sandbox
|
|
31
|
+
|
|
32
|
+
echo "~~~> Creating a pristine Rails app"
|
|
33
|
+
rails_version=`bundle exec ruby -e'require "rails"; puts Rails.version'`
|
|
34
|
+
rails _${rails_version}_ new sandbox \
|
|
35
|
+
--database="${DB:-sqlite3}" \
|
|
36
|
+
--skip-git \
|
|
37
|
+
--skip-keeps \
|
|
38
|
+
--skip-rc \
|
|
39
|
+
--skip-bootsnap \
|
|
40
|
+
--skip-test
|
|
41
|
+
|
|
42
|
+
if [ ! -d "sandbox" ]; then
|
|
43
|
+
echo 'sandbox rails application failed'
|
|
44
|
+
exit 1
|
|
45
|
+
fi
|
|
46
|
+
|
|
47
|
+
echo "~~~> Adding solidus (with i18n) to the Gemfile"
|
|
48
|
+
cd ./sandbox
|
|
49
|
+
cat <<RUBY >> Gemfile
|
|
50
|
+
gem 'solidus', github: 'solidusio/solidus', branch: '$SOLIDUS_BRANCH'
|
|
51
|
+
gem 'rails-i18n'
|
|
52
|
+
gem 'solidus_i18n'
|
|
53
|
+
gem 'solidus_auth_devise'
|
|
54
|
+
|
|
55
|
+
gem '$extension_name', path: '..'
|
|
56
|
+
|
|
57
|
+
group :test, :development do
|
|
58
|
+
platforms :mri do
|
|
59
|
+
gem 'pry-byebug'
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
RUBY
|
|
63
|
+
|
|
64
|
+
echo "Generating manifest file"
|
|
65
|
+
mkdir -p app/assets/config
|
|
66
|
+
cat <<MANIFESTJS > app/assets/config/manifest.js
|
|
67
|
+
//= link_tree ../images
|
|
68
|
+
//= link_directory ../javascripts .js
|
|
69
|
+
//= link_directory ../stylesheets .css
|
|
70
|
+
MANIFESTJS
|
|
71
|
+
|
|
72
|
+
unbundled bundle install --gemfile Gemfile
|
|
73
|
+
|
|
74
|
+
unbundled bundle exec rake db:drop db:create
|
|
75
|
+
|
|
76
|
+
unbundled bundle exec rails generate solidus:install \
|
|
77
|
+
--auto-accept \
|
|
78
|
+
$@
|
|
79
|
+
|
|
80
|
+
unbundled bundle exec rails generate solidus:auth:install --auto-run-migrations
|
|
81
|
+
unbundled bundle exec rails generate ${extension_name}:install --auto-run-migrations
|
|
82
|
+
|
|
83
|
+
echo
|
|
84
|
+
echo "🚀 Sandbox app successfully created for $extension_name!"
|
|
85
|
+
echo "🧪 This app is intended for test purposes."
|
data/bin/setup
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "json"
|
|
7
|
+
require "net/http"
|
|
8
|
+
require "uri"
|
|
9
|
+
|
|
10
|
+
module SolidusFoudra
|
|
11
|
+
# A small client for Foudra's JSON API (foudra.com/developers), on
|
|
12
|
+
# Net::HTTP so the gem adds no runtime dependency.
|
|
13
|
+
#
|
|
14
|
+
# Foudra's conventions it relies on: `Authorization: Bearer fdr_…`;
|
|
15
|
+
# answers wrapped as {"data": …} (lists add page/pages/total); errors as
|
|
16
|
+
# {"error": "…"} with a status that means what it says; decimals as
|
|
17
|
+
# strings; Idempotency-Key on writes makes a repeat return the first answer.
|
|
18
|
+
class Client
|
|
19
|
+
class Error < StandardError
|
|
20
|
+
attr_reader :status, :path
|
|
21
|
+
|
|
22
|
+
def initialize(message, status: nil, path: nil)
|
|
23
|
+
super(Redact.call(message))
|
|
24
|
+
@status = status
|
|
25
|
+
@path = path
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# 404: not there, or in another company.
|
|
30
|
+
class NotFound < Error; end
|
|
31
|
+
# 409: not in a state that allows it (e.g. cancelling a shipped order).
|
|
32
|
+
class Conflict < Error; end
|
|
33
|
+
# 401/402/403: the key, the subscription or the role. Retrying won't help
|
|
34
|
+
# until a person fixes it.
|
|
35
|
+
class Refused < Error; end
|
|
36
|
+
# 429, 5xx and network failures that outlasted our own attempts. Jobs
|
|
37
|
+
# retry these later with ActiveJob's backoff.
|
|
38
|
+
class Unavailable < Error; end
|
|
39
|
+
|
|
40
|
+
RETRY_STATUSES = [429, 500, 502, 503, 504].freeze
|
|
41
|
+
NETWORK_ERRORS = [IOError, SystemCallError, Timeout::Error, OpenSSL::SSL::SSLError,
|
|
42
|
+
Net::OpenTimeout, Net::ReadTimeout, SocketError].freeze
|
|
43
|
+
|
|
44
|
+
attr_reader :config
|
|
45
|
+
|
|
46
|
+
# sleeper is how long-running pauses happen; tests pass a no-op.
|
|
47
|
+
def initialize(config = SolidusFoudra.config, sleeper: ->(seconds) { sleep(seconds) })
|
|
48
|
+
@config = config
|
|
49
|
+
@sleeper = sleeper
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def get(path, params = {})
|
|
53
|
+
request(:get, path, params:)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def post(path, body = {}, idempotency_key: nil)
|
|
57
|
+
request(:post, path, body:, idempotency_key:)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def put(path, body = {}, idempotency_key: nil)
|
|
61
|
+
request(:put, path, body:, idempotency_key:)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Every row of a paged list, a page of 100 at a time.
|
|
65
|
+
def each_page(path, params = {}, &block)
|
|
66
|
+
return enum_for(:each_page, path, params) unless block
|
|
67
|
+
|
|
68
|
+
page = 1
|
|
69
|
+
loop do
|
|
70
|
+
out = get(path, params.merge(page:, per: 100))
|
|
71
|
+
Array(out["data"]).each(&block)
|
|
72
|
+
break if page >= out.fetch("pages", 1).to_i
|
|
73
|
+
|
|
74
|
+
page += 1
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# --- The calls the gem makes ------------------------------------------
|
|
79
|
+
|
|
80
|
+
def part(id)
|
|
81
|
+
get("/api/parts/#{Integer(id)}")["data"]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# A part by its exact part number. ?q= also matches other text, so
|
|
85
|
+
# compare; nil when Foudra has no such part.
|
|
86
|
+
def find_part(part_number)
|
|
87
|
+
get("/api/parts", q: part_number, per: 25)["data"].find { |p| p["part_number"] == part_number }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def parts(&)
|
|
91
|
+
each_page("/api/parts", &)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def images(part_id)
|
|
95
|
+
get("/api/parts/#{Integer(part_id)}/images")["data"]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The bytes of a file the API names by path (an image's url).
|
|
99
|
+
def download(path)
|
|
100
|
+
request(:get, path, raw: true)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def channels
|
|
104
|
+
get("/api/channels")["data"]
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Every listing on the channel, listed or not (not paged).
|
|
108
|
+
def listings(channel = config.channel)
|
|
109
|
+
get("/api/channels/#{escape(channel)}/listings")["data"]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def put_listing(part_id, attrs, channel: config.channel)
|
|
113
|
+
put("/api/channels/#{escape(channel)}/listings/#{Integer(part_id)}", attrs)["data"]
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def customers_by_email(email)
|
|
117
|
+
get("/api/customers", email:, per: 10)["data"]
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def create_customer(attrs, idempotency_key:)
|
|
121
|
+
post("/api/customers", attrs, idempotency_key:)["data"]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def find_sales_order(channel_order_ref, channel: config.channel)
|
|
125
|
+
get("/api/sales-orders", channel:, channel_order_ref:, per: 10)["data"].first
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def sales_order(id)
|
|
129
|
+
get("/api/sales-orders/#{Integer(id)}")["data"]
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def create_sales_order(attrs, idempotency_key:)
|
|
133
|
+
post("/api/sales-orders", attrs, idempotency_key:)["data"]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def cancel_sales_order(id, idempotency_key:)
|
|
137
|
+
post("/api/sales-orders/#{Integer(id)}/cancel", {}, idempotency_key:)["data"]
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def shipment(id)
|
|
141
|
+
get("/api/shipments/#{Integer(id)}")["data"]
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def shipments_for(sales_order_id, status: "shipped")
|
|
145
|
+
each_page("/api/shipments", sales_order_id: Integer(sales_order_id), status:).to_a
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
def escape(segment)
|
|
151
|
+
URI.encode_www_form_component(segment.to_s)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def request(method, path, params: nil, body: nil, idempotency_key: nil, raw: false)
|
|
155
|
+
raise Error, "Foudra is not configured (base_url, api_key, channel)" unless config.enabled?
|
|
156
|
+
|
|
157
|
+
uri = URI.join(config.base_url.to_s.chomp("/") + "/", path.delete_prefix("/"))
|
|
158
|
+
uri.query = URI.encode_www_form(params) if params.present?
|
|
159
|
+
attempt = 0
|
|
160
|
+
begin
|
|
161
|
+
attempt += 1
|
|
162
|
+
response = perform(method, uri, body, idempotency_key)
|
|
163
|
+
status = response.code.to_i
|
|
164
|
+
if RETRY_STATUSES.include?(status) && attempt < config.max_attempts
|
|
165
|
+
@sleeper.call(pause(attempt, response))
|
|
166
|
+
raise RetryNow
|
|
167
|
+
end
|
|
168
|
+
handle(response, method, path, raw)
|
|
169
|
+
rescue RetryNow
|
|
170
|
+
retry
|
|
171
|
+
rescue *NETWORK_ERRORS => e
|
|
172
|
+
# Every write we make carries an Idempotency-Key and GET and PUT are
|
|
173
|
+
# safe to repeat, so a network failure is always safe to retry.
|
|
174
|
+
if attempt < config.max_attempts
|
|
175
|
+
@sleeper.call(pause(attempt, nil))
|
|
176
|
+
retry
|
|
177
|
+
end
|
|
178
|
+
raise Unavailable.new("#{method.upcase} #{path}: #{e.class}", path:)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
class RetryNow < StandardError; end
|
|
183
|
+
private_constant :RetryNow
|
|
184
|
+
|
|
185
|
+
def perform(method, uri, body, idempotency_key)
|
|
186
|
+
klass = {get: Net::HTTP::Get, post: Net::HTTP::Post, put: Net::HTTP::Put}.fetch(method)
|
|
187
|
+
req = klass.new(uri)
|
|
188
|
+
req["Authorization"] = "Bearer #{config.api_key}"
|
|
189
|
+
req["Accept"] = "application/json"
|
|
190
|
+
req["User-Agent"] = "solidus_foudra/#{SolidusFoudra::VERSION}"
|
|
191
|
+
req["Idempotency-Key"] = idempotency_key if idempotency_key
|
|
192
|
+
if body
|
|
193
|
+
req["Content-Type"] = "application/json"
|
|
194
|
+
req.body = JSON.generate(body)
|
|
195
|
+
end
|
|
196
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
197
|
+
open_timeout: config.open_timeout,
|
|
198
|
+
read_timeout: config.read_timeout) do |http|
|
|
199
|
+
http.request(req)
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Retry-After when Foudra gives one (429), else 1s, 2s, 4s…
|
|
204
|
+
def pause(attempt, response)
|
|
205
|
+
after = response&.[]("Retry-After").to_i
|
|
206
|
+
after.positive? ? [after, 60].min : 2**(attempt - 1)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def handle(response, method, path, raw)
|
|
210
|
+
status = response.code.to_i
|
|
211
|
+
if status.between?(200, 299)
|
|
212
|
+
return response.body if raw
|
|
213
|
+
|
|
214
|
+
return response.body.blank? ? {} : JSON.parse(response.body)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
message = "#{method.upcase} #{path}: #{status} #{error_text(response)}".strip
|
|
218
|
+
klass = case status
|
|
219
|
+
when 404 then NotFound
|
|
220
|
+
when 409 then Conflict
|
|
221
|
+
when 401, 402, 403 then Refused
|
|
222
|
+
when *RETRY_STATUSES then Unavailable
|
|
223
|
+
else Error
|
|
224
|
+
end
|
|
225
|
+
raise klass.new(message, status:, path:)
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def error_text(response)
|
|
229
|
+
JSON.parse(response.body.to_s)["error"].to_s
|
|
230
|
+
rescue JSON::ParserError, TypeError, NoMethodError
|
|
231
|
+
response.body.to_s[0, 200]
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# The settings, from config/initializers/solidus_foudra.rb. The API key
|
|
8
|
+
# and the webhook secret come from ENV or Rails credentials and are never
|
|
9
|
+
# committed; #inspect leaves them out so a stray `p config` can't leak them.
|
|
10
|
+
class Configuration
|
|
11
|
+
# https://<company>.foudra.com: the company's own subdomain, where its
|
|
12
|
+
# API key works and nowhere else.
|
|
13
|
+
attr_accessor :base_url
|
|
14
|
+
# fdr_…: an API key with the write scope (orders and customers are writes).
|
|
15
|
+
attr_accessor :api_key
|
|
16
|
+
# The signing secret Foudra showed once when the webhook was made.
|
|
17
|
+
attr_accessor :webhook_secret
|
|
18
|
+
# The code of the Foudra channel that is this shop, e.g. "SHOP".
|
|
19
|
+
attr_accessor :channel
|
|
20
|
+
# Where Foudra's available stock is put: a Spree::StockLocation, its id,
|
|
21
|
+
# its name, or nil for the default (or only) one.
|
|
22
|
+
attr_accessor :stock_location
|
|
23
|
+
# The spree_variants column holding the Foudra part number. The install
|
|
24
|
+
# migration adds foudra_part_number; Kalopa's shop uses :stock_code.
|
|
25
|
+
attr_writer :part_number_column
|
|
26
|
+
# false: a product's name, description and image are Foudra's only when
|
|
27
|
+
# the product is created here; afterwards the shop edits them. true:
|
|
28
|
+
# every sync overwrites them from Foudra.
|
|
29
|
+
attr_accessor :sync_content
|
|
30
|
+
# How old a webhook's signed timestamp may be, in seconds.
|
|
31
|
+
attr_accessor :webhook_tolerance
|
|
32
|
+
# The nightly pass re-sends orders completed since this time that never
|
|
33
|
+
# reached Foudra (a job that gave up). nil: it doesn't. Set it to when
|
|
34
|
+
# the gem went live: an older order, already shipped by other means,
|
|
35
|
+
# would become demand in Foudra again.
|
|
36
|
+
attr_accessor :send_orders_since
|
|
37
|
+
# Days after the order date that a sales order is wanted.
|
|
38
|
+
attr_accessor :wanted_after_days
|
|
39
|
+
attr_accessor :open_timeout, :read_timeout
|
|
40
|
+
# Attempts per request inside one job, on 429 and 5xx; ActiveJob's own
|
|
41
|
+
# retries, with longer pauses, come after.
|
|
42
|
+
attr_accessor :max_attempts
|
|
43
|
+
# The ActiveJob queue for this gem's jobs.
|
|
44
|
+
attr_accessor :queue
|
|
45
|
+
# Logger; Rails.logger when nil.
|
|
46
|
+
attr_accessor :logger
|
|
47
|
+
# A callable turning Markdown into HTML, if the shop has a Markdown gem
|
|
48
|
+
# it prefers; nil uses SolidusFoudra::Markdown.
|
|
49
|
+
attr_accessor :markdown_renderer
|
|
50
|
+
|
|
51
|
+
def initialize
|
|
52
|
+
@part_number_column = :foudra_part_number
|
|
53
|
+
@sync_content = false
|
|
54
|
+
@webhook_tolerance = 300
|
|
55
|
+
@wanted_after_days = 0
|
|
56
|
+
@open_timeout = 10
|
|
57
|
+
@read_timeout = 60
|
|
58
|
+
@max_attempts = 3
|
|
59
|
+
@queue = :default
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Nothing is sent or synced until the three essentials are set, so the
|
|
63
|
+
# gem can be installed (and the test suite run) without a Foudra.
|
|
64
|
+
def enabled?
|
|
65
|
+
[base_url, api_key, channel].all?(&:present?)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def part_number_column
|
|
69
|
+
@part_number_column.to_s
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def stock_location_record
|
|
73
|
+
case stock_location
|
|
74
|
+
when Spree::StockLocation then stock_location
|
|
75
|
+
when Integer then Spree::StockLocation.find(stock_location)
|
|
76
|
+
when String, Symbol then Spree::StockLocation.find_by!(name: stock_location.to_s)
|
|
77
|
+
else
|
|
78
|
+
Spree::StockLocation.find_by(default: true) || Spree::StockLocation.order(:id).first!
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def inspect
|
|
83
|
+
"#<#{self.class.name} base_url=#{base_url.inspect} channel=#{channel.inspect} " \
|
|
84
|
+
"part_number_column=#{part_number_column.inspect} sync_content=#{sync_content.inspect}>"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "solidus_core"
|
|
7
|
+
require "solidus_support"
|
|
8
|
+
|
|
9
|
+
module SolidusFoudra
|
|
10
|
+
class Engine < Rails::Engine
|
|
11
|
+
include SolidusSupport::EngineExtensions
|
|
12
|
+
|
|
13
|
+
# Its own namespace, not Spree's: the one route (the webhook) is
|
|
14
|
+
# SolidusFoudra::WebhooksController, mounted by the host app at /foudra.
|
|
15
|
+
isolate_namespace SolidusFoudra
|
|
16
|
+
|
|
17
|
+
engine_name "solidus_foudra"
|
|
18
|
+
|
|
19
|
+
config.generators do |g|
|
|
20
|
+
g.test_framework :rspec
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "cgi"
|
|
7
|
+
|
|
8
|
+
module SolidusFoudra
|
|
9
|
+
# Foudra's part descriptions are Markdown; Solidus shows a product's
|
|
10
|
+
# description as HTML. Solidus pulls in no Markdown gem and a runtime
|
|
11
|
+
# dependency for this alone isn't worth it, so this is the small subset
|
|
12
|
+
# part descriptions use: paragraphs, headings, bullet and numbered lists,
|
|
13
|
+
# **bold**, *italic*, `code` and [links](https://…). Everything is
|
|
14
|
+
# HTML-escaped first, so nothing in a description can inject markup.
|
|
15
|
+
# A shop with a Markdown gem it trusts sets config.markdown_renderer.
|
|
16
|
+
module Markdown
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def render(text)
|
|
20
|
+
return "" if text.blank?
|
|
21
|
+
|
|
22
|
+
custom = SolidusFoudra.config.markdown_renderer
|
|
23
|
+
return custom.call(text.to_s) if custom
|
|
24
|
+
|
|
25
|
+
blocks(text.to_s.gsub("\r\n", "\n")).join("\n")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def blocks(text)
|
|
29
|
+
out = []
|
|
30
|
+
list = nil
|
|
31
|
+
paragraph = []
|
|
32
|
+
flush_paragraph = lambda do
|
|
33
|
+
out << "<p>#{paragraph.map { |l| inline(l) }.join("\n")}</p>" if paragraph.any?
|
|
34
|
+
paragraph = []
|
|
35
|
+
end
|
|
36
|
+
flush_list = lambda do
|
|
37
|
+
out << "<#{list[:tag]}>#{list[:items].map { |i| "<li>#{inline(i)}</li>" }.join}</#{list[:tag]}>" if list
|
|
38
|
+
list = nil
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
text.each_line(chomp: true) do |line|
|
|
42
|
+
if line.strip.empty?
|
|
43
|
+
flush_paragraph.call
|
|
44
|
+
flush_list.call
|
|
45
|
+
elsif (m = line.match(/\A(\#{1,6})\s+(.*)\z/))
|
|
46
|
+
flush_paragraph.call
|
|
47
|
+
flush_list.call
|
|
48
|
+
level = m[1].length
|
|
49
|
+
out << "<h#{level}>#{inline(m[2].sub(/\s+#+\s*\z/, ""))}</h#{level}>"
|
|
50
|
+
elsif (m = line.match(/\A\s*(?:[-*+]|(\d+)[.)])\s+(.*)\z/))
|
|
51
|
+
flush_paragraph.call
|
|
52
|
+
tag = m[1] ? "ol" : "ul"
|
|
53
|
+
flush_list.call if list && list[:tag] != tag
|
|
54
|
+
list ||= {tag:, items: []}
|
|
55
|
+
list[:items] << m[2]
|
|
56
|
+
elsif list && line.start_with?(" ")
|
|
57
|
+
list[:items][-1] = "#{list[:items].last} #{line.strip}"
|
|
58
|
+
else
|
|
59
|
+
flush_list.call
|
|
60
|
+
paragraph << line.strip
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
flush_paragraph.call
|
|
64
|
+
flush_list.call
|
|
65
|
+
out
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def inline(text)
|
|
69
|
+
html = CGI.escapeHTML(text)
|
|
70
|
+
codes = []
|
|
71
|
+
html = html.gsub(/`([^`]+)`/) do
|
|
72
|
+
codes << "<code>#{Regexp.last_match(1)}</code>"
|
|
73
|
+
"\u0000#{codes.length - 1}\u0000"
|
|
74
|
+
end
|
|
75
|
+
html = html.gsub(/\[([^\]]+)\]\((https?:[^)\s]+)\)/) do
|
|
76
|
+
%(<a href="#{Regexp.last_match(2)}">#{Regexp.last_match(1)}</a>)
|
|
77
|
+
end
|
|
78
|
+
html = html.gsub(/\*\*(.+?)\*\*/, '<strong>\1</strong>')
|
|
79
|
+
html = html.gsub(/(?<![*\w])\*(?!\s)(.+?)(?<!\s)\*(?![*\w])/, '<em>\1</em>')
|
|
80
|
+
html.gsub(/\u0000(\d+)\u0000/) { codes[Regexp.last_match(1).to_i] }
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
module SolidusFoudra
|
|
7
|
+
# Takes customers' email addresses and API keys out of text bound for a
|
|
8
|
+
# log. The gem never puts them there on purpose; this is for what it
|
|
9
|
+
# can't control, such as an error message Foudra wrote about a request.
|
|
10
|
+
module Redact
|
|
11
|
+
EMAIL = /[^\s@<>"',;:()]+@[^\s@<>"',;:()]+\.[A-Za-z]{2,}/
|
|
12
|
+
KEY = /\bfd(?:r|o|or)_[A-Za-z0-9]+/
|
|
13
|
+
|
|
14
|
+
def self.call(text)
|
|
15
|
+
text.to_s.gsub(EMAIL, "[email]").gsub(KEY, "[key]")
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
|
|
6
|
+
require "openssl"
|
|
7
|
+
|
|
8
|
+
module SolidusFoudra
|
|
9
|
+
# Checks X-Foudra-Signature exactly as Foudra makes it
|
|
10
|
+
# (mrp_engine internal/events/relay.go, Sign):
|
|
11
|
+
#
|
|
12
|
+
# X-Foudra-Signature: t=<unix time>,v1=<hex HMAC-SHA256 of "<t>.<body>">
|
|
13
|
+
#
|
|
14
|
+
# keyed with the webhook's signing secret, over the body exactly as
|
|
15
|
+
# received. The time is inside the signed text, so a captured request
|
|
16
|
+
# can't be replayed later with a fresh time: we also refuse an old t.
|
|
17
|
+
module WebhookSignature
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
def valid?(header:, body:, secret:, tolerance: 300, now: Time.now)
|
|
21
|
+
return false if header.blank? || secret.blank?
|
|
22
|
+
|
|
23
|
+
parts = header.to_s.split(",").filter_map do |pair|
|
|
24
|
+
key, value = pair.strip.split("=", 2)
|
|
25
|
+
[key, value] if value
|
|
26
|
+
end.to_h
|
|
27
|
+
timestamp = Integer(parts["t"], 10)
|
|
28
|
+
given = parts["v1"].to_s
|
|
29
|
+
expected = sign(secret, timestamp, body)
|
|
30
|
+
# Constant-time: a byte-by-byte compare that stops early would tell
|
|
31
|
+
# an attacker how much of a forged signature was right.
|
|
32
|
+
ActiveSupport::SecurityUtils.secure_compare(expected, given) &&
|
|
33
|
+
(now.to_i - timestamp).abs <= tolerance
|
|
34
|
+
rescue ArgumentError, TypeError
|
|
35
|
+
false
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The hex signature; public so that tests (and a receiver's own test
|
|
39
|
+
# tool) can make one the same way Foudra does.
|
|
40
|
+
def sign(secret, timestamp, body)
|
|
41
|
+
OpenSSL::HMAC.hexdigest("SHA256", secret, "#{timestamp}.#{body}")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def header_for(secret, body, now: Time.now)
|
|
45
|
+
"t=#{now.to_i},v1=#{sign(secret, now.to_i, body)}"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2026, Kalopa Robotics Limited.
|
|
4
|
+
# Licensed under the Apache License, Version 2.0; see LICENSE for details.
|
|
5
|
+
#
|
|
6
|
+
# SolidusFoudra connects a Solidus shop to Foudra MRP. Orders go in to
|
|
7
|
+
# Foudra as sales orders; stock, products, prices and shipments come back
|
|
8
|
+
# out. Foudra owns the physical facts (what, how many, where, shipped);
|
|
9
|
+
# Solidus owns the shop, the checkout and the money. See README.md.
|
|
10
|
+
|
|
11
|
+
require "solidus_foudra/version"
|
|
12
|
+
require "solidus_foudra/configuration"
|
|
13
|
+
require "solidus_foudra/redact"
|
|
14
|
+
require "solidus_foudra/client"
|
|
15
|
+
require "solidus_foudra/webhook_signature"
|
|
16
|
+
require "solidus_foudra/markdown"
|
|
17
|
+
require "solidus_foudra/engine"
|
|
18
|
+
|
|
19
|
+
module SolidusFoudra
|
|
20
|
+
class << self
|
|
21
|
+
def configuration
|
|
22
|
+
@configuration ||= Configuration.new
|
|
23
|
+
end
|
|
24
|
+
alias_method :config, :configuration
|
|
25
|
+
|
|
26
|
+
def configure
|
|
27
|
+
yield configuration
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# For tests: forget the configuration.
|
|
31
|
+
def reset_configuration!
|
|
32
|
+
@configuration = Configuration.new
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def logger
|
|
36
|
+
configuration.logger || Rails.logger
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Every line this gem logs goes through here, so it is tagged and so
|
|
40
|
+
# there is one place that never lets an email address through.
|
|
41
|
+
def log(level, message)
|
|
42
|
+
logger.public_send(level, "[solidus_foudra] #{Redact.call(message)}")
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|