flexops 1.1.0 → 2.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.
@@ -20,16 +20,30 @@ module FlexOps
20
20
  @http.get("#{ws_path}/webhooks")
21
21
  end
22
22
 
23
- def get(webhook_id)
24
- @http.get("#{ws_path}/webhooks/#{webhook_id}")
25
- end
26
-
23
+ # Creates a webhook subscription.
24
+ #
25
+ # request:: a hash with +url+, optional +description+ and optional +events+, an
26
+ # array of FlexOps::WebhookEventType names (strings or symbols). +events+
27
+ # is sent as the Gateway's comma-separated +eventTypes+; leave it out to
28
+ # receive every event. A raw +eventTypes+ string ("*" or comma-separated
29
+ # names, no spaces) is sent unchanged instead.
30
+ #
31
+ # Raises ArgumentError, before any request, when +events+ is empty, is not an
32
+ # array, names an event FlexOps does not send, or is given with +eventTypes+.
33
+ #
34
+ # Returns the new subscription itself ("id", "url", "eventTypes", "status",
35
+ # "secret", ...), not a success/data envelope: the Gateway answers 201 with the
36
+ # bare subscription. An enveloped answer is unwrapped, so "secret" (the whsec_...
37
+ # signing secret, returned only here) is always at the top level.
27
38
  def create(request)
28
- @http.post("#{ws_path}/webhooks", body: request)
39
+ unwrap_subscription(@http.post("#{ws_path}/webhooks", body: to_wire_request(request)))
29
40
  end
30
41
 
42
+ # Updates a webhook subscription. +data+ takes +url+, +description+, +status+
43
+ # ("Active", "Paused" or "Disabled") and +events+, which replaces the subscribed
44
+ # events and is sent as +eventTypes+. Raises ArgumentError as #create does.
31
45
  def update(webhook_id, data)
32
- @http.put("#{ws_path}/webhooks/#{webhook_id}", body: data)
46
+ @http.put("#{ws_path}/webhooks/#{webhook_id}", body: to_wire_request(data))
33
47
  end
34
48
 
35
49
  def delete(webhook_id)
@@ -44,9 +58,74 @@ module FlexOps
44
58
  @http.get("#{ws_path}/webhooks/#{webhook_id}/deliveries")
45
59
  end
46
60
 
47
- def self.verify_signature(payload, signature, secret)
48
- expected = OpenSSL::HMAC.hexdigest("SHA256", secret, payload)
49
- OpenSSL.secure_compare(expected, signature)
61
+ # Sends a test event (X-FlexOps-Event "test") to the subscription's URL.
62
+ def test(webhook_id)
63
+ @http.post("#{ws_path}/webhooks/#{webhook_id}/test")
64
+ end
65
+
66
+ # Headers FlexOps sends with every webhook delivery.
67
+ EVENT_HEADER = "X-FlexOps-Event" # event name; "test" for a test event
68
+ EVENT_ID_HEADER = "X-FlexOps-Event-Id"
69
+ TIMESTAMP_HEADER = "X-FlexOps-Timestamp" # Unix seconds, part of the signed string
70
+ SIGNATURE_HEADER = "X-FlexOps-Signature" # "sha256=" + lowercase hex HMAC-SHA256
71
+ DELIVERY_HEADER = "X-FlexOps-Delivery"
72
+
73
+ # Seconds the X-FlexOps-Timestamp may be from now, either way.
74
+ DEFAULT_TOLERANCE = 300
75
+
76
+ SIGNATURE_PREFIX = "sha256="
77
+ private_constant :SIGNATURE_PREFIX
78
+
79
+ # Returns true when a FlexOps webhook delivery is authentic and fresh,
80
+ # false otherwise. See verify_signature! for the arguments.
81
+ def self.verify_signature(payload:, signature:, timestamp:, secret:, tolerance: DEFAULT_TOLERANCE, now: Time.now)
82
+ verify_signature!(payload: payload, signature: signature, timestamp: timestamp,
83
+ secret: secret, tolerance: tolerance, now: now)
84
+ rescue FlexOps::WebhookSignatureError
85
+ false
86
+ end
87
+
88
+ # Verifies a FlexOps webhook delivery and returns true, or raises
89
+ # FlexOps::WebhookSignatureError saying why.
90
+ #
91
+ # payload:: the raw request body exactly as received (verify before parsing it)
92
+ # signature:: the X-FlexOps-Signature value, "sha256=<hex>" (bare hex is accepted)
93
+ # timestamp:: the X-FlexOps-Timestamp value, Unix seconds
94
+ # secret:: the subscription secret ("whsec_..."), used as-is
95
+ # tolerance:: seconds the timestamp may be from +now+; non-positive keeps the default
96
+ # now:: the current time (a Time or Unix seconds), for tests
97
+ #
98
+ # FlexOps signs "{timestamp}.{body}" with HMAC-SHA256; the comparison is
99
+ # constant time.
100
+ def self.verify_signature!(payload:, signature:, timestamp:, secret:, tolerance: DEFAULT_TOLERANCE, now: Time.now)
101
+ if secret.nil? || secret.to_s.empty?
102
+ raise FlexOps::WebhookSignatureError.new("Webhook secret is empty", code: "WEBHOOK_MISSING_SECRET")
103
+ end
104
+ if signature.nil? || signature.to_s.empty?
105
+ raise FlexOps::WebhookSignatureError.new("Webhook signature header is missing", code: "WEBHOOK_MISSING_SIGNATURE")
106
+ end
107
+
108
+ timestamp = timestamp.to_s
109
+ unless timestamp.match?(/\A\d+\z/)
110
+ raise FlexOps::WebhookSignatureError.new("Webhook timestamp header is missing or not Unix seconds",
111
+ code: "WEBHOOK_INVALID_TIMESTAMP")
112
+ end
113
+
114
+ tolerance = DEFAULT_TOLERANCE unless tolerance.is_a?(Numeric) && tolerance.positive?
115
+ if (now.to_i - timestamp.to_i).abs > tolerance
116
+ raise FlexOps::WebhookSignatureError.new("Webhook timestamp is outside the tolerance",
117
+ code: "WEBHOOK_TIMESTAMP_OUTSIDE_TOLERANCE")
118
+ end
119
+
120
+ signed = "#{timestamp}.".b << payload.to_s.b
121
+ expected = OpenSSL::HMAC.hexdigest("SHA256", secret.to_s, signed)
122
+ received = signature.to_s.delete_prefix(SIGNATURE_PREFIX).downcase
123
+ # OpenSSL.secure_compare hashes both sides first, so unequal lengths are safe.
124
+ unless OpenSSL.secure_compare(expected, received)
125
+ raise FlexOps::WebhookSignatureError.new("Webhook signature does not match", code: "WEBHOOK_SIGNATURE_MISMATCH")
126
+ end
127
+
128
+ true
50
129
  end
51
130
 
52
131
  private
@@ -54,6 +133,50 @@ module FlexOps
54
133
  def ws_path
55
134
  "/api/workspaces/#{@ws_id.call}"
56
135
  end
136
+
137
+ # The Gateway reads a comma-separated eventTypes string, ignores an events field
138
+ # (subscribing to every event) and matches each name exactly, so a name it never
139
+ # sends would leave the subscription silent; it is refused here instead.
140
+ def to_wire_request(request)
141
+ return request unless request.is_a?(Hash)
142
+
143
+ events_key = [:events, "events"].find { |key| request.key?(key) }
144
+ return request if events_key.nil?
145
+
146
+ body = request.reject { |key, _| key == events_key }
147
+ events = request[events_key]
148
+ return body if events.nil?
149
+
150
+ if [:eventTypes, "eventTypes"].any? { |key| body.key?(key) }
151
+ raise ArgumentError, "Pass events or eventTypes, not both."
152
+ end
153
+
154
+ body.merge(eventTypes: to_event_types(events))
155
+ end
156
+
157
+ def unwrap_subscription(response)
158
+ return response unless response.is_a?(Hash) && response.key?("success") && response["data"].is_a?(Hash)
159
+
160
+ response["data"]
161
+ end
162
+
163
+ def to_event_types(events)
164
+ unless events.is_a?(Array)
165
+ raise ArgumentError, "events must be an array of event names, not #{events.class}."
166
+ end
167
+ if events.empty?
168
+ raise ArgumentError, "events must name at least one event; leave it out to receive every event."
169
+ end
170
+
171
+ names = events.map { |event| event.is_a?(String) || event.is_a?(Symbol) ? event.to_s : event.inspect }.uniq
172
+ unknown = names - FlexOps::WebhookEventType::ALL
173
+ unless unknown.empty?
174
+ raise ArgumentError, "Unknown webhook event: #{unknown.join(", ")}. " \
175
+ "FlexOps sends #{FlexOps::WebhookEventType::ALL.join(", ")}."
176
+ end
177
+
178
+ names.join(",")
179
+ end
57
180
  end
58
181
  end
59
182
  end
@@ -23,30 +23,25 @@ module FlexOps
23
23
  @http.get("/api/workspaces/#{id}")
24
24
  end
25
25
 
26
- def create(request)
27
- @http.post("/api/workspaces", body: request)
28
- end
29
-
30
- def update(data)
31
- @http.put("/api/workspaces/#{@ws_id.call}", body: data)
32
- end
33
-
34
26
  def list_members
35
27
  @http.get("/api/workspaces/#{@ws_id.call}/members")
36
28
  end
37
29
 
38
- def invite_member(email:, role: nil)
39
- body = { email: email }
40
- body[:role] = role if role
41
- @http.post("/api/workspaces/#{@ws_id.call}/members/invite", body: body)
30
+ # Invites a member. +role+ is a FlexOps::WorkspaceRole number or name (for example
31
+ # :member); it is required because the Gateway reads a missing role as Owner.
32
+ def invite_member(email:, role:)
33
+ @http.post("/api/workspaces/#{@ws_id.call}/members/invite",
34
+ body: { email: email, role: FlexOps::WorkspaceRole.to_wire(role) })
42
35
  end
43
36
 
44
37
  def remove_member(user_id)
45
38
  @http.delete("/api/workspaces/#{@ws_id.call}/members/#{user_id}")
46
39
  end
47
40
 
41
+ # Changes a member's role; +role+ is a FlexOps::WorkspaceRole number or name.
48
42
  def update_member_role(user_id, role)
49
- @http.put("/api/workspaces/#{@ws_id.call}/members/#{user_id}/role", body: { role: role })
43
+ @http.put("/api/workspaces/#{@ws_id.call}/members/#{user_id}/role",
44
+ body: { newRole: FlexOps::WorkspaceRole.to_wire(role) })
50
45
  end
51
46
  end
52
47
  end
@@ -7,5 +7,5 @@
7
7
  # ***********************************************************************
8
8
 
9
9
  module FlexOps
10
- VERSION = "1.1.0"
10
+ VERSION = "2.1.0"
11
11
  end
@@ -0,0 +1,25 @@
1
+ # ***********************************************************************
2
+ # Package : flexops
3
+ # Author : FlexOps, LLC
4
+ # Created : 2026-10-16
5
+ #
6
+ # Copyright (c) 2021-2026 by FlexOps, LLC. All rights reserved.
7
+ # ***********************************************************************
8
+
9
+ module FlexOps
10
+ # Event names FlexOps sends in the X-FlexOps-Event header and matches webhook
11
+ # subscriptions against.
12
+ #
13
+ # Names are PascalCase and matched exactly: a dotted name such as "label.created"
14
+ # matches nothing. TRACKING_UPDATED and TRACKING_DELIVERED are sent only to
15
+ # workspaces in the USPS push-tracking pilot.
16
+ module WebhookEventType
17
+ LABEL_CREATED = "LabelCreated"
18
+ LABEL_CANCELED = "LabelCanceled"
19
+ BATCH_COMPLETED = "BatchCompleted"
20
+ TRACKING_UPDATED = "TrackingUpdated"
21
+ TRACKING_DELIVERED = "TrackingDelivered"
22
+
23
+ ALL = [LABEL_CREATED, LABEL_CANCELED, BATCH_COMPLETED, TRACKING_UPDATED, TRACKING_DELIVERED].freeze
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ # ***********************************************************************
2
+ # Package : flexops
3
+ # Author : FlexOps, LLC
4
+ # Created : 2026-10-19
5
+ #
6
+ # Copyright (c) 2021-2026 by FlexOps, LLC. All rights reserved.
7
+ # ***********************************************************************
8
+
9
+ module FlexOps
10
+ # Workspace member role, sent as the Gateway WorkspaceRole number.
11
+ module WorkspaceRole
12
+ OWNER = 0
13
+ ADMIN = 1
14
+ MEMBER = 2
15
+ MERCHANT = 3
16
+
17
+ ALL = { OWNER: OWNER, ADMIN: ADMIN, MEMBER: MEMBER, MERCHANT: MERCHANT }.freeze
18
+
19
+ # Returns the wire number for a role given as a number or a name (:member, "Admin").
20
+ # Raises ArgumentError, before any request, for anything else.
21
+ def self.to_wire(role)
22
+ return role if role.is_a?(Integer) && ALL.value?(role)
23
+
24
+ if role.is_a?(String) || role.is_a?(Symbol)
25
+ number = ALL[role.to_s.upcase.to_sym]
26
+ return number unless number.nil?
27
+ end
28
+
29
+ raise ArgumentError, "Unknown workspace role: #{role.inspect}. Use one of #{ALL.keys.join(", ")} or 0-3."
30
+ end
31
+ end
32
+ end
data/lib/flexops.rb CHANGED
@@ -8,6 +8,9 @@
8
8
 
9
9
  require_relative "flexops/version"
10
10
  require_relative "flexops/errors"
11
+ require_relative "flexops/hazmat_classification"
12
+ require_relative "flexops/webhook_event_type"
13
+ require_relative "flexops/workspace_role"
11
14
  require_relative "flexops/http"
12
15
  require_relative "flexops/client"
13
16
  require_relative "flexops/resources/auth"
@@ -18,18 +21,17 @@ require_relative "flexops/resources/webhooks"
18
21
  require_relative "flexops/resources/wallet"
19
22
  require_relative "flexops/resources/insurance"
20
23
  require_relative "flexops/resources/returns"
21
- require_relative "flexops/resources/api_keys"
22
24
  require_relative "flexops/resources/analytics"
23
25
  require_relative "flexops/resources/orders"
24
26
  require_relative "flexops/resources/inventory"
25
27
  require_relative "flexops/resources/pickups"
26
28
  require_relative "flexops/resources/scan_forms"
27
29
  require_relative "flexops/resources/rules"
28
- require_relative "flexops/resources/offsets"
29
30
  require_relative "flexops/resources/hs_codes"
31
+ require_relative "flexops/resources/carbon_offsets"
30
32
  require_relative "flexops/resources/recurring_shipments"
33
+ require_relative "flexops/resources/report_schedules"
31
34
  require_relative "flexops/resources/email_templates"
32
- require_relative "flexops/resources/reports"
33
35
 
34
36
  module FlexOps
35
37
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexops
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - FlexOps, LLC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-09-19 00:00:00.000000000 Z
11
+ date: 2026-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -66,20 +66,20 @@ files:
66
66
  - lib/flexops.rb
67
67
  - lib/flexops/client.rb
68
68
  - lib/flexops/errors.rb
69
+ - lib/flexops/hazmat_classification.rb
69
70
  - lib/flexops/http.rb
70
71
  - lib/flexops/resources/analytics.rb
71
- - lib/flexops/resources/api_keys.rb
72
72
  - lib/flexops/resources/auth.rb
73
+ - lib/flexops/resources/carbon_offsets.rb
73
74
  - lib/flexops/resources/carriers.rb
74
75
  - lib/flexops/resources/email_templates.rb
75
76
  - lib/flexops/resources/hs_codes.rb
76
77
  - lib/flexops/resources/insurance.rb
77
78
  - lib/flexops/resources/inventory.rb
78
- - lib/flexops/resources/offsets.rb
79
79
  - lib/flexops/resources/orders.rb
80
80
  - lib/flexops/resources/pickups.rb
81
81
  - lib/flexops/resources/recurring_shipments.rb
82
- - lib/flexops/resources/reports.rb
82
+ - lib/flexops/resources/report_schedules.rb
83
83
  - lib/flexops/resources/returns.rb
84
84
  - lib/flexops/resources/rules.rb
85
85
  - lib/flexops/resources/scan_forms.rb
@@ -88,6 +88,8 @@ files:
88
88
  - lib/flexops/resources/webhooks.rb
89
89
  - lib/flexops/resources/workspaces.rb
90
90
  - lib/flexops/version.rb
91
+ - lib/flexops/webhook_event_type.rb
92
+ - lib/flexops/workspace_role.rb
91
93
  homepage: https://github.com/BillEisenman/flexops-sdk-ruby
92
94
  licenses:
93
95
  - MIT
@@ -1,40 +0,0 @@
1
- # ***********************************************************************
2
- # Package : flexops
3
- # Author : FlexOps, LLC
4
- # Created : 2026-03-08
5
- #
6
- # Copyright (c) 2021-2026 by FlexOps, LLC. All rights reserved.
7
- # ***********************************************************************
8
-
9
- module FlexOps
10
- module Resources
11
- class ApiKeys
12
- def initialize(http, ws_id_proc)
13
- @http = http
14
- @ws_id = ws_id_proc
15
- end
16
-
17
- def list
18
- @http.get("#{ws_path}/api-keys")
19
- end
20
-
21
- def create(request)
22
- @http.post("#{ws_path}/api-keys", body: request)
23
- end
24
-
25
- def revoke(key_id)
26
- @http.delete("#{ws_path}/api-keys/#{key_id}")
27
- end
28
-
29
- def rotate(key_id)
30
- @http.post("#{ws_path}/api-keys/#{key_id}/rotate")
31
- end
32
-
33
- private
34
-
35
- def ws_path
36
- "/api/workspaces/#{@ws_id.call}"
37
- end
38
- end
39
- end
40
- end
@@ -1,38 +0,0 @@
1
- # ***********************************************************************
2
- # Package : flexops
3
- # Author : FlexOps, LLC
4
- # Created : 2026-03-31
5
- #
6
- # Copyright (c) 2021-2026 by FlexOps, LLC. All rights reserved.
7
- # ***********************************************************************
8
-
9
- # frozen_string_literal: true
10
-
11
- module FlexOps
12
- module Resources
13
- class Offsets
14
- def initialize(http, ws_id_proc)
15
- @http = http
16
- @ws_id = ws_id_proc
17
- end
18
-
19
- def offset(label_id)
20
- @http.post("#{ws_path}/shipping/labels/#{label_id}/offset")
21
- end
22
-
23
- def get_emissions(label_id)
24
- @http.get("#{ws_path}/shipping/labels/#{label_id}/emissions")
25
- end
26
-
27
- def batch_offset(label_ids)
28
- @http.post("#{ws_path}/shipping/labels/offset/batch", body: { labelIds: label_ids })
29
- end
30
-
31
- private
32
-
33
- def ws_path
34
- "/api/workspaces/#{@ws_id.call}"
35
- end
36
- end
37
- end
38
- end
@@ -1,46 +0,0 @@
1
- # ***********************************************************************
2
- # Package : flexops
3
- # Author : FlexOps, LLC
4
- # Created : 2026-03-31
5
- #
6
- # Copyright (c) 2021-2026 by FlexOps, LLC. All rights reserved.
7
- # ***********************************************************************
8
-
9
- # frozen_string_literal: true
10
-
11
- module FlexOps
12
- module Resources
13
- class Reports
14
- def initialize(http, ws_id_proc)
15
- @http = http
16
- @ws_id = ws_id_proc
17
- end
18
-
19
- def list
20
- @http.get("#{ws_path}/report-schedules/")
21
- end
22
-
23
- def get(id)
24
- @http.get("#{ws_path}/report-schedules/#{id}")
25
- end
26
-
27
- def create(request)
28
- @http.post("#{ws_path}/report-schedules/", body: request)
29
- end
30
-
31
- def update(id, request)
32
- @http.put("#{ws_path}/report-schedules/#{id}", body: request)
33
- end
34
-
35
- def delete(id)
36
- @http.delete("#{ws_path}/report-schedules/#{id}")
37
- end
38
-
39
- private
40
-
41
- def ws_path
42
- "/api/workspaces/#{@ws_id.call}"
43
- end
44
- end
45
- end
46
- end