rerout 0.5.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d7253a49e955b1812e46e3d0c435999fda4c7276d5f19bad47586ac56d06247
4
- data.tar.gz: aeb26215de9a5c1e3f5de9637d348342f7cac7461179a8db7b005a438d1260ef
3
+ metadata.gz: cd1c03ec5a7c2a67ace85a5ee82286d48ab3fbb8b78899ebbc67a173c03023be
4
+ data.tar.gz: 96532142041469856e94a99d762ccd7a411200b041b5359659f07bd9994aacbb
5
5
  SHA512:
6
- metadata.gz: 1a4435295c93cc7e74812509c38d5cd7c829e004a37ec71796b3cb08b3707d7959ea4ba8d28aef0925d58bb48b00b39f8df807f4503af1fd33d880de3c4ffc04
7
- data.tar.gz: ae6b3a652f246e38ca8537d4eda5699733de2fd3dcc1ed2e102eaca3e6e61041c1d8344082da8c764cdc35aeaa7c202c2818d6f0ed223bc10df376fdd5f658e6
6
+ metadata.gz: 22c78607cd3b3c0eecaeba7807292b2576701116ac60a386033e312baa17096cfb99fd39fe488d6c39df3bf33200c5576ab018624096e3d60363755f38162b5f
7
+ data.tar.gz: c9e4ca8e9b6157574fd7200096a664bf6d0f82ddca6f7c95fed077db5cd1e5607cf90b0d17e9e421a7ebc10ba0aebc881c9f20e7e3cd7262d754b217624b35f0
data/CHANGELOG.md CHANGED
@@ -4,6 +4,26 @@ All notable changes to the `rerout` gem are documented in this file. The
4
4
  format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.7.0] - 2026-11-18
8
+
9
+ ### Added
10
+
11
+ - `CreateLinkInput` `risk_report:` option — pass `true` to have the API scan
12
+ every destination before responding. Sent as `"risk_report": true` only
13
+ when set.
14
+ - Optional `Link#risk_report` (`Rerout::Models::RiskReport`) with typed
15
+ `RiskScanEntry`, `RiskScan`, `RiskFinding`, `RiskRedirectHop`,
16
+ `RiskScanLink`, and `RiskPreviousScan` models. Status and level values are
17
+ plain strings so new values never break parsing.
18
+
19
+ ## [0.6.0] - 2026-07-13
20
+
21
+ ### Added
22
+
23
+ - Explicit isolated sandbox selection, automatic routing to
24
+ \`https://sandbox-api.rerout.co\`, and fail-closed validation that prevents
25
+ genuine \`rrk_test_\` credentials from being mixed with live keys.
26
+
7
27
  ## [0.5.0] - 2026-06-20
8
28
 
9
29
  ### Added
data/README.md CHANGED
@@ -149,6 +149,21 @@ rerout.links.update('q4', Rerout::UpdateLinkInput.new(routing_rules: [], ab_vari
149
149
 
150
150
  Routing rules and A/B variants also accept plain Hashes.
151
151
 
152
+ ### Link safety report on create
153
+
154
+ Pass `risk_report: true` to `CreateLinkInput` to have the API scan every
155
+ destination before responding. The returned `link.risk_report`
156
+ (`Rerout::Models::RiskReport`) carries the overall `status` (`"completed"`,
157
+ `"incomplete"`, or `"unavailable"` — the result then arrives by webhook),
158
+ `warning_level`, and per-destination `scans`.
159
+
160
+ ```ruby
161
+ link = rerout.links.create(
162
+ Rerout::CreateLinkInput.new(target_url: 'https://example.com', risk_report: true)
163
+ )
164
+ warn 'risky link' if %w[high critical].include?(link.risk_report&.warning_level)
165
+ ```
166
+
152
167
  ## Batch link creation
153
168
 
154
169
  ```ruby
data/lib/rerout/client.rb CHANGED
@@ -24,6 +24,7 @@ require_relative 'tags_resource'
24
24
  module Rerout
25
25
  # Default production API base URL.
26
26
  DEFAULT_BASE_URL = 'https://api.rerout.co'
27
+ SANDBOX_BASE_URL = 'https://sandbox-api.rerout.co'
27
28
 
28
29
  # Main entry point — construct one of these per project API key and re-use
29
30
  # it across requests. Thread-safe so long as the injected Faraday connection
@@ -52,21 +53,19 @@ module Rerout
52
53
 
53
54
  # @param api_key [String] project API key (`rrk_…`). Required.
54
55
  # @param base_url [String, nil] override base URL. Defaults to `https://api.rerout.co`.
56
+ # @param environment [Symbol] production or sandbox data plane.
57
+ # @param sandbox [Boolean] convenience alias for the sandbox environment.
55
58
  # @param connection [Faraday::Connection, nil] inject a Faraday connection
56
59
  # (useful for the test adapter or for sharing connection pools).
57
60
  # @param timeout [Integer] per-request timeout in seconds. Default 30.
58
61
  # @param user_agent [String, nil] override the default `User-Agent` header.
59
- def initialize(api_key:, base_url: nil, connection: nil, timeout: 30, user_agent: nil)
60
- if api_key.nil? || !api_key.is_a?(String) || api_key.strip.empty?
61
- raise Error.new(
62
- code: 'missing_api_key',
63
- message: 'A project API key is required to construct Rerout::Client.',
64
- status: 0
65
- )
66
- end
62
+ def initialize(api_key:, base_url: nil, environment: :production, sandbox: false,
63
+ connection: nil, timeout: 30, user_agent: nil)
64
+ validate_api_key!(api_key)
65
+ is_sandbox = resolve_sandbox!(api_key, environment, sandbox)
67
66
 
68
67
  @api_key = api_key
69
- @base_url = (base_url || DEFAULT_BASE_URL).to_s.sub(%r{/+\z}, '')
68
+ @base_url = (base_url || (is_sandbox ? SANDBOX_BASE_URL : DEFAULT_BASE_URL)).to_s.sub(%r{/+\z}, '')
70
69
  @timeout = timeout
71
70
  @user_agent = user_agent || "rerout-ruby/#{Rerout::VERSION}"
72
71
  @connection = connection || default_connection
@@ -105,6 +104,37 @@ module Rerout
105
104
 
106
105
  private
107
106
 
107
+ def validate_api_key!(api_key)
108
+ return if api_key.is_a?(String) && !api_key.strip.empty?
109
+
110
+ raise Error.new(
111
+ code: 'missing_api_key',
112
+ message: 'A project API key is required to construct Rerout::Client.',
113
+ status: 0
114
+ )
115
+ end
116
+
117
+ def resolve_sandbox!(api_key, environment, sandbox)
118
+ resolved_environment = sandbox ? :sandbox : environment.to_sym
119
+ unless %i[production sandbox].include?(resolved_environment)
120
+ raise Error.new(
121
+ code: 'invalid_environment',
122
+ message: 'environment must be production or sandbox.',
123
+ status: 0
124
+ )
125
+ end
126
+
127
+ is_sandbox = resolved_environment == :sandbox
128
+ is_sandbox_key = api_key.match?(/\Arrk_test_[A-Za-z0-9]{32}\z/)
129
+ return is_sandbox if is_sandbox_key == is_sandbox
130
+
131
+ raise Error.new(
132
+ code: 'wrong_environment',
133
+ message: 'The API key does not match the selected Rerout environment.',
134
+ status: 0
135
+ )
136
+ end
137
+
108
138
  def perform_request(method:, path:, query:, headers:, payload:)
109
139
  full_url = "#{@base_url}#{path}"
110
140
  @connection.public_send(method) do |req|
@@ -8,7 +8,7 @@ module Rerout
8
8
  :seo_title, :seo_description, :seo_image_url,
9
9
  :seo_canonical_url, :seo_noindex,
10
10
  :password, :max_clicks, :track_conversions,
11
- :routing_rules, :ab_variants
11
+ :routing_rules, :ab_variants, :risk_report
12
12
 
13
13
  # @param target_url [String] required, the destination URL.
14
14
  # @param domain_hostname [String, nil] verified custom domain (e.g. `go.brand.com`).
@@ -26,11 +26,13 @@ module Rerout
26
26
  # Smart Link routing rules.
27
27
  # @param ab_variants [Array<Rerout::Models::AbVariant, Hash>, nil]
28
28
  # A/B-test variants — each `{ target_url:, weight? }`.
29
+ # @param risk_report [Boolean] scan every destination before responding and
30
+ # attach the result as `Link#risk_report`. Sent only when `true`.
29
31
  def initialize(target_url:, domain_hostname: nil, code: nil, expires_at: nil,
30
32
  seo_title: nil, seo_description: nil, seo_image_url: nil,
31
33
  seo_canonical_url: nil, seo_noindex: nil,
32
34
  password: nil, max_clicks: nil, track_conversions: nil,
33
- routing_rules: nil, ab_variants: nil)
35
+ routing_rules: nil, ab_variants: nil, risk_report: false)
34
36
  raise ArgumentError, 'target_url is required' if target_url.nil? || target_url.to_s.empty?
35
37
 
36
38
  @target_url = target_url
@@ -47,6 +49,7 @@ module Rerout
47
49
  @track_conversions = track_conversions
48
50
  @routing_rules = routing_rules
49
51
  @ab_variants = ab_variants
52
+ @risk_report = risk_report
50
53
  freeze
51
54
  end
52
55
 
@@ -66,6 +69,7 @@ module Rerout
66
69
  hash['track_conversions'] = track_conversions unless track_conversions.nil?
67
70
  hash['routing_rules'] = routing_rules.map { |r| InputSerialization.rule_hash(r) } unless routing_rules.nil?
68
71
  hash['ab_variants'] = ab_variants.map { |v| InputSerialization.variant_hash(v) } unless ab_variants.nil?
72
+ hash['risk_report'] = true if risk_report
69
73
  hash
70
74
  end
71
75
  end
data/lib/rerout/models.rb CHANGED
@@ -186,7 +186,7 @@ module Rerout
186
186
  is_active seo_title seo_description seo_image_url seo_canonical_url
187
187
  seo_noindex seo_updated_at tags created_at updated_at
188
188
  password_protected max_clicks click_count track_conversions
189
- routing_rules ab_variants
189
+ routing_rules ab_variants risk_report
190
190
  ].freeze
191
191
 
192
192
  attr_reader(*ATTRS)
@@ -217,14 +217,23 @@ module Rerout
217
217
  tags: (hash['tags'] || []).map { |t| Tag.from_hash(t) },
218
218
  created_at: hash['created_at'],
219
219
  updated_at: hash['updated_at'],
220
+ **smart_attrs(hash)
221
+ )
222
+ end
223
+
224
+ # Smart Link fields plus the optional create-time risk report.
225
+ def self.smart_attrs(hash)
226
+ {
220
227
  password_protected: hash.fetch('password_protected', false),
221
228
  max_clicks: hash['max_clicks'],
222
229
  click_count: hash.fetch('click_count', 0),
223
230
  track_conversions: hash.fetch('track_conversions', false),
224
231
  routing_rules: (hash['routing_rules'] || []).map { |r| RoutingRule.from_hash(r) },
225
- ab_variants: (hash['ab_variants'] || []).map { |v| AbVariant.from_hash(v) }
226
- )
232
+ ab_variants: (hash['ab_variants'] || []).map { |v| AbVariant.from_hash(v) },
233
+ risk_report: RiskReport.parse(hash['risk_report'])
234
+ }
227
235
  end
236
+ private_class_method :smart_attrs
228
237
 
229
238
  def to_h
230
239
  ATTRS.to_h do |k|
@@ -232,6 +241,7 @@ module Rerout
232
241
  when :tags then tags.map(&:to_h)
233
242
  when :routing_rules then routing_rules.map(&:to_h)
234
243
  when :ab_variants then ab_variants.map(&:to_h)
244
+ when :risk_report then risk_report&.to_h
235
245
  else public_send(k)
236
246
  end]
237
247
  end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rerout
4
+ module Models
5
+ # Shared plumbing for the link-safety report value objects. Every field is
6
+ # optional and enum-like values stay plain strings, so unknown values from
7
+ # newer API versions never break parsing.
8
+ class RiskValue
9
+ def initialize(**attrs)
10
+ self.class::ATTRS.each { |k| instance_variable_set(:"@#{k}", attrs[k]) }
11
+ freeze
12
+ end
13
+
14
+ def self.hash_list(value)
15
+ value.is_a?(Array) ? value.grep(Hash) : []
16
+ end
17
+
18
+ def to_h
19
+ self.class::ATTRS.to_h do |k|
20
+ v = public_send(k)
21
+ [k, if v.is_a?(Array) then v.map(&:to_h)
22
+ elsif v.is_a?(RiskValue) then v.to_h
23
+ else v
24
+ end]
25
+ end
26
+ end
27
+
28
+ def ==(other)
29
+ other.instance_of?(self.class) && other.to_h == to_h
30
+ end
31
+ alias eql? ==
32
+
33
+ def hash
34
+ [self.class, to_h].hash
35
+ end
36
+ end
37
+
38
+ # One hop in the redirect chain followed while scanning — `{ url:, status: }`.
39
+ class RiskRedirectHop < RiskValue
40
+ ATTRS = %i[url status].freeze
41
+ attr_reader(*ATTRS)
42
+
43
+ def self.from_hash(hash)
44
+ new(url: hash['url'], status: hash['status'])
45
+ end
46
+ end
47
+
48
+ # A single finding raised by a scan.
49
+ class RiskFinding < RiskValue
50
+ ATTRS = %i[code category level confidence description].freeze
51
+ attr_reader(*ATTRS)
52
+
53
+ def self.from_hash(hash)
54
+ new(**ATTRS.to_h { |k| [k, hash[k.to_s]] })
55
+ end
56
+ end
57
+
58
+ # The result of scanning one destination.
59
+ class RiskScan < RiskValue
60
+ ATTRS = %i[
61
+ id trigger final_url redirect_chain status warning_level confidence
62
+ coverage verdict summary findings engine_version completed_at report_url
63
+ ].freeze
64
+ attr_reader(*ATTRS)
65
+
66
+ def initialize(**attrs)
67
+ super(**attrs.merge(redirect_chain: (attrs[:redirect_chain] || []).freeze,
68
+ findings: (attrs[:findings] || []).freeze))
69
+ end
70
+
71
+ def self.from_hash(hash)
72
+ attrs = ATTRS.to_h { |k| [k, hash[k.to_s]] }
73
+ attrs[:redirect_chain] = hash_list(hash['redirect_chain']).map { |h| RiskRedirectHop.from_hash(h) }
74
+ attrs[:findings] = hash_list(hash['findings']).map { |f| RiskFinding.from_hash(f) }
75
+ new(**attrs)
76
+ end
77
+ end
78
+
79
+ # Which link and destination a scan covered.
80
+ class RiskScanLink < RiskValue
81
+ ATTRS = %i[code short_url revision destination_kind destination_index destination_url].freeze
82
+ attr_reader(*ATTRS)
83
+
84
+ def self.from_hash(hash)
85
+ new(**ATTRS.to_h { |k| [k, hash[k.to_s]] })
86
+ end
87
+ end
88
+
89
+ # The previous scan of the same destination — `{ scan_id:, warning_level: }`.
90
+ class RiskPreviousScan < RiskValue
91
+ ATTRS = %i[scan_id warning_level].freeze
92
+ attr_reader(*ATTRS)
93
+
94
+ def self.from_hash(hash)
95
+ new(scan_id: hash['scan_id'], warning_level: hash['warning_level'])
96
+ end
97
+ end
98
+
99
+ # One entry of `RiskReport#scans`: the scan, the destination it covered,
100
+ # and the previous scan (or `nil`).
101
+ class RiskScanEntry < RiskValue
102
+ ATTRS = %i[scan link previous].freeze
103
+ attr_reader(*ATTRS)
104
+
105
+ def self.from_hash(hash)
106
+ scan = hash['scan'].is_a?(Hash) ? hash['scan'] : {}
107
+ link = hash['link'].is_a?(Hash) ? hash['link'] : {}
108
+ previous = hash['previous']
109
+ new(
110
+ scan: RiskScan.from_hash(scan),
111
+ link: RiskScanLink.from_hash(link),
112
+ previous: previous.is_a?(Hash) ? RiskPreviousScan.from_hash(previous) : nil
113
+ )
114
+ end
115
+ end
116
+
117
+ # Synchronous link-safety report, returned by `links.create` when
118
+ # `risk_report: true` was requested. `status` is `"completed"`,
119
+ # `"incomplete"` or `"unavailable"`; `message` is only set when the
120
+ # scanner was unavailable and the result will arrive by webhook.
121
+ class RiskReport < RiskValue
122
+ ATTRS = %i[status warning_level message scans].freeze
123
+ attr_reader(*ATTRS)
124
+
125
+ def initialize(**attrs)
126
+ super(**attrs.merge(scans: (attrs[:scans] || []).freeze))
127
+ end
128
+
129
+ # Parse the `risk_report` field of a link, or `nil` when absent.
130
+ def self.parse(value)
131
+ value.is_a?(Hash) ? from_hash(value) : nil
132
+ end
133
+
134
+ def self.from_hash(hash)
135
+ new(
136
+ status: hash['status'],
137
+ warning_level: hash['warning_level'],
138
+ message: hash['message'],
139
+ scans: hash_list(hash['scans']).map { |e| RiskScanEntry.from_hash(e) }
140
+ )
141
+ end
142
+ end
143
+ end
144
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Rerout
4
4
  # Library version. Follows semantic versioning.
5
- VERSION = '0.5.0'
5
+ VERSION = '0.7.0'
6
6
  end
data/lib/rerout.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative 'rerout/version'
4
4
  require_relative 'rerout/error'
5
5
  require_relative 'rerout/models'
6
+ require_relative 'rerout/risk_report'
6
7
  require_relative 'rerout/input_serialization'
7
8
  require_relative 'rerout/create_link_input'
8
9
  require_relative 'rerout/update_link_input'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rerout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Codecraft Solutions
@@ -119,6 +119,7 @@ files:
119
119
  - lib/rerout/project.rb
120
120
  - lib/rerout/qr.rb
121
121
  - lib/rerout/qr_options.rb
122
+ - lib/rerout/risk_report.rb
122
123
  - lib/rerout/tags_resource.rb
123
124
  - lib/rerout/update_link_input.rb
124
125
  - lib/rerout/update_tag_input.rb