seerstack 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.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.ignore +2 -0
  3. data/CHANGELOG.md +15 -0
  4. data/README.md +205 -0
  5. data/SECURITY.md +23 -0
  6. data/lib/seerstack/client.rb +79 -0
  7. data/lib/seerstack/errors.rb +228 -0
  8. data/lib/seerstack/file_part.rb +58 -0
  9. data/lib/seerstack/internal/transport/base_client.rb +573 -0
  10. data/lib/seerstack/internal/transport/pooled_net_requester.rb +210 -0
  11. data/lib/seerstack/internal/type/array_of.rb +168 -0
  12. data/lib/seerstack/internal/type/base_model.rb +529 -0
  13. data/lib/seerstack/internal/type/base_page.rb +55 -0
  14. data/lib/seerstack/internal/type/boolean.rb +77 -0
  15. data/lib/seerstack/internal/type/converter.rb +327 -0
  16. data/lib/seerstack/internal/type/enum.rb +131 -0
  17. data/lib/seerstack/internal/type/file_input.rb +111 -0
  18. data/lib/seerstack/internal/type/hash_of.rb +188 -0
  19. data/lib/seerstack/internal/type/request_parameters.rb +42 -0
  20. data/lib/seerstack/internal/type/union.rb +237 -0
  21. data/lib/seerstack/internal/type/unknown.rb +81 -0
  22. data/lib/seerstack/internal/util.rb +920 -0
  23. data/lib/seerstack/internal.rb +20 -0
  24. data/lib/seerstack/models/event_capture_params.rb +38 -0
  25. data/lib/seerstack/models/event_capture_response.rb +16 -0
  26. data/lib/seerstack/models/user_identify_params.rb +38 -0
  27. data/lib/seerstack/models/user_identify_response.rb +16 -0
  28. data/lib/seerstack/models.rb +45 -0
  29. data/lib/seerstack/request_options.rb +77 -0
  30. data/lib/seerstack/resources/events.rb +38 -0
  31. data/lib/seerstack/resources/users.rb +38 -0
  32. data/lib/seerstack/version.rb +5 -0
  33. data/lib/seerstack.rb +61 -0
  34. data/manifest.yaml +17 -0
  35. data/rbi/seerstack/client.rbi +53 -0
  36. data/rbi/seerstack/errors.rbi +205 -0
  37. data/rbi/seerstack/file_part.rbi +37 -0
  38. data/rbi/seerstack/internal/transport/base_client.rbi +298 -0
  39. data/rbi/seerstack/internal/transport/pooled_net_requester.rbi +83 -0
  40. data/rbi/seerstack/internal/type/array_of.rbi +104 -0
  41. data/rbi/seerstack/internal/type/base_model.rbi +308 -0
  42. data/rbi/seerstack/internal/type/base_page.rbi +42 -0
  43. data/rbi/seerstack/internal/type/boolean.rbi +58 -0
  44. data/rbi/seerstack/internal/type/converter.rbi +216 -0
  45. data/rbi/seerstack/internal/type/enum.rbi +82 -0
  46. data/rbi/seerstack/internal/type/file_input.rbi +59 -0
  47. data/rbi/seerstack/internal/type/hash_of.rbi +104 -0
  48. data/rbi/seerstack/internal/type/request_parameters.rbi +29 -0
  49. data/rbi/seerstack/internal/type/union.rbi +128 -0
  50. data/rbi/seerstack/internal/type/unknown.rbi +58 -0
  51. data/rbi/seerstack/internal/util.rbi +487 -0
  52. data/rbi/seerstack/internal.rbi +18 -0
  53. data/rbi/seerstack/models/event_capture_params.rbi +68 -0
  54. data/rbi/seerstack/models/event_capture_response.rbi +26 -0
  55. data/rbi/seerstack/models/user_identify_params.rbi +68 -0
  56. data/rbi/seerstack/models/user_identify_response.rbi +26 -0
  57. data/rbi/seerstack/models.rbi +7 -0
  58. data/rbi/seerstack/request_options.rbi +59 -0
  59. data/rbi/seerstack/resources/events.rbi +31 -0
  60. data/rbi/seerstack/resources/users.rbi +31 -0
  61. data/rbi/seerstack/version.rbi +5 -0
  62. data/sig/seerstack/client.rbs +28 -0
  63. data/sig/seerstack/errors.rbs +117 -0
  64. data/sig/seerstack/file_part.rbs +21 -0
  65. data/sig/seerstack/internal/transport/base_client.rbs +133 -0
  66. data/sig/seerstack/internal/transport/pooled_net_requester.rbs +48 -0
  67. data/sig/seerstack/internal/type/array_of.rbs +48 -0
  68. data/sig/seerstack/internal/type/base_model.rbs +102 -0
  69. data/sig/seerstack/internal/type/base_page.rbs +24 -0
  70. data/sig/seerstack/internal/type/boolean.rbs +26 -0
  71. data/sig/seerstack/internal/type/converter.rbs +79 -0
  72. data/sig/seerstack/internal/type/enum.rbs +32 -0
  73. data/sig/seerstack/internal/type/file_input.rbs +25 -0
  74. data/sig/seerstack/internal/type/hash_of.rbs +48 -0
  75. data/sig/seerstack/internal/type/request_parameters.rbs +19 -0
  76. data/sig/seerstack/internal/type/union.rbs +52 -0
  77. data/sig/seerstack/internal/type/unknown.rbs +26 -0
  78. data/sig/seerstack/internal/util.rbs +185 -0
  79. data/sig/seerstack/internal.rbs +9 -0
  80. data/sig/seerstack/models/event_capture_params.rbs +47 -0
  81. data/sig/seerstack/models/event_capture_response.rbs +13 -0
  82. data/sig/seerstack/models/user_identify_params.rbs +47 -0
  83. data/sig/seerstack/models/user_identify_response.rbs +13 -0
  84. data/sig/seerstack/models.rbs +5 -0
  85. data/sig/seerstack/request_options.rbs +34 -0
  86. data/sig/seerstack/resources/events.rbs +15 -0
  87. data/sig/seerstack/resources/users.rbs +15 -0
  88. data/sig/seerstack/version.rbs +3 -0
  89. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 11da1d1e35334518033eafa8f889784b7e909dd773d643a003df58ca4ba88a33
4
+ data.tar.gz: 80b24e85c272a0872185da7d83e9910f30c7395269794cf043ae709854930813
5
+ SHA512:
6
+ metadata.gz: 65625be19920286b02197f1f980c85ab09a235cae7fbbf8d88074f5dcc89034a3ea9540b965b01fbce4a1eac2a37cc14d7c6981d0a0832ebfe3a423f69664690
7
+ data.tar.gz: 14f1a0ee7161a73093971af81d141af841c896ec084b60a20e4ea5376427969d47f6167bd4d8b45fc5a11cf40753df752f3cfa38bc7f13345e4803ca22a2c92e
data/.ignore ADDED
@@ -0,0 +1,2 @@
1
+ rbi/*
2
+ sig/*
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2026-01-06)
4
+
5
+ Full Changelog: [v0.0.1...v0.1.0](https://github.com/dqnamo/seerstack-ruby/compare/v0.0.1...v0.1.0)
6
+
7
+ ### Features
8
+
9
+ * **api:** manual updates ([102924d](https://github.com/dqnamo/seerstack-ruby/commit/102924d5dfc572ab2e324949171eda4786ddcc1a))
10
+
11
+
12
+ ### Chores
13
+
14
+ * configure new SDK language ([b6b98dd](https://github.com/dqnamo/seerstack-ruby/commit/b6b98dd5697ad0c53e76a61235834f4d19854918))
15
+ * update SDK settings ([13bfd14](https://github.com/dqnamo/seerstack-ruby/commit/13bfd14556708187bbb346bd23cb5015900ecac6))
data/README.md ADDED
@@ -0,0 +1,205 @@
1
+ # Seerstack Ruby API library
2
+
3
+ The Seerstack Ruby library provides convenient access to the Seerstack REST API from any Ruby 3.2.0+ application. It ships with comprehensive types & docstrings in Yard, RBS, and RBI – [see below](https://github.com/dqnamo/seerstack-ruby#Sorbet) for usage with Sorbet. The standard library's `net/http` is used as the HTTP transport, with connection pooling via the `connection_pool` gem.
4
+
5
+ It is generated with [Stainless](https://www.stainless.com/).
6
+
7
+ ## Documentation
8
+
9
+ Documentation for releases of this gem can be found [on RubyDoc](https://gemdocs.org/gems/seerstack).
10
+
11
+ ## Installation
12
+
13
+ To use this gem, install via Bundler by adding the following to your application's `Gemfile`:
14
+
15
+ <!-- x-release-please-start-version -->
16
+
17
+ ```ruby
18
+ gem "seerstack", "~> 0.1.0"
19
+ ```
20
+
21
+ <!-- x-release-please-end -->
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require "bundler/setup"
27
+ require "seerstack"
28
+
29
+ seerstack = Seerstack::Client.new(
30
+ api_key: ENV["SEERSTACK_API_KEY"] # This is the default and can be omitted
31
+ )
32
+
33
+ response = seerstack.events.capture(name: "REPLACE_ME")
34
+
35
+ puts(response.success)
36
+ ```
37
+
38
+ ### Handling errors
39
+
40
+ When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of `Seerstack::Errors::APIError` will be thrown:
41
+
42
+ ```ruby
43
+ begin
44
+ event = seerstack.events.capture(name: "REPLACE_ME")
45
+ rescue Seerstack::Errors::APIConnectionError => e
46
+ puts("The server could not be reached")
47
+ puts(e.cause) # an underlying Exception, likely raised within `net/http`
48
+ rescue Seerstack::Errors::RateLimitError => e
49
+ puts("A 429 status code was received; we should back off a bit.")
50
+ rescue Seerstack::Errors::APIStatusError => e
51
+ puts("Another non-200-range status code was received")
52
+ puts(e.status)
53
+ end
54
+ ```
55
+
56
+ Error codes are as follows:
57
+
58
+ | Cause | Error Type |
59
+ | ---------------- | -------------------------- |
60
+ | HTTP 400 | `BadRequestError` |
61
+ | HTTP 401 | `AuthenticationError` |
62
+ | HTTP 403 | `PermissionDeniedError` |
63
+ | HTTP 404 | `NotFoundError` |
64
+ | HTTP 409 | `ConflictError` |
65
+ | HTTP 422 | `UnprocessableEntityError` |
66
+ | HTTP 429 | `RateLimitError` |
67
+ | HTTP >= 500 | `InternalServerError` |
68
+ | Other HTTP error | `APIStatusError` |
69
+ | Timeout | `APITimeoutError` |
70
+ | Network error | `APIConnectionError` |
71
+
72
+ ### Retries
73
+
74
+ Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
75
+
76
+ Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default.
77
+
78
+ You can use the `max_retries` option to configure or disable this:
79
+
80
+ ```ruby
81
+ # Configure the default for all requests:
82
+ seerstack = Seerstack::Client.new(
83
+ max_retries: 0 # default is 2
84
+ )
85
+
86
+ # Or, configure per-request:
87
+ seerstack.events.capture(name: "REPLACE_ME", request_options: {max_retries: 5})
88
+ ```
89
+
90
+ ### Timeouts
91
+
92
+ By default, requests will time out after 60 seconds. You can use the timeout option to configure or disable this:
93
+
94
+ ```ruby
95
+ # Configure the default for all requests:
96
+ seerstack = Seerstack::Client.new(
97
+ timeout: nil # default is 60
98
+ )
99
+
100
+ # Or, configure per-request:
101
+ seerstack.events.capture(name: "REPLACE_ME", request_options: {timeout: 5})
102
+ ```
103
+
104
+ On timeout, `Seerstack::Errors::APITimeoutError` is raised.
105
+
106
+ Note that requests that time out are retried by default.
107
+
108
+ ## Advanced concepts
109
+
110
+ ### BaseModel
111
+
112
+ All parameter and response objects inherit from `Seerstack::Internal::Type::BaseModel`, which provides several conveniences, including:
113
+
114
+ 1. All fields, including unknown ones, are accessible with `obj[:prop]` syntax, and can be destructured with `obj => {prop: prop}` or pattern-matching syntax.
115
+
116
+ 2. Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
117
+
118
+ 3. Both instances and the classes themselves can be pretty-printed.
119
+
120
+ 4. Helpers such as `#to_h`, `#deep_to_h`, `#to_json`, and `#to_yaml`.
121
+
122
+ ### Making custom or undocumented requests
123
+
124
+ #### Undocumented properties
125
+
126
+ You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
127
+
128
+ Note: the `extra_` parameters of the same name overrides the documented parameters.
129
+
130
+ ```ruby
131
+ response =
132
+ seerstack.events.capture(
133
+ name: "REPLACE_ME",
134
+ request_options: {
135
+ extra_query: {my_query_parameter: value},
136
+ extra_body: {my_body_parameter: value},
137
+ extra_headers: {"my-header": value}
138
+ }
139
+ )
140
+
141
+ puts(response[:my_undocumented_property])
142
+ ```
143
+
144
+ #### Undocumented request params
145
+
146
+ If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` under the `request_options:` parameter when making a request, as seen in the examples above.
147
+
148
+ #### Undocumented endpoints
149
+
150
+ To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using `client.request`, like so:
151
+
152
+ ```ruby
153
+ response = client.request(
154
+ method: :post,
155
+ path: '/undocumented/endpoint',
156
+ query: {"dog": "woof"},
157
+ headers: {"useful-header": "interesting-value"},
158
+ body: {"hello": "world"}
159
+ )
160
+ ```
161
+
162
+ ### Concurrency & connection pooling
163
+
164
+ The `Seerstack::Client` instances are threadsafe, but are only are fork-safe when there are no in-flight HTTP requests.
165
+
166
+ Each instance of `Seerstack::Client` has its own HTTP connection pool with a default size of 99. As such, we recommend instantiating the client once per application in most settings.
167
+
168
+ When all available connections from the pool are checked out, requests wait for a new connection to become available, with queue time counting towards the request timeout.
169
+
170
+ Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
171
+
172
+ ## Sorbet
173
+
174
+ This library provides comprehensive [RBI](https://sorbet.org/docs/rbi) definitions, and has no dependency on sorbet-runtime.
175
+
176
+ You can provide typesafe request parameters like so:
177
+
178
+ ```ruby
179
+ seerstack.events.capture(name: "REPLACE_ME")
180
+ ```
181
+
182
+ Or, equivalently:
183
+
184
+ ```ruby
185
+ # Hashes work, but are not typesafe:
186
+ seerstack.events.capture(name: "REPLACE_ME")
187
+
188
+ # You can also splat a full Params class:
189
+ params = Seerstack::EventCaptureParams.new(name: "REPLACE_ME")
190
+ seerstack.events.capture(**params)
191
+ ```
192
+
193
+ ## Versioning
194
+
195
+ This package follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions. As the library is in initial development and has a major version of `0`, APIs may change at any time.
196
+
197
+ This package considers improvements to the (non-runtime) `*.rbi` and `*.rbs` type definitions to be non-breaking changes.
198
+
199
+ ## Requirements
200
+
201
+ Ruby 3.2.0 or higher.
202
+
203
+ ## Contributing
204
+
205
+ See [the contributing documentation](https://github.com/dqnamo/seerstack-ruby/tree/main/CONTRIBUTING.md).
data/SECURITY.md ADDED
@@ -0,0 +1,23 @@
1
+ # Security Policy
2
+
3
+ ## Reporting Security Issues
4
+
5
+ This SDK is generated by [Stainless Software Inc](http://stainless.com). Stainless takes security seriously, and encourages you to report any security vulnerability promptly so that appropriate action can be taken.
6
+
7
+ To report a security issue, please contact the Stainless team at security@stainless.com.
8
+
9
+ ## Responsible Disclosure
10
+
11
+ We appreciate the efforts of security researchers and individuals who help us maintain the security of
12
+ SDKs we generate. If you believe you have found a security vulnerability, please adhere to responsible
13
+ disclosure practices by allowing us a reasonable amount of time to investigate and address the issue
14
+ before making any information public.
15
+
16
+ ## Reporting Non-SDK Related Security Issues
17
+
18
+ If you encounter security issues that are not directly related to SDKs but pertain to the services
19
+ or products provided by Seerstack, please follow the respective company's security reporting guidelines.
20
+
21
+ ---
22
+
23
+ Thank you for helping us keep the SDKs and systems they interact with secure.
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Seerstack
4
+ class Client < Seerstack::Internal::Transport::BaseClient
5
+ # Default max number of retries to attempt after a failed retryable request.
6
+ DEFAULT_MAX_RETRIES = 2
7
+
8
+ # Default per-request timeout.
9
+ DEFAULT_TIMEOUT_IN_SECONDS = 60.0
10
+
11
+ # Default initial retry delay in seconds.
12
+ # Overall delay is calculated using exponential backoff + jitter.
13
+ DEFAULT_INITIAL_RETRY_DELAY = 0.5
14
+
15
+ # Default max retry delay in seconds.
16
+ DEFAULT_MAX_RETRY_DELAY = 8.0
17
+
18
+ # API key authentication via Bearer token
19
+ # @return [String]
20
+ attr_reader :api_key
21
+
22
+ # @return [Seerstack::Resources::Events]
23
+ attr_reader :events
24
+
25
+ # @return [Seerstack::Resources::Users]
26
+ attr_reader :users
27
+
28
+ # @api private
29
+ #
30
+ # @return [Hash{String=>String}]
31
+ private def auth_headers
32
+ return {} if @api_key.nil?
33
+
34
+ {"authorization" => "Bearer #{@api_key}"}
35
+ end
36
+
37
+ # Creates and returns a new client for interacting with the API.
38
+ #
39
+ # @param api_key [String, nil] API key authentication via Bearer token Defaults to `ENV["SEERSTACK_API_KEY"]`
40
+ #
41
+ # @param base_url [String, nil] Override the default base URL for the API, e.g.,
42
+ # `"https://api.example.com/v2/"`. Defaults to `ENV["SEERSTACK_BASE_URL"]`
43
+ #
44
+ # @param max_retries [Integer] Max number of retries to attempt after a failed retryable request.
45
+ #
46
+ # @param timeout [Float]
47
+ #
48
+ # @param initial_retry_delay [Float]
49
+ #
50
+ # @param max_retry_delay [Float]
51
+ def initialize(
52
+ api_key: ENV["SEERSTACK_API_KEY"],
53
+ base_url: ENV["SEERSTACK_BASE_URL"],
54
+ max_retries: self.class::DEFAULT_MAX_RETRIES,
55
+ timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
56
+ initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
57
+ max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
58
+ )
59
+ base_url ||= "https://api.userplex.app"
60
+
61
+ if api_key.nil?
62
+ raise ArgumentError.new("api_key is required, and can be set via environ: \"SEERSTACK_API_KEY\"")
63
+ end
64
+
65
+ @api_key = api_key.to_s
66
+
67
+ super(
68
+ base_url: base_url,
69
+ timeout: timeout,
70
+ max_retries: max_retries,
71
+ initial_retry_delay: initial_retry_delay,
72
+ max_retry_delay: max_retry_delay
73
+ )
74
+
75
+ @events = Seerstack::Resources::Events.new(client: self)
76
+ @users = Seerstack::Resources::Users.new(client: self)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Seerstack
4
+ module Errors
5
+ class Error < StandardError
6
+ # @!attribute cause
7
+ #
8
+ # @return [StandardError, nil]
9
+ end
10
+
11
+ class ConversionError < Seerstack::Errors::Error
12
+ # @return [StandardError, nil]
13
+ def cause = @cause.nil? ? super : @cause
14
+
15
+ # @api private
16
+ #
17
+ # @param on [Class<StandardError>]
18
+ # @param method [Symbol]
19
+ # @param target [Object]
20
+ # @param value [Object]
21
+ # @param cause [StandardError, nil]
22
+ def initialize(on:, method:, target:, value:, cause: nil)
23
+ cls = on.name.split("::").last
24
+
25
+ message = [
26
+ "Failed to parse #{cls}.#{method} from #{value.class} to #{target.inspect}.",
27
+ "To get the unparsed API response, use #{cls}[#{method.inspect}].",
28
+ cause && "Cause: #{cause.message}"
29
+ ].filter(&:itself).join(" ")
30
+
31
+ @cause = cause
32
+ super(message)
33
+ end
34
+ end
35
+
36
+ class APIError < Seerstack::Errors::Error
37
+ # @return [URI::Generic]
38
+ attr_accessor :url
39
+
40
+ # @return [Integer, nil]
41
+ attr_accessor :status
42
+
43
+ # @return [Hash{String=>String}, nil]
44
+ attr_accessor :headers
45
+
46
+ # @return [Object, nil]
47
+ attr_accessor :body
48
+
49
+ # @api private
50
+ #
51
+ # @param url [URI::Generic]
52
+ # @param status [Integer, nil]
53
+ # @param headers [Hash{String=>String}, nil]
54
+ # @param body [Object, nil]
55
+ # @param request [nil]
56
+ # @param response [nil]
57
+ # @param message [String, nil]
58
+ def initialize(url:, status: nil, headers: nil, body: nil, request: nil, response: nil, message: nil)
59
+ @url = url
60
+ @status = status
61
+ @headers = headers
62
+ @body = body
63
+ @request = request
64
+ @response = response
65
+ super(message)
66
+ end
67
+ end
68
+
69
+ class APIConnectionError < Seerstack::Errors::APIError
70
+ # @!attribute status
71
+ #
72
+ # @return [nil]
73
+
74
+ # @!attribute body
75
+ #
76
+ # @return [nil]
77
+
78
+ # @api private
79
+ #
80
+ # @param url [URI::Generic]
81
+ # @param status [nil]
82
+ # @param headers [Hash{String=>String}, nil]
83
+ # @param body [nil]
84
+ # @param request [nil]
85
+ # @param response [nil]
86
+ # @param message [String, nil]
87
+ def initialize(
88
+ url:,
89
+ status: nil,
90
+ headers: nil,
91
+ body: nil,
92
+ request: nil,
93
+ response: nil,
94
+ message: "Connection error."
95
+ )
96
+ super
97
+ end
98
+ end
99
+
100
+ class APITimeoutError < Seerstack::Errors::APIConnectionError
101
+ # @api private
102
+ #
103
+ # @param url [URI::Generic]
104
+ # @param status [nil]
105
+ # @param headers [Hash{String=>String}, nil]
106
+ # @param body [nil]
107
+ # @param request [nil]
108
+ # @param response [nil]
109
+ # @param message [String, nil]
110
+ def initialize(
111
+ url:,
112
+ status: nil,
113
+ headers: nil,
114
+ body: nil,
115
+ request: nil,
116
+ response: nil,
117
+ message: "Request timed out."
118
+ )
119
+ super
120
+ end
121
+ end
122
+
123
+ class APIStatusError < Seerstack::Errors::APIError
124
+ # @api private
125
+ #
126
+ # @param url [URI::Generic]
127
+ # @param status [Integer]
128
+ # @param headers [Hash{String=>String}, nil]
129
+ # @param body [Object, nil]
130
+ # @param request [nil]
131
+ # @param response [nil]
132
+ # @param message [String, nil]
133
+ #
134
+ # @return [self]
135
+ def self.for(url:, status:, headers:, body:, request:, response:, message: nil)
136
+ kwargs =
137
+ {
138
+ url: url,
139
+ status: status,
140
+ headers: headers,
141
+ body: body,
142
+ request: request,
143
+ response: response,
144
+ message: message
145
+ }
146
+
147
+ case status
148
+ in 400
149
+ Seerstack::Errors::BadRequestError.new(**kwargs)
150
+ in 401
151
+ Seerstack::Errors::AuthenticationError.new(**kwargs)
152
+ in 403
153
+ Seerstack::Errors::PermissionDeniedError.new(**kwargs)
154
+ in 404
155
+ Seerstack::Errors::NotFoundError.new(**kwargs)
156
+ in 409
157
+ Seerstack::Errors::ConflictError.new(**kwargs)
158
+ in 422
159
+ Seerstack::Errors::UnprocessableEntityError.new(**kwargs)
160
+ in 429
161
+ Seerstack::Errors::RateLimitError.new(**kwargs)
162
+ in (500..)
163
+ Seerstack::Errors::InternalServerError.new(**kwargs)
164
+ else
165
+ Seerstack::Errors::APIStatusError.new(**kwargs)
166
+ end
167
+ end
168
+
169
+ # @!parse
170
+ # # @return [Integer]
171
+ # attr_accessor :status
172
+
173
+ # @api private
174
+ #
175
+ # @param url [URI::Generic]
176
+ # @param status [Integer]
177
+ # @param headers [Hash{String=>String}, nil]
178
+ # @param body [Object, nil]
179
+ # @param request [nil]
180
+ # @param response [nil]
181
+ # @param message [String, nil]
182
+ def initialize(url:, status:, headers:, body:, request:, response:, message: nil)
183
+ message ||= {url: url.to_s, status: status, body: body}
184
+ super(
185
+ url: url,
186
+ status: status,
187
+ headers: headers,
188
+ body: body,
189
+ request: request,
190
+ response: response,
191
+ message: message&.to_s
192
+ )
193
+ end
194
+ end
195
+
196
+ class BadRequestError < Seerstack::Errors::APIStatusError
197
+ HTTP_STATUS = 400
198
+ end
199
+
200
+ class AuthenticationError < Seerstack::Errors::APIStatusError
201
+ HTTP_STATUS = 401
202
+ end
203
+
204
+ class PermissionDeniedError < Seerstack::Errors::APIStatusError
205
+ HTTP_STATUS = 403
206
+ end
207
+
208
+ class NotFoundError < Seerstack::Errors::APIStatusError
209
+ HTTP_STATUS = 404
210
+ end
211
+
212
+ class ConflictError < Seerstack::Errors::APIStatusError
213
+ HTTP_STATUS = 409
214
+ end
215
+
216
+ class UnprocessableEntityError < Seerstack::Errors::APIStatusError
217
+ HTTP_STATUS = 422
218
+ end
219
+
220
+ class RateLimitError < Seerstack::Errors::APIStatusError
221
+ HTTP_STATUS = 429
222
+ end
223
+
224
+ class InternalServerError < Seerstack::Errors::APIStatusError
225
+ HTTP_STATUS = (500..)
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Seerstack
4
+ class FilePart
5
+ # @return [Pathname, StringIO, IO, String]
6
+ attr_reader :content
7
+
8
+ # @return [String, nil]
9
+ attr_reader :content_type
10
+
11
+ # @return [String, nil]
12
+ attr_reader :filename
13
+
14
+ # @api private
15
+ #
16
+ # @return [String]
17
+ private def read
18
+ case content
19
+ in Pathname
20
+ content.read(binmode: true)
21
+ in StringIO
22
+ content.string
23
+ in IO
24
+ content.read
25
+ in String
26
+ content
27
+ end
28
+ end
29
+
30
+ # @param a [Object]
31
+ #
32
+ # @return [String]
33
+ def to_json(*a) = read.to_json(*a)
34
+
35
+ # @param a [Object]
36
+ #
37
+ # @return [String]
38
+ def to_yaml(*a) = read.to_yaml(*a)
39
+
40
+ # @param content [Pathname, StringIO, IO, String]
41
+ # @param filename [Pathname, String, nil]
42
+ # @param content_type [String, nil]
43
+ def initialize(content, filename: nil, content_type: nil)
44
+ @content_type = content_type
45
+ @filename =
46
+ case [filename, (@content = content)]
47
+ in [String | Pathname, _]
48
+ ::File.basename(filename)
49
+ in [nil, Pathname]
50
+ content.basename.to_path
51
+ in [nil, IO]
52
+ content.to_path
53
+ else
54
+ filename
55
+ end
56
+ end
57
+ end
58
+ end