scanii-ruby 0.0.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a216ea149bec56b29bd6b1a9337b07c31084a9387aaf74be336b6e0c08a53145
4
- data.tar.gz: 24b8201eb8ed2cf9596ab5ca94850107f908b2268dc59edc6dc0f9c78d099db3
3
+ metadata.gz: '0418285adf3f56f3389b379b9eedadd5b281591cd2c7e62ad05092a8e8b22595'
4
+ data.tar.gz: a61ce21d24bc78f557e09394868fc0c50b9fb2777b8649dce59b017a7d6a427c
5
5
  SHA512:
6
- metadata.gz: b4e5e75b11d1989195d0d46399ad9e906883bb06bba7b8040cc32931b8cfee9347a3bcac01e571671bcb619a97f5be368b9bf41855ad837053b789468a3a96b8
7
- data.tar.gz: d8df27af608f542892c73a4b2a93de77a3032e4d5f1f0a3e978df73164185caac083290bea6b0f6a8102b7847810ce36eb3b565d80ec2c558b9b95312626f127
6
+ metadata.gz: 504165ee4f450fa8266675e1eb9d43d54a44e76547a5a8fe870865b4123c1563a4d9bf693df893855ecef9197288beb0e5da3425bf3a4524aa76cc1d02bbec25
7
+ data.tar.gz: 897b47ed149ea571105dde92462e61d69f43fd496f20a50200dc64d0bce2e2ab7b52ade2837de7d5d6ac5a0f25f5de93f98f0d0ad5781f474b076d8c76c874e0
data/CHANGELOG.md ADDED
@@ -0,0 +1,65 @@
1
+ # Changelog
2
+
3
+ All notable changes to `scanii-ruby` are documented here. Versions follow [SemVer](https://semver.org).
4
+
5
+ ## 1.1.0 — Streaming standardization
6
+
7
+ Adds stream-based `process` and `process_async` methods, aligning scanii-ruby with the
8
+ cross-SDK streaming standard. File content is now truly streamed to the socket via
9
+ `Net::HTTP#body_stream=` rather than buffered into a single String.
10
+
11
+ ### New API
12
+
13
+ - `Scanii::Client#process(io, filename:, content_type: nil, metadata: nil, callback: nil)` →
14
+ `Scanii::ProcessingResult` — accepts any IO-like object (anything responding to `read(n)`).
15
+ Both `File` (opened with `File.open(path, "rb")`) and `StringIO` work.
16
+ - `Scanii::Client#process_file(path, metadata: nil, callback: nil)` →
17
+ `Scanii::ProcessingResult` — convenience wrapper that opens the file in binary mode and
18
+ delegates to `process`. This is the replacement for the old `process(path, ...)` form.
19
+ - Same shapes for `process_async` / `process_async_file`.
20
+
21
+ ### Deprecations
22
+
23
+ - `process(path_string, ...)` — passing a String path to `process` is deprecated; use
24
+ `process_file(path)` instead. The old form still works and emits a runtime `warn`. Will be
25
+ removed in a future major version.
26
+ - `process_async(path_string, ...)` — same; use `process_async_file(path)`. Will be removed
27
+ in a future major version.
28
+
29
+ ### Internals
30
+
31
+ - `Scanii::Multipart.stream_encode` replaces the old `encode`. Returns a `[ChainedIO,
32
+ content_type, content_length]` triple. `ChainedIO` reads prologue → caller IO → epilogue
33
+ without ever buffering the full body.
34
+
35
+ ## 1.0.1 — Release infrastructure fix
36
+
37
+ Wires up `bundler/gem_tasks` in the Rakefile so `bundle exec rake release` (invoked by `rubygems/release-gem@v1`) resolves correctly. v1.0.0 was tagged but never published to RubyGems because the release workflow failed at the `rake release` task lookup; v1.0.1 is functionally identical to that tag. No SDK behavior changes.
38
+
39
+ ## 1.0.0 — Initial release
40
+
41
+ First public release of the Scanii Ruby SDK on RubyGems as `scanii-ruby` (the plain `scanii` gem name is held by an abandoned third-party gem; this SDK uses the `scanii-ruby` convention matching `twilio-ruby`). Supersedes the `scanii-ruby 0.0.1` namespace-reservation placeholder.
42
+
43
+ Reference implementation frozen at scanii-java v8.0.0.
44
+
45
+ ### API surface
46
+
47
+ - `Scanii::Client#process(file_path, metadata:, callback:)` → `Scanii::ProcessingResult`
48
+ - `Scanii::Client#process_async(file_path, metadata:, callback:)` → `Scanii::PendingResult`
49
+ - `Scanii::Client#fetch(url, metadata:, callback:)` → `Scanii::PendingResult`
50
+ - `Scanii::Client#retrieve(id)` → `Scanii::ProcessingResult`
51
+ - `Scanii::Client#ping` → `true`
52
+ - `Scanii::Client#create_auth_token(timeout_seconds)` → `Scanii::AuthToken`
53
+ - `Scanii::Client#retrieve_auth_token(id)` → `Scanii::AuthToken`
54
+ - `Scanii::Client#delete_auth_token(id)` → `true`
55
+
56
+ Errors: `Scanii::Error` (base), `Scanii::AuthError` (401/403), `Scanii::RateLimitError` (429, with `retry_after`).
57
+
58
+ ### Highlights
59
+
60
+ - **Zero runtime dependencies.** Stdlib `net/http` + `json` + `securerandom` + `base64` only.
61
+ - **Hand-rolled multipart/form-data encoder** (no `multipart-post`, no `rack-mime`).
62
+ - **Synchronous.** Single-threaded by default; create one `Scanii::Client` per thread.
63
+ - **Targets Ruby 3.4+.**
64
+ - **scanii-cli** integration tests cover Linux / macOS / Windows on Ruby 3.4 and 4.0 without burning real Scanii credits. (Ruby 3.5 was preview-only and has no rubyinstaller2 build for Windows.)
65
+ - **OIDC trusted publishing** to RubyGems via `rubygems/release-gem` — no long-lived API key in repo secrets.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md CHANGED
@@ -1,5 +1,147 @@
1
- # scanii-ruby (placeholder)
1
+ # scanii-ruby
2
2
 
3
- The official Scanii Ruby SDK is being released as `scanii-ruby` v1.0.0 shortly.
3
+ Official Ruby SDK for the [Scanii](https://www.scanii.com) content security API.
4
4
 
5
- In the meantime, see [scanii.com](https://scanii.com) and [github.com/scanii/scanii-ruby](https://github.com/scanii/scanii-ruby).
5
+ [![Gem Version](https://img.shields.io/gem/v/scanii-ruby.svg)](https://rubygems.org/gems/scanii-ruby)
6
+
7
+ > **Looking for `scanii`?** That gem name is held by an abandoned third-party gem. The official Scanii Ruby SDK is published as **`scanii-ruby`** (matching the [`twilio-ruby`](https://rubygems.org/gems/twilio-ruby) convention).
8
+
9
+ ## SDK Principles
10
+
11
+ 1. **Light.** Zero runtime dependencies, stdlib only.
12
+ 2. **Up to date.** Always current with the latest Scanii API.
13
+ 3. **Integration-only.** Wraps the REST API — retries, concurrency, and batching are the caller's responsibility.
14
+
15
+ ## Install
16
+
17
+ Add to your `Gemfile`:
18
+
19
+ ```ruby
20
+ gem "scanii-ruby", "~> 1.0"
21
+ ```
22
+
23
+ Or install directly:
24
+
25
+ ```bash
26
+ gem install scanii-ruby
27
+ ```
28
+
29
+ Targets Ruby 3.4+. Zero runtime dependencies.
30
+
31
+ ## Quickstart
32
+
33
+ Scan a file from disk:
34
+
35
+ ```ruby
36
+ require "scanii"
37
+
38
+ client = Scanii::Client.new(key: "your-key", secret: "your-secret")
39
+
40
+ result = client.process_file("./file.pdf")
41
+ puts "findings: #{result.findings.inspect}"
42
+ ```
43
+
44
+ Scan content already in memory (no temp file needed):
45
+
46
+ ```ruby
47
+ require "stringio"
48
+
49
+ result = client.process(StringIO.new(bytes), filename: "upload.bin")
50
+ puts "findings: #{result.findings.inspect}"
51
+ ```
52
+
53
+ `findings` is an `Array<String>`. An empty array means the content is clean.
54
+
55
+ ## API
56
+
57
+ | Method | REST | Returns |
58
+ |---|---|---|
59
+ | `process(io, filename:, content_type:, metadata:, callback:)` | `POST /files` | `Scanii::ProcessingResult` |
60
+ | `process_file(path, metadata:, callback:)` | `POST /files` | `Scanii::ProcessingResult` |
61
+ | `process_async(io, filename:, content_type:, metadata:, callback:)` | `POST /files/async` | `Scanii::PendingResult` |
62
+ | `process_async_file(path, metadata:, callback:)` | `POST /files/async` | `Scanii::PendingResult` |
63
+ | `fetch(url, metadata:, callback:)` | `POST /files/fetch` | `Scanii::PendingResult` |
64
+ | `retrieve(id)` | `GET /files/{id}` | `Scanii::ProcessingResult` |
65
+ | `ping` | `GET /ping` | `true` |
66
+ | `create_auth_token(timeout_seconds)` | `POST /auth/tokens` | `Scanii::AuthToken` |
67
+ | `retrieve_auth_token(id)` | `GET /auth/tokens/{id}` | `Scanii::AuthToken` |
68
+ | `delete_auth_token(id)` | `DELETE /auth/tokens/{id}` | `true` |
69
+
70
+ Full API reference: <https://scanii.github.io/openapi/v22/>.
71
+
72
+ ## Regional endpoints
73
+
74
+ ```ruby
75
+ client = Scanii::Client.new(
76
+ key: "k",
77
+ secret: "s",
78
+ endpoint: "https://api-eu1.scanii.com"
79
+ )
80
+ ```
81
+
82
+ | Region | Endpoint |
83
+ |---|---|
84
+ | Auto (default) | `https://api.scanii.com` |
85
+ | US 1 | `https://api-us1.scanii.com` |
86
+ | EU 1 | `https://api-eu1.scanii.com` |
87
+ | EU 2 | `https://api-eu2.scanii.com` |
88
+ | AP 1 | `https://api-ap1.scanii.com` |
89
+ | AP 2 | `https://api-ap2.scanii.com` |
90
+ | CA 1 | `https://api-ca1.scanii.com` |
91
+
92
+ ## Errors
93
+
94
+ ```ruby
95
+ require "scanii"
96
+
97
+ begin
98
+ client.ping
99
+ rescue Scanii::AuthError => e
100
+ warn "bad creds: #{e.message} (request_id=#{e.request_id})"
101
+ rescue Scanii::RateLimitError => e
102
+ warn "rate-limited; retry after #{e.retry_after}s"
103
+ rescue Scanii::Error => e
104
+ warn "other error: #{e.message} (status=#{e.status_code})"
105
+ end
106
+ ```
107
+
108
+ Error hierarchy:
109
+
110
+ - `Scanii::Error` — base class, carries `status_code`, `request_id`, `host_id`, `body`
111
+ - `Scanii::AuthError` — HTTP 401 / 403
112
+ - `Scanii::RateLimitError` — HTTP 429, with `retry_after` (seconds, when the server provided `Retry-After`)
113
+
114
+ Per SDK Principle 3, the SDK does not retry on the caller's behalf — backoff and retry policy belong to your application.
115
+
116
+ ## Auth tokens
117
+
118
+ Mint a short-lived token server-side and authenticate with it from a less-trusted client:
119
+
120
+ ```ruby
121
+ server_client = Scanii::Client.new(key: "k", secret: "s")
122
+ token = server_client.create_auth_token(300)
123
+
124
+ token_client = Scanii::Client.new(token: token.id)
125
+ token_client.ping
126
+ ```
127
+
128
+ ## Local testing with scanii-cli
129
+
130
+ The SDK ships integration tests against [scanii-cli](https://github.com/scanii/scanii-cli), a local mock server. No real Scanii credentials are needed.
131
+
132
+ ```bash
133
+ docker run -d --name scanii-cli -p 4000:4000 ghcr.io/scanii/scanii-cli:latest server
134
+
135
+ bundle install
136
+ bundle exec rake test
137
+ ```
138
+
139
+ Test credentials are `key` / `secret`. Override the endpoint by exporting `SCANII_TEST_ENDPOINT=...` before `rake test`. Integration tests self-skip when scanii-cli is not reachable, so `rake test` is safe to run in any environment.
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and PRs welcome at <https://github.com/scanii/scanii-ruby/issues>.
144
+
145
+ ## License
146
+
147
+ [Apache-2.0](LICENSE).
@@ -0,0 +1,40 @@
1
+ require "json"
2
+
3
+ module Scanii
4
+ # Short-lived auth token returned by Client#create_auth_token and
5
+ # Client#retrieve_auth_token.
6
+ #
7
+ # Pass id back as the token: keyword argument when constructing a Client to
8
+ # authenticate using the token instead of API key + secret.
9
+ #
10
+ # @see https://scanii.github.io/openapi/v22/
11
+ class AuthToken
12
+ attr_reader :id, :creation_date, :expiration_date,
13
+ :request_id, :host_id, :resource_location, :raw_response
14
+
15
+ def initialize(id:, creation_date:, expiration_date:,
16
+ request_id:, host_id:, resource_location:, raw_response:)
17
+ @id = id
18
+ @creation_date = creation_date
19
+ @expiration_date = expiration_date
20
+ @request_id = request_id
21
+ @host_id = host_id
22
+ @resource_location = resource_location
23
+ @raw_response = raw_response
24
+ end
25
+
26
+ def self.from_response(body, headers)
27
+ json = body.nil? || body.empty? ? {} : JSON.parse(body)
28
+
29
+ new(
30
+ id: (json["id"] || "").to_s,
31
+ creation_date: json["creation_date"]&.to_s,
32
+ expiration_date: json["expiration_date"]&.to_s,
33
+ request_id: headers["x-scanii-request-id"],
34
+ host_id: headers["x-scanii-host-id"],
35
+ resource_location: headers["location"],
36
+ raw_response: body
37
+ )
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,360 @@
1
+ require "json"
2
+ require "net/http"
3
+ require "uri"
4
+
5
+ module Scanii
6
+ # Synchronous client for the Scanii REST API v2.2.
7
+ #
8
+ # Construct with either +key:+ + +secret:+ (HTTP Basic Auth) or +token:+
9
+ # (auth-token authentication). Mixing the two raises ArgumentError.
10
+ #
11
+ # Per SDK Principle 3 the client is integration-only: it does not retry,
12
+ # batch, or paginate. Each public method maps to exactly one HTTP request.
13
+ #
14
+ # @see https://scanii.github.io/openapi/v22/
15
+ #
16
+ # @example Scan a file from disk
17
+ # client = Scanii::Client.new(key: "your-key", secret: "your-secret")
18
+ # result = client.process_file("./file.pdf")
19
+ # puts result.findings # [] when clean
20
+ #
21
+ # @example Scan content already in memory
22
+ # result = client.process(StringIO.new(bytes), filename: "upload.bin")
23
+ class Client
24
+ DEFAULT_ENDPOINT = "https://api.scanii.com".freeze
25
+ DEFAULT_TIMEOUT = 60
26
+ API_VERSION_PATH = "/v2.2".freeze
27
+ USER_AGENT = "scanii-ruby/#{Scanii::VERSION}".freeze
28
+
29
+ attr_reader :endpoint, :timeout, :user_agent
30
+
31
+ # @param key [String, nil] API key (mutually exclusive with token)
32
+ # @param secret [String, nil] API secret (required when key is set)
33
+ # @param token [String, nil] auth-token id (mutually exclusive with key/secret)
34
+ # @param endpoint [String] base URL; defaults to https://api.scanii.com
35
+ # @param timeout [Integer] open + read timeout in seconds; default 60
36
+ # @param user_agent [String, nil] optional fragment prepended to the SDK's default User-Agent
37
+ def initialize(key: nil, secret: nil, token: nil, endpoint: DEFAULT_ENDPOINT,
38
+ timeout: DEFAULT_TIMEOUT, user_agent: nil)
39
+ @auth_header = build_auth_header(key, secret, token)
40
+ @endpoint = endpoint.to_s.sub(%r{/+\z}, "")
41
+ raise ArgumentError, "endpoint must not be empty" if @endpoint.empty?
42
+
43
+ @base_uri = URI.parse("#{@endpoint}#{API_VERSION_PATH}")
44
+ raise ArgumentError, "endpoint must be http(s)" unless %w[http https].include?(@base_uri.scheme)
45
+
46
+ @timeout = Integer(timeout)
47
+ @user_agent = user_agent && !user_agent.empty? ? "#{user_agent} #{USER_AGENT}" : USER_AGENT
48
+ end
49
+
50
+ # Submit an IO-like object for synchronous scanning.
51
+ #
52
+ # +io+ is duck-typed: anything responding to +read(n)+ returning a String.
53
+ # Both +File+ (opened with +File.open(path, "rb")+) and +StringIO+ work.
54
+ # The body is streamed to the socket; file content is never fully buffered.
55
+ #
56
+ # Passing a String path is deprecated — use {#process_file} instead.
57
+ #
58
+ # @overload process(io, filename:, content_type: nil, metadata: nil, callback: nil)
59
+ # @param io [#read] IO-like object
60
+ # @param filename [String] filename sent in the multipart part
61
+ # @param content_type [String, nil] content-type of the file part; guessed from filename when nil
62
+ # @param metadata [Hash{String=>String}, nil] arbitrary key/value pairs attached to the result
63
+ # @param callback [String, nil] URL to POST the result to on completion
64
+ #
65
+ # @see https://scanii.github.io/openapi/v22/ POST /files
66
+ # @return [Scanii::ProcessingResult]
67
+ def process(first_arg, filename: nil, content_type: nil, metadata: nil, callback: nil)
68
+ if first_arg.is_a?(String)
69
+ # @deprecated Use {#process_file} instead. Will be removed in a future major version.
70
+ warn "[DEPRECATION] `Scanii::Client#process(path)` is deprecated; " \
71
+ "use `process_file(path)` instead. Will be removed in a future major version."
72
+ return process_file(first_arg, metadata: metadata, callback: callback)
73
+ end
74
+
75
+ raise ArgumentError, "io must respond to read" unless first_arg.respond_to?(:read)
76
+ raise ArgumentError, "filename: is required" if filename.nil? || filename.to_s.empty?
77
+
78
+ fields = build_text_fields(metadata, callback)
79
+ stream, ct, length = Multipart.stream_encode(fields, first_arg, filename.to_s, content_type)
80
+ status, resp_body, headers = post("/files", body_stream: stream, content_type: ct,
81
+ content_length: length)
82
+ raise_for_status(status, resp_body, headers) unless status == 201
83
+ ProcessingResult.from_response(resp_body, headers)
84
+ end
85
+
86
+ # Submit a file path for synchronous scanning.
87
+ #
88
+ # Opens the file in binary mode, streams it to Scanii, and closes it.
89
+ # Delegates to {#process} with +filename+ set to the basename.
90
+ #
91
+ # @param file_path [String] path to the file to upload
92
+ # @param metadata [Hash{String=>String}, nil]
93
+ # @param callback [String, nil]
94
+ # @see https://scanii.github.io/openapi/v22/ POST /files
95
+ # @return [Scanii::ProcessingResult]
96
+ def process_file(file_path, metadata: nil, callback: nil)
97
+ assert_readable(file_path)
98
+ File.open(file_path.to_s, "rb") do |f|
99
+ process(f, filename: File.basename(file_path.to_s), metadata: metadata, callback: callback)
100
+ end
101
+ end
102
+
103
+ # Submit an IO-like object for server-side asynchronous scanning.
104
+ #
105
+ # Returns a pending id; the final result is delivered to +callback+ (when
106
+ # supplied) or fetched via {#retrieve}.
107
+ #
108
+ # Passing a String path is deprecated — use {#process_async_file} instead.
109
+ #
110
+ # @overload process_async(io, filename:, content_type: nil, metadata: nil, callback: nil)
111
+ # @param io [#read] IO-like object
112
+ # @param filename [String] filename sent in the multipart part
113
+ # @param content_type [String, nil]
114
+ # @param metadata [Hash{String=>String}, nil]
115
+ # @param callback [String, nil]
116
+ #
117
+ # @see https://scanii.github.io/openapi/v22/ POST /files/async
118
+ # @return [Scanii::PendingResult]
119
+ def process_async(first_arg, filename: nil, content_type: nil, metadata: nil, callback: nil)
120
+ if first_arg.is_a?(String)
121
+ # @deprecated Use {#process_async_file} instead. Will be removed in a future major version.
122
+ warn "[DEPRECATION] `Scanii::Client#process_async(path)` is deprecated; " \
123
+ "use `process_async_file(path)` instead. Will be removed in a future major version."
124
+ return process_async_file(first_arg, metadata: metadata, callback: callback)
125
+ end
126
+
127
+ raise ArgumentError, "io must respond to read" unless first_arg.respond_to?(:read)
128
+ raise ArgumentError, "filename: is required" if filename.nil? || filename.to_s.empty?
129
+
130
+ fields = build_text_fields(metadata, callback)
131
+ stream, ct, length = Multipart.stream_encode(fields, first_arg, filename.to_s, content_type)
132
+ status, resp_body, headers = post("/files/async", body_stream: stream, content_type: ct,
133
+ content_length: length)
134
+ raise_for_status(status, resp_body, headers) unless status == 202
135
+ PendingResult.from_response(resp_body, headers)
136
+ end
137
+
138
+ # Submit a file path for server-side asynchronous scanning.
139
+ #
140
+ # Opens the file in binary mode and delegates to {#process_async}.
141
+ #
142
+ # @param file_path [String] path to the file to upload
143
+ # @param metadata [Hash{String=>String}, nil]
144
+ # @param callback [String, nil]
145
+ # @see https://scanii.github.io/openapi/v22/ POST /files/async
146
+ # @return [Scanii::PendingResult]
147
+ def process_async_file(file_path, metadata: nil, callback: nil)
148
+ assert_readable(file_path)
149
+ File.open(file_path.to_s, "rb") do |f|
150
+ process_async(f, filename: File.basename(file_path.to_s), metadata: metadata,
151
+ callback: callback)
152
+ end
153
+ end
154
+
155
+ # Ask Scanii to download a remote URL and scan it asynchronously.
156
+ #
157
+ # @see https://scanii.github.io/openapi/v22/ POST /files/fetch
158
+ # @return [Scanii::PendingResult]
159
+ def fetch(url, metadata: nil, callback: nil)
160
+ raise ArgumentError, "url must not be empty" if url.nil? || url.empty?
161
+
162
+ form = { "location" => url }
163
+ form["callback"] = callback if callback && !callback.empty?
164
+ (metadata || {}).each { |k, v| form["metadata[#{k}]"] = v.to_s }
165
+
166
+ status, resp_body, headers = post(
167
+ "/files/fetch",
168
+ body: URI.encode_www_form(form),
169
+ content_type: "application/x-www-form-urlencoded"
170
+ )
171
+ raise_for_status(status, resp_body, headers) unless status == 202
172
+ PendingResult.from_response(resp_body, headers)
173
+ end
174
+
175
+ # Retrieve a previously submitted scan result by id.
176
+ #
177
+ # @see https://scanii.github.io/openapi/v22/ GET /files/{id}
178
+ # @return [Scanii::ProcessingResult]
179
+ def retrieve(id)
180
+ raise ArgumentError, "id must not be empty" if id.nil? || id.empty?
181
+
182
+ status, resp_body, headers = request("GET", "/files/#{url_encode(id)}")
183
+ raise_for_status(status, resp_body, headers) unless status == 200
184
+ ProcessingResult.from_response(resp_body, headers)
185
+ end
186
+
187
+ # Verify that the configured credentials reach the API.
188
+ #
189
+ # @see https://scanii.github.io/openapi/v22/ GET /ping
190
+ # @return [Boolean] true when the API responds 200
191
+ def ping
192
+ status, resp_body, headers = request("GET", "/ping")
193
+ return true if status == 200
194
+
195
+ raise_for_status(status, resp_body, headers)
196
+ end
197
+
198
+ # Mint a short-lived auth token. timeout_seconds must be positive.
199
+ #
200
+ # @see https://scanii.github.io/openapi/v22/ POST /auth/tokens
201
+ # @return [Scanii::AuthToken]
202
+ def create_auth_token(timeout_seconds)
203
+ ts = Integer(timeout_seconds)
204
+ raise ArgumentError, "timeout_seconds must be positive" if ts <= 0
205
+
206
+ status, resp_body, headers = post(
207
+ "/auth/tokens",
208
+ body: URI.encode_www_form("timeout" => ts),
209
+ content_type: "application/x-www-form-urlencoded"
210
+ )
211
+ raise_for_status(status, resp_body, headers) unless [200, 201].include?(status)
212
+ AuthToken.from_response(resp_body, headers)
213
+ end
214
+
215
+ # Inspect a previously created auth token.
216
+ #
217
+ # @see https://scanii.github.io/openapi/v22/ GET /auth/tokens/{id}
218
+ # @return [Scanii::AuthToken]
219
+ def retrieve_auth_token(id)
220
+ raise ArgumentError, "id must not be empty" if id.nil? || id.empty?
221
+
222
+ status, resp_body, headers = request("GET", "/auth/tokens/#{url_encode(id)}")
223
+ raise_for_status(status, resp_body, headers) unless status == 200
224
+ AuthToken.from_response(resp_body, headers)
225
+ end
226
+
227
+ # Revoke an auth token.
228
+ #
229
+ # @see https://scanii.github.io/openapi/v22/ DELETE /auth/tokens/{id}
230
+ # @return [Boolean] true on 204
231
+ def delete_auth_token(id)
232
+ raise ArgumentError, "id must not be empty" if id.nil? || id.empty?
233
+
234
+ status, resp_body, headers = request("DELETE", "/auth/tokens/#{url_encode(id)}")
235
+ raise_for_status(status, resp_body, headers) unless status == 204
236
+ true
237
+ end
238
+
239
+ private
240
+
241
+ def build_auth_header(key, secret, token)
242
+ if token && !token.empty?
243
+ raise ArgumentError, "supply either token: or key:+secret:, not both" if key || secret
244
+
245
+ "Basic #{base64_encode("#{token}:")}"
246
+ else
247
+ raise ArgumentError, "key must be set (or use token: for auth-token mode)" if key.nil? || key.empty?
248
+ raise ArgumentError, "key must not contain a colon" if key.include?(":")
249
+ raise ArgumentError, "secret must be set when using key auth" if secret.nil? || secret.empty?
250
+
251
+ "Basic #{base64_encode("#{key}:#{secret}")}"
252
+ end
253
+ end
254
+
255
+ # Stdlib-only base64. Array#pack("m0") emits RFC 4648 base64 with no
256
+ # newlines -- identical output to Base64.strict_encode64 -- without
257
+ # requiring the unbundled `base64` gem on Ruby 3.4+.
258
+ def base64_encode(value)
259
+ [value].pack("m0")
260
+ end
261
+
262
+ def assert_readable(path)
263
+ raise ArgumentError, "file at #{path} is not readable" unless File.file?(path) && File.readable?(path)
264
+ end
265
+
266
+ def build_text_fields(metadata, callback)
267
+ fields = {}
268
+ (metadata || {}).each { |k, v| fields["metadata[#{k}]"] = v.to_s }
269
+ fields["callback"] = callback if callback && !callback.empty?
270
+ fields
271
+ end
272
+
273
+ def post(path, body: nil, content_type: nil, body_stream: nil, content_length: nil)
274
+ request("POST", path, body: body, content_type: content_type,
275
+ body_stream: body_stream, content_length: content_length)
276
+ end
277
+
278
+ def request(method, path, body: nil, content_type: nil, body_stream: nil, content_length: nil)
279
+ uri = URI.parse("#{@base_uri}#{path}")
280
+
281
+ req = build_request(method, uri, body: body, content_type: content_type,
282
+ body_stream: body_stream, content_length: content_length)
283
+
284
+ Net::HTTP.start(uri.hostname, uri.port,
285
+ use_ssl: uri.scheme == "https",
286
+ open_timeout: @timeout,
287
+ read_timeout: @timeout) do |http|
288
+ response = http.request(req)
289
+ headers = capture_headers(response)
290
+ [response.code.to_i, response.body.to_s, headers]
291
+ end
292
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Net::OpenTimeout, Net::ReadTimeout, SocketError => e
293
+ raise Scanii::Error, "transport error: #{e.class}: #{e.message}"
294
+ end
295
+
296
+ def build_request(method, uri, body: nil, content_type: nil, body_stream: nil, content_length: nil)
297
+ klass = case method
298
+ when "GET" then Net::HTTP::Get
299
+ when "POST" then Net::HTTP::Post
300
+ when "DELETE" then Net::HTTP::Delete
301
+ else raise ArgumentError, "unsupported method: #{method}"
302
+ end
303
+
304
+ req = klass.new(uri.request_uri)
305
+ req["Authorization"] = @auth_header
306
+ req["User-Agent"] = @user_agent
307
+ req["Accept"] = "application/json"
308
+ req["Content-Type"] = content_type if content_type
309
+
310
+ if body_stream
311
+ req.body_stream = body_stream
312
+ req["Content-Length"] = content_length.to_s
313
+ elsif body
314
+ req.body = body
315
+ end
316
+
317
+ req
318
+ end
319
+
320
+ def capture_headers(response)
321
+ headers = {}
322
+ response.each_header { |name, value| headers[name.downcase] = value }
323
+ headers
324
+ end
325
+
326
+ def raise_for_status(status, body, headers)
327
+ request_id = headers["x-scanii-request-id"]
328
+ host_id = headers["x-scanii-host-id"]
329
+ message = extract_error_message(body) || "HTTP #{status}"
330
+
331
+ case status
332
+ when 401, 403
333
+ raise Scanii::AuthError.new(message, status_code: status, request_id: request_id,
334
+ host_id: host_id, body: body)
335
+ when 429
336
+ retry_after = headers["retry-after"]&.to_i
337
+ raise Scanii::RateLimitError.new(message, status_code: status, request_id: request_id,
338
+ host_id: host_id, body: body, retry_after: retry_after)
339
+ else
340
+ raise Scanii::Error.new(message, status_code: status, request_id: request_id,
341
+ host_id: host_id, body: body)
342
+ end
343
+ end
344
+
345
+ def extract_error_message(body)
346
+ return nil if body.nil? || body.empty?
347
+
348
+ decoded = JSON.parse(body)
349
+ return decoded["error"].to_s if decoded.is_a?(Hash) && decoded["error"].is_a?(String)
350
+
351
+ body
352
+ rescue JSON::ParserError
353
+ body
354
+ end
355
+
356
+ def url_encode(value)
357
+ URI.encode_www_form_component(value)
358
+ end
359
+ end
360
+ end
@@ -0,0 +1,36 @@
1
+ module Scanii
2
+ # Base class for all errors raised by Scanii::Client. Carries the API-supplied
3
+ # message plus optional X-Scanii-Request-Id and X-Scanii-Host-Id headers for
4
+ # support handoffs.
5
+ #
6
+ # Per SDK Principle 3 (integration-only) the SDK does not retry on the caller's
7
+ # behalf -- backoff is the caller's responsibility.
8
+ #
9
+ # @see https://scanii.github.io/openapi/v22/
10
+ class Error < StandardError
11
+ attr_reader :status_code, :request_id, :host_id, :body
12
+
13
+ def initialize(message, status_code: nil, request_id: nil, host_id: nil, body: nil)
14
+ super(message)
15
+ @status_code = status_code
16
+ @request_id = request_id
17
+ @host_id = host_id
18
+ @body = body
19
+ end
20
+ end
21
+
22
+ # Raised on HTTP 401 / 403 -- the credentials were rejected by the API.
23
+ class AuthError < Error
24
+ end
25
+
26
+ # Raised on HTTP 429. retry_after carries the value of the Retry-After
27
+ # response header in seconds when the server provided one.
28
+ class RateLimitError < Error
29
+ attr_reader :retry_after
30
+
31
+ def initialize(message, retry_after: nil, **)
32
+ super(message, **)
33
+ @retry_after = retry_after
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,155 @@
1
+ require "securerandom"
2
+ require "stringio"
3
+
4
+ module Scanii
5
+ # Hand-rolled multipart/form-data encoder (RFC 7578).
6
+ #
7
+ # Ruby's stdlib Net::HTTP does not bundle a multipart encoder; this is the
8
+ # smallest viable implementation that covers the Scanii POST /files payload.
9
+ module Multipart
10
+ module_function
11
+
12
+ # Generate a unique multipart boundary.
13
+ def make_boundary
14
+ "----scanii-ruby-boundary-#{SecureRandom.hex(16)}"
15
+ end
16
+
17
+ # Build the Content-Type header value for a request using boundary.
18
+ def make_content_type(boundary)
19
+ "multipart/form-data; boundary=#{boundary}"
20
+ end
21
+
22
+ # Encode a multipart body as a streaming ChainedIO.
23
+ #
24
+ # Builds the RFC 7578 prologue and epilogue as binary Strings, chains them
25
+ # around the caller's IO, and returns the triple required for
26
+ # Net::HTTP body_stream= uploads. The caller's IO is never read here --
27
+ # only when Net::HTTP reads from the returned ChainedIO.
28
+ #
29
+ # @param fields [Hash{String=>String}] text form fields (e.g. metadata[k]=v, callback)
30
+ # @param io [#read, #size] IO-like object (anything responding to read(n))
31
+ # @param filename [String] filename for the file part
32
+ # @param content_type [String, nil] content-type of the file part; falls back to extension lookup
33
+ # @param file_field [String] name of the file form field; default "file"
34
+ # @return [Array(ChainedIO, String, Integer)] [body_stream, content_type_header, content_length]
35
+ def stream_encode(fields, io, filename, content_type = nil, file_field: "file")
36
+ boundary = make_boundary
37
+ ct = content_type || guess_content_type(filename)
38
+
39
+ prologue = String.new(encoding: Encoding::BINARY)
40
+ fields.each do |name, value|
41
+ write_text_part(prologue, boundary, name.to_s, value.to_s)
42
+ end
43
+ prologue << "--#{boundary}\r\n".b
44
+ prologue << "Content-Disposition: form-data; name=\"#{file_field}\"; filename=\"#{filename}\"\r\n".b
45
+ prologue << "Content-Type: #{ct}\r\n\r\n".b
46
+
47
+ epilogue = "\r\n--#{boundary}--\r\n".b
48
+
49
+ io_size = io_remaining_bytes(io)
50
+ total_length = prologue.bytesize + io_size + epilogue.bytesize
51
+
52
+ [ChainedIO.new(prologue, io, epilogue), make_content_type(boundary), total_length]
53
+ end
54
+
55
+ # Best-effort content-type lookup by filename extension. Falls back to
56
+ # application/octet-stream. The Scanii API does not require an accurate
57
+ # content-type on the multipart part -- the server inspects the bytes -- so
58
+ # a short table is sufficient.
59
+ def guess_content_type(filename)
60
+ ext = File.extname(filename.to_s).delete_prefix(".").downcase
61
+ MIME_TYPES.fetch(ext, "application/octet-stream")
62
+ end
63
+
64
+ MIME_TYPES = {
65
+ "txt" => "text/plain",
66
+ "html" => "text/html",
67
+ "htm" => "text/html",
68
+ "css" => "text/css",
69
+ "csv" => "text/csv",
70
+ "json" => "application/json",
71
+ "xml" => "application/xml",
72
+ "pdf" => "application/pdf",
73
+ "zip" => "application/zip",
74
+ "gz" => "application/gzip",
75
+ "jpg" => "image/jpeg",
76
+ "jpeg" => "image/jpeg",
77
+ "png" => "image/png",
78
+ "gif" => "image/gif",
79
+ "webp" => "image/webp",
80
+ "svg" => "image/svg+xml",
81
+ "mp3" => "audio/mpeg",
82
+ "mp4" => "video/mp4",
83
+ "mov" => "video/quicktime",
84
+ "doc" => "application/msword",
85
+ "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
86
+ "xls" => "application/vnd.ms-excel",
87
+ "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
88
+ }.freeze
89
+ private_constant :MIME_TYPES
90
+
91
+ def write_text_part(body, boundary, name, value)
92
+ body << "--#{boundary}\r\n".b
93
+ body << "Content-Disposition: form-data; name=\"#{name}\"\r\n".b
94
+ body << "Content-Type: text/plain; charset=UTF-8\r\n\r\n".b
95
+ body << value.b
96
+ body << "\r\n".b
97
+ end
98
+ private_class_method :write_text_part
99
+
100
+ # Return the number of bytes remaining to be read from io.
101
+ # Requires io to respond to size (File and StringIO both do).
102
+ def io_remaining_bytes(io)
103
+ if io.respond_to?(:pos) && io.respond_to?(:size)
104
+ io.size - io.pos
105
+ elsif io.respond_to?(:size)
106
+ io.size
107
+ else
108
+ raise ArgumentError, "io must respond to size (File and StringIO do; got #{io.class})"
109
+ end
110
+ end
111
+ private_class_method :io_remaining_bytes
112
+
113
+ # Reads prologue_str, then io, then epilogue_str in sequence.
114
+ # Used as Net::HTTP body_stream for streaming multipart uploads.
115
+ class ChainedIO
116
+ def initialize(prologue, io, epilogue)
117
+ @parts = [StringIO.new(prologue), io, StringIO.new(epilogue)]
118
+ @idx = 0
119
+ end
120
+
121
+ def read(length = nil, buf = nil)
122
+ result = length.nil? ? read_all : read_n(length)
123
+ return nil if result.nil?
124
+
125
+ buf ? buf.replace(result) : result
126
+ end
127
+
128
+ private
129
+
130
+ def read_all
131
+ result = String.new(encoding: Encoding::BINARY)
132
+ @parts[@idx..].each do |part|
133
+ chunk = part.read
134
+ result << chunk.b if chunk
135
+ end
136
+ @idx = @parts.size
137
+ result
138
+ end
139
+
140
+ def read_n(length)
141
+ result = String.new(encoding: Encoding::BINARY)
142
+ while result.bytesize < length && @idx < @parts.size
143
+ chunk = @parts[@idx].read(length - result.bytesize)
144
+ if chunk.nil? || chunk.empty?
145
+ @idx += 1
146
+ else
147
+ result << chunk.b
148
+ end
149
+ end
150
+ result.empty? ? nil : result
151
+ end
152
+ end
153
+ private_constant :ChainedIO
154
+ end
155
+ end
@@ -0,0 +1,34 @@
1
+ require "json"
2
+
3
+ module Scanii
4
+ # Result of an asynchronous scan submission returned by Client#process_async
5
+ # and Client#fetch.
6
+ #
7
+ # The actual scan result is fetched later via Client#retrieve, or delivered
8
+ # to the supplied callback URL.
9
+ #
10
+ # @see https://scanii.github.io/openapi/v22/
11
+ class PendingResult
12
+ attr_reader :id, :request_id, :host_id, :resource_location, :raw_response
13
+
14
+ def initialize(id:, request_id:, host_id:, resource_location:, raw_response:)
15
+ @id = id
16
+ @request_id = request_id
17
+ @host_id = host_id
18
+ @resource_location = resource_location
19
+ @raw_response = raw_response
20
+ end
21
+
22
+ def self.from_response(body, headers)
23
+ json = body.nil? || body.empty? ? {} : JSON.parse(body)
24
+
25
+ new(
26
+ id: (json["id"] || "").to_s,
27
+ request_id: headers["x-scanii-request-id"],
28
+ host_id: headers["x-scanii-host-id"],
29
+ resource_location: headers["location"],
30
+ raw_response: body
31
+ )
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ require "json"
2
+
3
+ module Scanii
4
+ # Result of a synchronous file scan returned by Client#process and
5
+ # Client#retrieve.
6
+ #
7
+ # findings is always an Array. Empty array means the content is clean.
8
+ #
9
+ # @see https://scanii.github.io/openapi/v22/
10
+ class ProcessingResult
11
+ attr_reader :id, :findings, :checksum, :content_length, :content_type,
12
+ :metadata, :creation_date, :error,
13
+ :request_id, :host_id, :resource_location, :raw_response
14
+
15
+ def initialize(id:, findings:, checksum:, content_length:, content_type:,
16
+ metadata:, creation_date:, error:,
17
+ request_id:, host_id:, resource_location:, raw_response:)
18
+ @id = id
19
+ @findings = findings
20
+ @checksum = checksum
21
+ @content_length = content_length
22
+ @content_type = content_type
23
+ @metadata = metadata
24
+ @creation_date = creation_date
25
+ @error = error
26
+ @request_id = request_id
27
+ @host_id = host_id
28
+ @resource_location = resource_location
29
+ @raw_response = raw_response
30
+ end
31
+
32
+ def self.from_response(body, headers)
33
+ json = body.nil? || body.empty? ? {} : JSON.parse(body)
34
+
35
+ new(
36
+ id: (json["id"] || "").to_s,
37
+ findings: Array(json["findings"]).map(&:to_s),
38
+ checksum: json["checksum"]&.to_s,
39
+ content_length: json["content_length"]&.to_i,
40
+ content_type: json["content_type"]&.to_s,
41
+ metadata: (json["metadata"] || {}).transform_values(&:to_s),
42
+ creation_date: json["creation_date"]&.to_s,
43
+ error: json["error"]&.to_s,
44
+ request_id: headers["x-scanii-request-id"],
45
+ host_id: headers["x-scanii-host-id"],
46
+ resource_location: headers["location"],
47
+ raw_response: body
48
+ )
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Scanii
2
+ VERSION = "1.1.0".freeze
3
+ end
data/lib/scanii.rb ADDED
@@ -0,0 +1,13 @@
1
+ require_relative "scanii/version"
2
+ require_relative "scanii/error"
3
+ require_relative "scanii/processing_result"
4
+ require_relative "scanii/pending_result"
5
+ require_relative "scanii/auth_token"
6
+ require_relative "scanii/multipart"
7
+ require_relative "scanii/client"
8
+
9
+ # Top-level namespace for the Scanii Ruby SDK.
10
+ #
11
+ # See https://scanii.github.io/openapi/v22/ for the full API reference.
12
+ module Scanii
13
+ end
metadata CHANGED
@@ -1,30 +1,95 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scanii-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scanii
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2026-04-28 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Placeholder release. See https://github.com/scanii/scanii-ruby for the
14
- upcoming v1.0.0.
15
- email:
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: minitest
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '5'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '5'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '13'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rubocop
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1'
54
+ - !ruby/object:Gem::Dependency
55
+ name: webmock
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3'
68
+ description: Ruby client for Scanii (scanii.com). Stdlib only -- no runtime dependencies.
16
69
  executables: []
17
70
  extensions: []
18
71
  extra_rdoc_files: []
19
72
  files:
73
+ - CHANGELOG.md
74
+ - LICENSE
20
75
  - README.md
76
+ - lib/scanii.rb
77
+ - lib/scanii/auth_token.rb
78
+ - lib/scanii/client.rb
79
+ - lib/scanii/error.rb
80
+ - lib/scanii/multipart.rb
81
+ - lib/scanii/pending_result.rb
82
+ - lib/scanii/processing_result.rb
83
+ - lib/scanii/version.rb
21
84
  homepage: https://github.com/scanii/scanii-ruby
22
85
  licenses:
23
86
  - Apache-2.0
24
87
  metadata:
25
88
  homepage_uri: https://scanii.com
26
89
  source_code_uri: https://github.com/scanii/scanii-ruby
27
- post_install_message:
90
+ documentation_uri: https://scanii.github.io/openapi/v22/
91
+ changelog_uri: https://github.com/scanii/scanii-ruby/blob/main/CHANGELOG.md
92
+ rubygems_mfa_required: 'true'
28
93
  rdoc_options: []
29
94
  require_paths:
30
95
  - lib
@@ -39,9 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
104
  - !ruby/object:Gem::Version
40
105
  version: '0'
41
106
  requirements: []
42
- rubygems_version: 3.0.3.1
43
- signing_key:
107
+ rubygems_version: 4.0.6
44
108
  specification_version: 4
45
- summary: Placeholder. The official Scanii Ruby SDK is releasing as scanii-ruby v1.0.0
46
- — coming soon.
109
+ summary: Zero-dependency Ruby SDK for the Scanii content security API
47
110
  test_files: []