userplex 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 +14 -0
  4. data/README.md +217 -0
  5. data/SECURITY.md +23 -0
  6. data/lib/userplex/client.rb +80 -0
  7. data/lib/userplex/errors.rb +228 -0
  8. data/lib/userplex/file_part.rb +58 -0
  9. data/lib/userplex/internal/transport/base_client.rb +567 -0
  10. data/lib/userplex/internal/transport/pooled_net_requester.rb +198 -0
  11. data/lib/userplex/internal/type/array_of.rb +168 -0
  12. data/lib/userplex/internal/type/base_model.rb +530 -0
  13. data/lib/userplex/internal/type/base_page.rb +55 -0
  14. data/lib/userplex/internal/type/boolean.rb +77 -0
  15. data/lib/userplex/internal/type/converter.rb +327 -0
  16. data/lib/userplex/internal/type/enum.rb +131 -0
  17. data/lib/userplex/internal/type/file_input.rb +111 -0
  18. data/lib/userplex/internal/type/hash_of.rb +188 -0
  19. data/lib/userplex/internal/type/request_parameters.rb +42 -0
  20. data/lib/userplex/internal/type/union.rb +237 -0
  21. data/lib/userplex/internal/type/unknown.rb +81 -0
  22. data/lib/userplex/internal/util.rb +915 -0
  23. data/lib/userplex/internal.rb +20 -0
  24. data/lib/userplex/models/event_track_params.rb +46 -0
  25. data/lib/userplex/models/event_track_response.rb +33 -0
  26. data/lib/userplex/models/user_identify_params.rb +46 -0
  27. data/lib/userplex/models/user_identify_response.rb +25 -0
  28. data/lib/userplex/models.rb +45 -0
  29. data/lib/userplex/request_options.rb +77 -0
  30. data/lib/userplex/resources/events.rb +43 -0
  31. data/lib/userplex/resources/users.rb +43 -0
  32. data/lib/userplex/version.rb +5 -0
  33. data/lib/userplex.rb +59 -0
  34. data/manifest.yaml +15 -0
  35. data/rbi/userplex/client.rbi +54 -0
  36. data/rbi/userplex/errors.rbi +205 -0
  37. data/rbi/userplex/file_part.rbi +37 -0
  38. data/rbi/userplex/internal/transport/base_client.rbi +292 -0
  39. data/rbi/userplex/internal/transport/pooled_net_requester.rbi +79 -0
  40. data/rbi/userplex/internal/type/array_of.rbi +104 -0
  41. data/rbi/userplex/internal/type/base_model.rbi +302 -0
  42. data/rbi/userplex/internal/type/base_page.rbi +42 -0
  43. data/rbi/userplex/internal/type/boolean.rbi +58 -0
  44. data/rbi/userplex/internal/type/converter.rbi +216 -0
  45. data/rbi/userplex/internal/type/enum.rbi +82 -0
  46. data/rbi/userplex/internal/type/file_input.rbi +59 -0
  47. data/rbi/userplex/internal/type/hash_of.rbi +104 -0
  48. data/rbi/userplex/internal/type/request_parameters.rbi +29 -0
  49. data/rbi/userplex/internal/type/union.rbi +128 -0
  50. data/rbi/userplex/internal/type/unknown.rbi +58 -0
  51. data/rbi/userplex/internal/util.rbi +487 -0
  52. data/rbi/userplex/internal.rbi +16 -0
  53. data/rbi/userplex/models/event_track_params.rbi +73 -0
  54. data/rbi/userplex/models/event_track_response.rbi +52 -0
  55. data/rbi/userplex/models/user_identify_params.rbi +70 -0
  56. data/rbi/userplex/models/user_identify_response.rbi +38 -0
  57. data/rbi/userplex/models.rbi +7 -0
  58. data/rbi/userplex/request_options.rbi +59 -0
  59. data/rbi/userplex/resources/events.rbi +36 -0
  60. data/rbi/userplex/resources/users.rbi +36 -0
  61. data/rbi/userplex/version.rbi +5 -0
  62. data/sig/userplex/client.rbs +28 -0
  63. data/sig/userplex/errors.rbs +117 -0
  64. data/sig/userplex/file_part.rbs +21 -0
  65. data/sig/userplex/internal/transport/base_client.rbs +131 -0
  66. data/sig/userplex/internal/transport/pooled_net_requester.rbs +45 -0
  67. data/sig/userplex/internal/type/array_of.rbs +48 -0
  68. data/sig/userplex/internal/type/base_model.rbs +102 -0
  69. data/sig/userplex/internal/type/base_page.rbs +24 -0
  70. data/sig/userplex/internal/type/boolean.rbs +26 -0
  71. data/sig/userplex/internal/type/converter.rbs +79 -0
  72. data/sig/userplex/internal/type/enum.rbs +32 -0
  73. data/sig/userplex/internal/type/file_input.rbs +25 -0
  74. data/sig/userplex/internal/type/hash_of.rbs +48 -0
  75. data/sig/userplex/internal/type/request_parameters.rbs +17 -0
  76. data/sig/userplex/internal/type/union.rbs +52 -0
  77. data/sig/userplex/internal/type/unknown.rbs +26 -0
  78. data/sig/userplex/internal/util.rbs +185 -0
  79. data/sig/userplex/internal.rbs +9 -0
  80. data/sig/userplex/models/event_track_params.rbs +45 -0
  81. data/sig/userplex/models/event_track_response.rbs +22 -0
  82. data/sig/userplex/models/user_identify_params.rbs +43 -0
  83. data/sig/userplex/models/user_identify_response.rbs +15 -0
  84. data/sig/userplex/models.rbs +5 -0
  85. data/sig/userplex/request_options.rbs +34 -0
  86. data/sig/userplex/resources/events.rbs +15 -0
  87. data/sig/userplex/resources/users.rbs +15 -0
  88. data/sig/userplex/version.rbs +3 -0
  89. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1d3a6006aa1726e9643a159149a388583ecf3156b91fda10064397685af1d2af
4
+ data.tar.gz: 6437cd93059ca4464b9a92f1e7462e200aa01ca868b6a7c513e54cbc2ad27a1c
5
+ SHA512:
6
+ metadata.gz: b103a06762433ab9a0e2a0aa77e320a5802cda16d577e9bfc6c6aa12f006aeb3b1f8e6aa9e5fa75f4f6d4c0f26fe2c18282efaef70d433aeacb18e99d4351559
7
+ data.tar.gz: 1e160957f5c7562471bd43b08c5bed17126665083cfcdbf979da0a4787856477ee2a37dddf846e524539b922f15f6071f4a9cf8add46eeb7a4585db7ffaf8563
data/.ignore ADDED
@@ -0,0 +1,2 @@
1
+ rbi/*
2
+ sig/*
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2025-10-02)
4
+
5
+ Full Changelog: [v0.0.1...v0.1.0](https://github.com/dqnamo/userplex-ruby/compare/v0.0.1...v0.1.0)
6
+
7
+ ### Features
8
+
9
+ * **api:** manual updates ([945732d](https://github.com/dqnamo/userplex-ruby/commit/945732dc8407655bcc36a9088b56c3c093dce9e8))
10
+
11
+
12
+ ### Chores
13
+
14
+ * update SDK settings ([6bd2e40](https://github.com/dqnamo/userplex-ruby/commit/6bd2e40b8dd26435ae696f0abd3f2e1eb823ca42))
data/README.md ADDED
@@ -0,0 +1,217 @@
1
+ # Userplex Ruby API library
2
+
3
+ The Userplex Ruby library provides convenient access to the Userplex 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/userplex-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/userplex).
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 "userplex", "~> 0.1.0"
19
+ ```
20
+
21
+ <!-- x-release-please-end -->
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require "bundler/setup"
27
+ require "userplex"
28
+
29
+ userplex = Userplex::Client.new(
30
+ api_key: ENV["USERPLEX_API_KEY"] # This is the default and can be omitted
31
+ )
32
+
33
+ response = userplex.users.identify(email: "REPLACE_ME", name: "REPLACE_ME", user_id: "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 `Userplex::Errors::APIError` will be thrown:
41
+
42
+ ```ruby
43
+ begin
44
+ user = userplex.users.identify(email: "REPLACE_ME", name: "REPLACE_ME", user_id: "REPLACE_ME")
45
+ rescue Userplex::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 Userplex::Errors::RateLimitError => e
49
+ puts("A 429 status code was received; we should back off a bit.")
50
+ rescue Userplex::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
+ userplex = Userplex::Client.new(
83
+ max_retries: 0 # default is 2
84
+ )
85
+
86
+ # Or, configure per-request:
87
+ userplex.users.identify(
88
+ email: "REPLACE_ME",
89
+ name: "REPLACE_ME",
90
+ user_id: "REPLACE_ME",
91
+ request_options: {max_retries: 5}
92
+ )
93
+ ```
94
+
95
+ ### Timeouts
96
+
97
+ By default, requests will time out after 60 seconds. You can use the timeout option to configure or disable this:
98
+
99
+ ```ruby
100
+ # Configure the default for all requests:
101
+ userplex = Userplex::Client.new(
102
+ timeout: nil # default is 60
103
+ )
104
+
105
+ # Or, configure per-request:
106
+ userplex.users.identify(
107
+ email: "REPLACE_ME",
108
+ name: "REPLACE_ME",
109
+ user_id: "REPLACE_ME",
110
+ request_options: {timeout: 5}
111
+ )
112
+ ```
113
+
114
+ On timeout, `Userplex::Errors::APITimeoutError` is raised.
115
+
116
+ Note that requests that time out are retried by default.
117
+
118
+ ## Advanced concepts
119
+
120
+ ### BaseModel
121
+
122
+ All parameter and response objects inherit from `Userplex::Internal::Type::BaseModel`, which provides several conveniences, including:
123
+
124
+ 1. All fields, including unknown ones, are accessible with `obj[:prop]` syntax, and can be destructured with `obj => {prop: prop}` or pattern-matching syntax.
125
+
126
+ 2. Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
127
+
128
+ 3. Both instances and the classes themselves can be pretty-printed.
129
+
130
+ 4. Helpers such as `#to_h`, `#deep_to_h`, `#to_json`, and `#to_yaml`.
131
+
132
+ ### Making custom or undocumented requests
133
+
134
+ #### Undocumented properties
135
+
136
+ You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
137
+
138
+ Note: the `extra_` parameters of the same name overrides the documented parameters.
139
+
140
+ ```ruby
141
+ response =
142
+ userplex.users.identify(
143
+ email: "REPLACE_ME",
144
+ name: "REPLACE_ME",
145
+ user_id: "REPLACE_ME",
146
+ request_options: {
147
+ extra_query: {my_query_parameter: value},
148
+ extra_body: {my_body_parameter: value},
149
+ extra_headers: {"my-header": value}
150
+ }
151
+ )
152
+
153
+ puts(response[:my_undocumented_property])
154
+ ```
155
+
156
+ #### Undocumented request params
157
+
158
+ 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.
159
+
160
+ #### Undocumented endpoints
161
+
162
+ 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:
163
+
164
+ ```ruby
165
+ response = client.request(
166
+ method: :post,
167
+ path: '/undocumented/endpoint',
168
+ query: {"dog": "woof"},
169
+ headers: {"useful-header": "interesting-value"},
170
+ body: {"hello": "world"}
171
+ )
172
+ ```
173
+
174
+ ### Concurrency & connection pooling
175
+
176
+ The `Userplex::Client` instances are threadsafe, but are only are fork-safe when there are no in-flight HTTP requests.
177
+
178
+ Each instance of `Userplex::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.
179
+
180
+ 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.
181
+
182
+ Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
183
+
184
+ ## Sorbet
185
+
186
+ This library provides comprehensive [RBI](https://sorbet.org/docs/rbi) definitions, and has no dependency on sorbet-runtime.
187
+
188
+ You can provide typesafe request parameters like so:
189
+
190
+ ```ruby
191
+ userplex.users.identify(email: "REPLACE_ME", name: "REPLACE_ME", user_id: "REPLACE_ME")
192
+ ```
193
+
194
+ Or, equivalently:
195
+
196
+ ```ruby
197
+ # Hashes work, but are not typesafe:
198
+ userplex.users.identify(email: "REPLACE_ME", name: "REPLACE_ME", user_id: "REPLACE_ME")
199
+
200
+ # You can also splat a full Params class:
201
+ params = Userplex::UserIdentifyParams.new(email: "REPLACE_ME", name: "REPLACE_ME", user_id: "REPLACE_ME")
202
+ userplex.users.identify(**params)
203
+ ```
204
+
205
+ ## Versioning
206
+
207
+ 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.
208
+
209
+ This package considers improvements to the (non-runtime) `*.rbi` and `*.rbs` type definitions to be non-breaking changes.
210
+
211
+ ## Requirements
212
+
213
+ Ruby 3.2.0 or higher.
214
+
215
+ ## Contributing
216
+
217
+ See [the contributing documentation](https://github.com/dqnamo/userplex-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 Userplex, 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,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Userplex
4
+ class Client < Userplex::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
+ # Enter your API key from your project settings
19
+ # @return [String]
20
+ attr_reader :api_key
21
+
22
+ # @return [Userplex::Resources::Users]
23
+ attr_reader :users
24
+
25
+ # @return [Userplex::Resources::Events]
26
+ attr_reader :events
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] Enter your API key from your project settings Defaults to
40
+ # `ENV["USERPLEX_API_KEY"]`
41
+ #
42
+ # @param base_url [String, nil] Override the default base URL for the API, e.g.,
43
+ # `"https://api.example.com/v2/"`. Defaults to `ENV["USERPLEX_BASE_URL"]`
44
+ #
45
+ # @param max_retries [Integer] Max number of retries to attempt after a failed retryable request.
46
+ #
47
+ # @param timeout [Float]
48
+ #
49
+ # @param initial_retry_delay [Float]
50
+ #
51
+ # @param max_retry_delay [Float]
52
+ def initialize(
53
+ api_key: ENV["USERPLEX_API_KEY"],
54
+ base_url: ENV["USERPLEX_BASE_URL"],
55
+ max_retries: self.class::DEFAULT_MAX_RETRIES,
56
+ timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
57
+ initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
58
+ max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
59
+ )
60
+ base_url ||= "http://localhost:3000"
61
+
62
+ if api_key.nil?
63
+ raise ArgumentError.new("api_key is required, and can be set via environ: \"USERPLEX_API_KEY\"")
64
+ end
65
+
66
+ @api_key = api_key.to_s
67
+
68
+ super(
69
+ base_url: base_url,
70
+ timeout: timeout,
71
+ max_retries: max_retries,
72
+ initial_retry_delay: initial_retry_delay,
73
+ max_retry_delay: max_retry_delay
74
+ )
75
+
76
+ @users = Userplex::Resources::Users.new(client: self)
77
+ @events = Userplex::Resources::Events.new(client: self)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Userplex
4
+ module Errors
5
+ class Error < StandardError
6
+ # @!attribute cause
7
+ #
8
+ # @return [StandardError, nil]
9
+ end
10
+
11
+ class ConversionError < Userplex::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 < Userplex::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 < Userplex::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 < Userplex::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 < Userplex::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
+ Userplex::Errors::BadRequestError.new(**kwargs)
150
+ in 401
151
+ Userplex::Errors::AuthenticationError.new(**kwargs)
152
+ in 403
153
+ Userplex::Errors::PermissionDeniedError.new(**kwargs)
154
+ in 404
155
+ Userplex::Errors::NotFoundError.new(**kwargs)
156
+ in 409
157
+ Userplex::Errors::ConflictError.new(**kwargs)
158
+ in 422
159
+ Userplex::Errors::UnprocessableEntityError.new(**kwargs)
160
+ in 429
161
+ Userplex::Errors::RateLimitError.new(**kwargs)
162
+ in (500..)
163
+ Userplex::Errors::InternalServerError.new(**kwargs)
164
+ else
165
+ Userplex::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 < Userplex::Errors::APIStatusError
197
+ HTTP_STATUS = 400
198
+ end
199
+
200
+ class AuthenticationError < Userplex::Errors::APIStatusError
201
+ HTTP_STATUS = 401
202
+ end
203
+
204
+ class PermissionDeniedError < Userplex::Errors::APIStatusError
205
+ HTTP_STATUS = 403
206
+ end
207
+
208
+ class NotFoundError < Userplex::Errors::APIStatusError
209
+ HTTP_STATUS = 404
210
+ end
211
+
212
+ class ConflictError < Userplex::Errors::APIStatusError
213
+ HTTP_STATUS = 409
214
+ end
215
+
216
+ class UnprocessableEntityError < Userplex::Errors::APIStatusError
217
+ HTTP_STATUS = 422
218
+ end
219
+
220
+ class RateLimitError < Userplex::Errors::APIStatusError
221
+ HTTP_STATUS = 429
222
+ end
223
+
224
+ class InternalServerError < Userplex::Errors::APIStatusError
225
+ HTTP_STATUS = (500..)
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Userplex
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