proc 0.7.0 → 0.10.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: 3436b4fd5fc66c40110f6dbf9cae15ab41d3e056126efd0aeb2ea1f3d9a0dd17
4
- data.tar.gz: 1e4d324315a902f1f53ad611f8379cc6a3ec86f7c897c0dd27cf8901736c85ce
3
+ metadata.gz: cc5ea46072b5407ec2b71822b8406163aeecd2aa6a57d10773e440bb08721ac5
4
+ data.tar.gz: 4640295398b9266d57361f62aadf461b9d23e69f9fa0d21d60c5d1cd8b00715f
5
5
  SHA512:
6
- metadata.gz: ac68f3119ab651efe8705ebe7cc68ad33e7cf9b49c58248c390f5667e31f7521b2b8494f169e213aae4a1a4371df45c6d485b04af743ddb872c993ed04c0dd2b
7
- data.tar.gz: cc114dca8b977b3b365127120ad0f1ae37b53e0e75e5f2a025d9bc0741ebdc5f9fb667be6fd4df2793eab0fd9c02ea5b902146f995624d832f91642d2a4064cc
6
+ metadata.gz: 45bb39ab2f194fe673f22a09d7d681c8514e639c71829a5673ac437991652212260fa6031c46fbdf92c48397865e1552ca7fdda72064a0ce6011c29dbee5e018
7
+ data.tar.gz: 8d1ae22aa4ec5f39cd4c10e2fdf1fa24ad38355e511d5d12cd8935563cb7f6a835a166683922e56bf7db9cc30a5dda7dc649c4c39f3c47b060c5f528f4077331
data/LICENSE CHANGED
@@ -1,22 +1,353 @@
1
- This software is licensed under the MIT License.
2
-
3
- Copyright 2020 Metabahn.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a
6
- copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to permit
10
- persons to whom the Software is furnished to do so, subject to the
11
- following conditions:
12
-
13
- The above copyright notice and this permission notice shall be included
14
- in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
19
- NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20
- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22
- USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Mozilla Public License, version 2.0
2
+
3
+ 1. Definitions
4
+
5
+ 1.1. “Contributor”
6
+
7
+ means each individual or legal entity that creates, contributes to the
8
+ creation of, or owns Covered Software.
9
+
10
+ 1.2. “Contributor Version”
11
+
12
+ means the combination of the Contributions of others (if any) used by a
13
+ Contributor and that particular Contributor’s Contribution.
14
+
15
+ 1.3. “Contribution”
16
+
17
+ means Covered Software of a particular Contributor.
18
+
19
+ 1.4. “Covered Software”
20
+
21
+ means Source Code Form to which the initial Contributor has attached the
22
+ notice in Exhibit A, the Executable Form of such Source Code Form, and
23
+ Modifications of such Source Code Form, in each case including portions
24
+ thereof.
25
+
26
+ 1.5. “Incompatible With Secondary Licenses”
27
+ means
28
+
29
+ a. that the initial Contributor has attached the notice described in
30
+ Exhibit B to the Covered Software; or
31
+
32
+ b. that the Covered Software was made available under the terms of version
33
+ 1.1 or earlier of the License, but not also under the terms of a
34
+ Secondary License.
35
+
36
+ 1.6. “Executable Form”
37
+
38
+ means any form of the work other than Source Code Form.
39
+
40
+ 1.7. “Larger Work”
41
+
42
+ means a work that combines Covered Software with other material, in a separate
43
+ file or files, that is not Covered Software.
44
+
45
+ 1.8. “License”
46
+
47
+ means this document.
48
+
49
+ 1.9. “Licensable”
50
+
51
+ means having the right to grant, to the maximum extent possible, whether at the
52
+ time of the initial grant or subsequently, any and all of the rights conveyed by
53
+ this License.
54
+
55
+ 1.10. “Modifications”
56
+
57
+ means any of the following:
58
+
59
+ a. any file in Source Code Form that results from an addition to, deletion
60
+ from, or modification of the contents of Covered Software; or
61
+
62
+ b. any new file in Source Code Form that contains any Covered Software.
63
+
64
+ 1.11. “Patent Claims” of a Contributor
65
+
66
+ means any patent claim(s), including without limitation, method, process,
67
+ and apparatus claims, in any patent Licensable by such Contributor that
68
+ would be infringed, but for the grant of the License, by the making,
69
+ using, selling, offering for sale, having made, import, or transfer of
70
+ either its Contributions or its Contributor Version.
71
+
72
+ 1.12. “Secondary License”
73
+
74
+ means either the GNU General Public License, Version 2.0, the GNU Lesser
75
+ General Public License, Version 2.1, the GNU Affero General Public
76
+ License, Version 3.0, or any later versions of those licenses.
77
+
78
+ 1.13. “Source Code Form”
79
+
80
+ means the form of the work preferred for making modifications.
81
+
82
+ 1.14. “You” (or “Your”)
83
+
84
+ means an individual or a legal entity exercising rights under this
85
+ License. For legal entities, “You” includes any entity that controls, is
86
+ controlled by, or is under common control with You. For purposes of this
87
+ definition, “control” means (a) the power, direct or indirect, to cause
88
+ the direction or management of such entity, whether by contract or
89
+ otherwise, or (b) ownership of more than fifty percent (50%) of the
90
+ outstanding shares or beneficial ownership of such entity.
91
+
92
+
93
+ 2. License Grants and Conditions
94
+
95
+ 2.1. Grants
96
+
97
+ Each Contributor hereby grants You a world-wide, royalty-free,
98
+ non-exclusive license:
99
+
100
+ a. under intellectual property rights (other than patent or trademark)
101
+ Licensable by such Contributor to use, reproduce, make available,
102
+ modify, display, perform, distribute, and otherwise exploit its
103
+ Contributions, either on an unmodified basis, with Modifications, or as
104
+ part of a Larger Work; and
105
+
106
+ b. under Patent Claims of such Contributor to make, use, sell, offer for
107
+ sale, have made, import, and otherwise transfer either its Contributions
108
+ or its Contributor Version.
109
+
110
+ 2.2. Effective Date
111
+
112
+ The licenses granted in Section 2.1 with respect to any Contribution become
113
+ effective for each Contribution on the date the Contributor first distributes
114
+ such Contribution.
115
+
116
+ 2.3. Limitations on Grant Scope
117
+
118
+ The licenses granted in this Section 2 are the only rights granted under this
119
+ License. No additional rights or licenses will be implied from the distribution
120
+ or licensing of Covered Software under this License. Notwithstanding Section
121
+ 2.1(b) above, no patent license is granted by a Contributor:
122
+
123
+ a. for any code that a Contributor has removed from Covered Software; or
124
+
125
+ b. for infringements caused by: (i) Your and any other third party’s
126
+ modifications of Covered Software, or (ii) the combination of its
127
+ Contributions with other software (except as part of its Contributor
128
+ Version); or
129
+
130
+ c. under Patent Claims infringed by Covered Software in the absence of its
131
+ Contributions.
132
+
133
+ This License does not grant any rights in the trademarks, service marks, or
134
+ logos of any Contributor (except as may be necessary to comply with the
135
+ notice requirements in Section 3.4).
136
+
137
+ 2.4. Subsequent Licenses
138
+
139
+ No Contributor makes additional grants as a result of Your choice to
140
+ distribute the Covered Software under a subsequent version of this License
141
+ (see Section 10.2) or under the terms of a Secondary License (if permitted
142
+ under the terms of Section 3.3).
143
+
144
+ 2.5. Representation
145
+
146
+ Each Contributor represents that the Contributor believes its Contributions
147
+ are its original creation(s) or it has sufficient rights to grant the
148
+ rights to its Contributions conveyed by this License.
149
+
150
+ 2.6. Fair Use
151
+
152
+ This License is not intended to limit any rights You have under applicable
153
+ copyright doctrines of fair use, fair dealing, or other equivalents.
154
+
155
+ 2.7. Conditions
156
+
157
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
158
+ Section 2.1.
159
+
160
+
161
+ 3. Responsibilities
162
+
163
+ 3.1. Distribution of Source Form
164
+
165
+ All distribution of Covered Software in Source Code Form, including any
166
+ Modifications that You create or to which You contribute, must be under the
167
+ terms of this License. You must inform recipients that the Source Code Form
168
+ of the Covered Software is governed by the terms of this License, and how
169
+ they can obtain a copy of this License. You may not attempt to alter or
170
+ restrict the recipients’ rights in the Source Code Form.
171
+
172
+ 3.2. Distribution of Executable Form
173
+
174
+ If You distribute Covered Software in Executable Form then:
175
+
176
+ a. such Covered Software must also be made available in Source Code Form,
177
+ as described in Section 3.1, and You must inform recipients of the
178
+ Executable Form how they can obtain a copy of such Source Code Form by
179
+ reasonable means in a timely manner, at a charge no more than the cost
180
+ of distribution to the recipient; and
181
+
182
+ b. You may distribute such Executable Form under the terms of this License,
183
+ or sublicense it under different terms, provided that the license for
184
+ the Executable Form does not attempt to limit or alter the recipients’
185
+ rights in the Source Code Form under this License.
186
+
187
+ 3.3. Distribution of a Larger Work
188
+
189
+ You may create and distribute a Larger Work under terms of Your choice,
190
+ provided that You also comply with the requirements of this License for the
191
+ Covered Software. If the Larger Work is a combination of Covered Software
192
+ with a work governed by one or more Secondary Licenses, and the Covered
193
+ Software is not Incompatible With Secondary Licenses, this License permits
194
+ You to additionally distribute such Covered Software under the terms of
195
+ such Secondary License(s), so that the recipient of the Larger Work may, at
196
+ their option, further distribute the Covered Software under the terms of
197
+ either this License or such Secondary License(s).
198
+
199
+ 3.4. Notices
200
+
201
+ You may not remove or alter the substance of any license notices (including
202
+ copyright notices, patent notices, disclaimers of warranty, or limitations
203
+ of liability) contained within the Source Code Form of the Covered
204
+ Software, except that You may alter any license notices to the extent
205
+ required to remedy known factual inaccuracies.
206
+
207
+ 3.5. Application of Additional Terms
208
+
209
+ You may choose to offer, and to charge a fee for, warranty, support,
210
+ indemnity or liability obligations to one or more recipients of Covered
211
+ Software. However, You may do so only on Your own behalf, and not on behalf
212
+ of any Contributor. You must make it absolutely clear that any such
213
+ warranty, support, indemnity, or liability obligation is offered by You
214
+ alone, and You hereby agree to indemnify every Contributor for any
215
+ liability incurred by such Contributor as a result of warranty, support,
216
+ indemnity or liability terms You offer. You may include additional
217
+ disclaimers of warranty and limitations of liability specific to any
218
+ jurisdiction.
219
+
220
+ 4. Inability to Comply Due to Statute or Regulation
221
+
222
+ If it is impossible for You to comply with any of the terms of this License
223
+ with respect to some or all of the Covered Software due to statute, judicial
224
+ order, or regulation then You must: (a) comply with the terms of this License
225
+ to the maximum extent possible; and (b) describe the limitations and the code
226
+ they affect. Such description must be placed in a text file included with all
227
+ distributions of the Covered Software under this License. Except to the
228
+ extent prohibited by statute or regulation, such description must be
229
+ sufficiently detailed for a recipient of ordinary skill to be able to
230
+ understand it.
231
+
232
+ 5. Termination
233
+
234
+ 5.1. The rights granted under this License will terminate automatically if You
235
+ fail to comply with any of its terms. However, if You become compliant,
236
+ then the rights granted under this License from a particular Contributor
237
+ are reinstated (a) provisionally, unless and until such Contributor
238
+ explicitly and finally terminates Your grants, and (b) on an ongoing basis,
239
+ if such Contributor fails to notify You of the non-compliance by some
240
+ reasonable means prior to 60 days after You have come back into compliance.
241
+ Moreover, Your grants from a particular Contributor are reinstated on an
242
+ ongoing basis if such Contributor notifies You of the non-compliance by
243
+ some reasonable means, this is the first time You have received notice of
244
+ non-compliance with this License from such Contributor, and You become
245
+ compliant prior to 30 days after Your receipt of the notice.
246
+
247
+ 5.2. If You initiate litigation against any entity by asserting a patent
248
+ infringement claim (excluding declaratory judgment actions, counter-claims,
249
+ and cross-claims) alleging that a Contributor Version directly or
250
+ indirectly infringes any patent, then the rights granted to You by any and
251
+ all Contributors for the Covered Software under Section 2.1 of this License
252
+ shall terminate.
253
+
254
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
255
+ license agreements (excluding distributors and resellers) which have been
256
+ validly granted by You or Your distributors under this License prior to
257
+ termination shall survive termination.
258
+
259
+ 6. Disclaimer of Warranty
260
+
261
+ Covered Software is provided under this License on an “as is” basis, without
262
+ warranty of any kind, either expressed, implied, or statutory, including,
263
+ without limitation, warranties that the Covered Software is free of defects,
264
+ merchantable, fit for a particular purpose or non-infringing. The entire
265
+ risk as to the quality and performance of the Covered Software is with You.
266
+ Should any Covered Software prove defective in any respect, You (not any
267
+ Contributor) assume the cost of any necessary servicing, repair, or
268
+ correction. This disclaimer of warranty constitutes an essential part of this
269
+ License. No use of any Covered Software is authorized under this License
270
+ except under this disclaimer.
271
+
272
+ 7. Limitation of Liability
273
+
274
+ Under no circumstances and under no legal theory, whether tort (including
275
+ negligence), contract, or otherwise, shall any Contributor, or anyone who
276
+ distributes Covered Software as permitted above, be liable to You for any
277
+ direct, indirect, special, incidental, or consequential damages of any
278
+ character including, without limitation, damages for lost profits, loss of
279
+ goodwill, work stoppage, computer failure or malfunction, or any and all
280
+ other commercial damages or losses, even if such party shall have been
281
+ informed of the possibility of such damages. This limitation of liability
282
+ shall not apply to liability for death or personal injury resulting from such
283
+ party’s negligence to the extent applicable law prohibits such limitation.
284
+ Some jurisdictions do not allow the exclusion or limitation of incidental or
285
+ consequential damages, so this exclusion and limitation may not apply to You.
286
+
287
+ 8. Litigation
288
+
289
+ Any litigation relating to this License may be brought only in the courts of
290
+ a jurisdiction where the defendant maintains its principal place of business
291
+ and such litigation shall be governed by laws of that jurisdiction, without
292
+ reference to its conflict-of-law provisions. Nothing in this Section shall
293
+ prevent a party’s ability to bring cross-claims or counter-claims.
294
+
295
+ 9. Miscellaneous
296
+
297
+ This License represents the complete agreement concerning the subject matter
298
+ hereof. If any provision of this License is held to be unenforceable, such
299
+ provision shall be reformed only to the extent necessary to make it
300
+ enforceable. Any law or regulation which provides that the language of a
301
+ contract shall be construed against the drafter shall not be used to construe
302
+ this License against a Contributor.
303
+
304
+
305
+ 10. Versions of the License
306
+
307
+ 10.1. New Versions
308
+
309
+ Mozilla Foundation is the license steward. Except as provided in Section
310
+ 10.3, no one other than the license steward has the right to modify or
311
+ publish new versions of this License. Each version will be given a
312
+ distinguishing version number.
313
+
314
+ 10.2. Effect of New Versions
315
+
316
+ You may distribute the Covered Software under the terms of the version of
317
+ the License under which You originally received the Covered Software, or
318
+ under the terms of any subsequent version published by the license
319
+ steward.
320
+
321
+ 10.3. Modified Versions
322
+
323
+ If you create software not governed by this License, and you want to
324
+ create a new license for such software, you may create and use a modified
325
+ version of this License if you rename the license and remove any
326
+ references to the name of the license steward (except to note that such
327
+ modified license differs from this License).
328
+
329
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
330
+ If You choose to distribute Source Code Form that is Incompatible With
331
+ Secondary Licenses under the terms of this version of the License, the
332
+ notice described in Exhibit B of this License must be attached.
333
+
334
+ Exhibit A - Source Code Form License Notice
335
+
336
+ This Source Code Form is subject to the
337
+ terms of the Mozilla Public License, v.
338
+ 2.0. If a copy of the MPL was not
339
+ distributed with this file, You can
340
+ obtain one at
341
+ http://mozilla.org/MPL/2.0/.
342
+
343
+ If it is not possible or desirable to put the notice in a particular file, then
344
+ You may include the notice in a location (such as a LICENSE file in a relevant
345
+ directory) where a recipient would be likely to look for such a notice.
346
+
347
+ You may add additional accurate notices of copyright ownership.
348
+
349
+ Exhibit B - “Incompatible With Secondary Licenses” Notice
350
+
351
+ This Source Code Form is “Incompatible
352
+ With Secondary Licenses”, as defined by
353
+ the Mozilla Public License, v. 2.0.
data/lib/proc.rb CHANGED
@@ -1,17 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "proc/client"
4
+ require_relative "proc/version"
4
5
 
5
6
  class Proc
6
- def self.connect(authorization, **options)
7
- Client.new(authorization, **options)
8
- end
7
+ class << self
8
+ # [public] Connect a client with an authorization.
9
+ #
10
+ def connect(authorization, **options)
11
+ Client.new(authorization, **options)
12
+ end
9
13
 
10
- def self.undefined
11
- @_undefined ||= ::Object.new
12
- end
14
+ def undefined
15
+ @_undefined ||= ::Object.new
16
+ end
13
17
 
14
- def self.undefined?(value)
15
- value == undefined
18
+ def undefined?(value)
19
+ value == undefined
20
+ end
16
21
  end
17
22
  end
data/lib/proc/callable.rb CHANGED
@@ -16,7 +16,15 @@ class Proc
16
16
  @arguments = arguments.dup
17
17
  end
18
18
 
19
+ # [public] Dispatches this callable context to proc using the client.
20
+ #
21
+ # If a block is passed, it will be called to prior to dispatch and its result passed as a nested context.
22
+ #
19
23
  def call(input = input_omitted = true, **arguments)
24
+ if block_given?
25
+ arguments[:proc] = yield
26
+ end
27
+
20
28
  callable = self.class.new(
21
29
  @proc,
22
30
  client: @client,
@@ -27,7 +35,26 @@ class Proc
27
35
  @client.call(@proc, callable.input, **callable.arguments)
28
36
  end
29
37
 
38
+ # [public] Dispatches this callable context to proc using the client, calling the given block once for each value.
39
+ #
40
+ def each(input = input_omitted = true, **arguments, &block)
41
+ callable = self.class.new(
42
+ @proc,
43
+ client: @client,
44
+ input: input_omitted ? @input : input,
45
+ arguments: @arguments.merge(arguments)
46
+ )
47
+
48
+ @client.call(@proc, callable.input, **callable.arguments, &block)
49
+ end
50
+
51
+ # [public] Creates a new callable context based on this one, with a new input and/or arguments.
52
+ #
30
53
  def with(input = input_omitted = true, **arguments)
54
+ if block_given?
55
+ arguments[:proc] = yield
56
+ end
57
+
31
58
  self.class.new(
32
59
  @proc,
33
60
  client: @client,
@@ -36,6 +63,17 @@ class Proc
36
63
  )
37
64
  end
38
65
 
66
+ # [public] Returns a composition built from this callable context and one or more other callables.
67
+ #
68
+ def compose(*others)
69
+ composed = Composition.new(client: @client, input: @input)
70
+ composed << self
71
+ others.each { |other| composed << other }
72
+ composed
73
+ end
74
+
75
+ # [public] Returns a composition built from this callable context and another callable.
76
+ #
39
77
  def >>(other)
40
78
  composed = Composition.new(client: @client, input: @input)
41
79
  composed << self
@@ -69,21 +107,37 @@ class Proc
69
107
  }
70
108
  end
71
109
 
110
+ # [public] Returns a callable context for `proc`, nested within this callable context.
111
+ #
72
112
  def [](proc)
113
+ arguments = if block_given?
114
+ duped = @arguments.dup
115
+ duped[:proc] = yield
116
+ duped
117
+ else
118
+ @arguments
119
+ end
120
+
73
121
  Callable.new(
74
122
  [@proc, proc].join("."),
75
123
  client: @client,
76
124
  input: @input,
77
- arguments: @arguments
125
+ arguments: arguments
78
126
  )
79
127
  end
80
128
 
81
129
  IGNORE_MISSING = %i[to_hash].freeze
82
130
 
131
+ # [public] Allows nested callable contexts to be built through method lookups.
132
+ #
83
133
  def method_missing(name, input = input_omitted = true, **arguments)
84
134
  if IGNORE_MISSING.include?(name)
85
135
  super
86
136
  else
137
+ if block_given?
138
+ arguments[:proc] = yield
139
+ end
140
+
87
141
  Callable.new(
88
142
  [@proc, name].join("."),
89
143
  client: @client,
@@ -102,7 +156,9 @@ class Proc
102
156
  end
103
157
 
104
158
  private def serialize_value(value)
105
- if value.respond_to?(:serialize)
159
+ if value.is_a?(Symbol)
160
+ ["@@", value.to_s, {}]
161
+ elsif value.respond_to?(:serialize)
106
162
  value.serialize
107
163
  else
108
164
  ["%%", value]
data/lib/proc/client.rb CHANGED
@@ -1,80 +1,139 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "async"
4
- require "oj"
3
+ require "core/async"
4
+ require "http"
5
+ require "msgpack"
6
+
7
+ require_relative "msgpack/types/decimal"
8
+ MessagePack::DefaultFactory.register_type(0x00, Proc::Msgpack::Types::Decimal)
9
+ MessagePack::DefaultFactory.register_type(-1, Time, packer: MessagePack::Time::Packer, unpacker: MessagePack::Time::Unpacker)
5
10
 
6
11
  require_relative "argument"
7
12
  require_relative "callable"
8
13
  require_relative "composition"
9
-
10
- require_relative "http/client"
11
-
12
- Console.logger.off!
13
-
14
- Oj.default_options = {
15
- mode: :custom,
16
- bigdecimal_load: :auto
17
- }.freeze
14
+ require_relative "enumerator"
18
15
 
19
16
  class Proc
17
+ # [public]
18
+ #
20
19
  class Error < StandardError
21
20
  end
22
21
 
22
+ # [public] Raised when input or arguments are invalid.
23
+ #
23
24
  class Invalid < ::ArgumentError
24
25
  end
25
26
 
27
+ # [public] Raised when a proc endpoint does not exist.
28
+ #
26
29
  class Undefined < ::NameError
27
30
  end
28
31
 
32
+ # [public] Raised when the client is unauthorized.
33
+ #
29
34
  class Unauthorized < Error
30
35
  end
31
36
 
37
+ # [public] Raised when a call is authorized but is blocked.
38
+ #
32
39
  class Forbidden < Error
33
40
  end
34
41
 
42
+ # [public] Raised when proc is unavailable due to an unexpected error.
43
+ #
35
44
  class Unavailable < Error
36
45
  end
37
46
 
47
+ # [public] Raised when the client is being rate limited.
48
+ #
38
49
  class Limited < Error
39
50
  end
40
51
 
52
+ # [public] Raised when a proc call surpasses the defined limit.
53
+ #
41
54
  class Timeout < Error
42
55
  end
43
56
 
57
+ # [public] Connection to proc, configured with an authorization.
58
+ #
44
59
  class Client
60
+ include Is::Async
61
+
62
+ # [public] The configured authorization.
63
+ #
64
+ attr_reader :authorization
65
+
66
+ # [public] The configured scheme.
67
+ #
68
+ attr_reader :scheme
69
+
70
+ # [public] The configured host.
71
+ #
72
+ attr_reader :host
73
+
74
+ # [public] The number of requests this client has performed.
75
+ #
76
+ attr_reader :request_count
77
+
78
+ DEFAULT_HEADERS = {
79
+ "accept" => "application/vnd.proc+msgpack",
80
+ "content-type" => "application/vnd.proc+msgpack"
81
+ }.freeze
82
+
45
83
  def initialize(authorization, scheme: "https", host: "proc.dev")
46
84
  @authorization = authorization
47
85
  @scheme = scheme
48
86
  @host = host
49
- @internal = Http::Client.new
87
+ @request_count = 0
88
+
89
+ @__base_url = "#{@scheme}://#{host}"
90
+ @__headers = {
91
+ "authorization" => "bearer #{@authorization}"
92
+ }.merge(DEFAULT_HEADERS)
50
93
  end
51
94
 
95
+ # [public] Returns a callable context for `proc`.
96
+ #
52
97
  def [](proc)
53
- Callable.new(proc, client: self)
98
+ if block_given?
99
+ Callable.new(proc, client: self, arguments: {proc: yield})
100
+ else
101
+ Callable.new(proc, client: self)
102
+ end
54
103
  end
55
104
 
56
- def remaining
57
- unless defined?(@remaining)
58
- self["ping"].call
59
- end
105
+ # [public] Returns the current rate limit.
106
+ #
107
+ def rate_limit
108
+ refresh_rate_limit
109
+ @rate_limit
110
+ end
111
+
112
+ # [public] Returns the current rate limit window.
113
+ #
114
+ def rate_limit_window
115
+ refresh_rate_limit
116
+ @rate_limit_window
117
+ end
60
118
 
61
- @remaining
119
+ # [public] Returns the time at which the current rate limit will reset.
120
+ #
121
+ def rate_limit_reset
122
+ refresh_rate_limit
123
+ @rate_limit_reset
62
124
  end
63
125
 
64
- def resets_at
65
- unless defined?(@resets_at)
126
+ private def refresh_rate_limit
127
+ unless defined?(@rate_limit_reset)
66
128
  self["ping"].call
67
129
  end
68
-
69
- @resets_at
70
130
  end
71
131
 
72
- DEFAULT_HEADERS = {
73
- "accept" => "application/vnd.proc+json",
74
- "content-type" => "application/vnd.proc+json"
75
- }.freeze
76
-
77
- def call(proc = nil, input = Proc.undefined, **arguments)
132
+ # [public] Calls a proc with the given input and arguments.
133
+ #
134
+ # If a block is passed and the proc returns an enumerable, the block will be called with each value.
135
+ #
136
+ def call(proc = nil, input = Proc.undefined, **arguments, &block)
78
137
  body = []
79
138
 
80
139
  unless Proc.undefined?(input)
@@ -85,15 +144,30 @@ class Proc
85
144
  body << ["$$", key.to_s, serialize_value(value)]
86
145
  end
87
146
 
88
- headers = {
89
- "authorization" => "bearer #{@authorization}"
90
- }.merge(DEFAULT_HEADERS)
91
-
92
- status, payload = get_payload(proc: proc, headers: headers, body: body)
147
+ status, headers, payload = get_payload(proc: proc, body: body)
93
148
 
94
149
  case status
95
150
  when 200
96
- extract_output(payload)
151
+ result = extract_output(payload)
152
+
153
+ if (cursor = headers["x-cursor"])
154
+ enumerator = if cursor.empty?
155
+ Enumerator.new(result)
156
+ else
157
+ Enumerator.new(result) {
158
+ arguments[:cursor] = cursor.to_s
159
+ call(proc, input, **arguments)
160
+ }
161
+ end
162
+
163
+ if block
164
+ enumerator.each(&block)
165
+ else
166
+ enumerator
167
+ end
168
+ else
169
+ result
170
+ end
97
171
  when 400
98
172
  raise Proc::Invalid, extract_error_message(payload)
99
173
  when 401
@@ -117,6 +191,8 @@ class Proc
117
191
  end
118
192
  end
119
193
 
194
+ # [public] Allows callable contexts to be built through method lookups.
195
+ #
120
196
  def method_missing(name, input = input_omitted = true, *, **arguments)
121
197
  if input_omitted
122
198
  Callable.new(name, client: self, arguments: arguments)
@@ -129,39 +205,58 @@ class Proc
129
205
  true
130
206
  end
131
207
 
208
+ # [public] Builds a named argument with options.
209
+ #
132
210
  def argument(name, **options)
133
211
  Argument.new(name, **options)
134
212
  end
135
213
  alias_method :arg, :argument
136
214
 
137
- def close
138
- @internal.close
139
- end
140
-
141
215
  private def build_uri(proc)
142
- host_and_path = File.join(@host, proc.to_s.split(".").join("/"))
143
-
144
- "#{@scheme}://#{host_and_path}"
216
+ File.join(@__base_url, proc.to_s.split(".").join("/"))
145
217
  end
146
218
 
147
219
  private def serialize_value(value)
148
- if value.respond_to?(:serialize)
220
+ if value.is_a?(Symbol)
221
+ ["@@", value.to_s, {}]
222
+ elsif value.respond_to?(:serialize)
149
223
  value.serialize
150
224
  else
151
225
  ["%%", value]
152
226
  end
153
227
  end
154
228
 
155
- private def get_payload(proc:, headers:, body:)
156
- @internal.call(:post, build_uri(proc), headers: headers, body: Oj.dump(body)) do |response|
157
- @remaining = response.headers["x-rate-limit-remaining"].to_s.to_i
158
- @resets_at = Time.at(response.headers["x-rate-limit-reset"].to_s.to_i)
159
- [response.status, Oj.load(response.read)]
160
- end
229
+ private def get_payload(proc:, body:)
230
+ await {
231
+ @request_count += 1
232
+
233
+ response = HTTP.headers(@__headers).post(build_uri(proc), body: MessagePack.pack(body))
234
+
235
+ update_rate_limit(response)
236
+
237
+ [response.status, response.headers, MessagePack.unpack(response.to_s)]
238
+ }
161
239
  rescue
162
240
  raise Proc::Unavailable
163
241
  end
164
242
 
243
+ private def update_rate_limit(response)
244
+ split_limit = response.headers["x-rate-limit"].to_s.split(";window=")
245
+
246
+ @rate_limit = split_limit[0].to_i
247
+
248
+ @rate_limit_window = case split_limit[1]
249
+ when "60"
250
+ :minute
251
+ when "1"
252
+ :second
253
+ end
254
+
255
+ @rate_limit_reset = if (reset = response.headers["x-rate-limit-reset"])
256
+ Time.at(reset.to_s.to_i)
257
+ end
258
+ end
259
+
165
260
  private def extract_output(payload)
166
261
  payload.each do |tuple|
167
262
  case tuple[0]
@@ -15,7 +15,13 @@ class Proc
15
15
  @callables = @callables.dup
16
16
  end
17
17
 
18
+ # [public] Dispatches this composition to proc using the client.
19
+ #
18
20
  def call(input = input_omitted = true, **arguments)
21
+ if block_given?
22
+ arguments[:proc] = yield
23
+ end
24
+
19
25
  callable = self.class.new(
20
26
  client: @client,
21
27
  input: input_omitted ? @input : input,
@@ -26,7 +32,26 @@ class Proc
26
32
  @client.call("exec", Proc.undefined, proc: callable)
27
33
  end
28
34
 
35
+ # [public] Dispatches this composition to proc using the client, calling the given block once for each value.
36
+ #
37
+ def each(input = input_omitted = true, **arguments, &block)
38
+ callable = self.class.new(
39
+ client: @client,
40
+ input: input_omitted ? @input : input,
41
+ callables: @callables.dup,
42
+ arguments: @arguments.merge(arguments)
43
+ )
44
+
45
+ @client.call("exec", Proc.undefined, proc: callable, &block)
46
+ end
47
+
48
+ # [public] Creates a new composition based on this one, with a new input and/or arguments.
49
+ #
29
50
  def with(input = input_omitted = true, **arguments)
51
+ if block_given?
52
+ arguments[:proc] = yield
53
+ end
54
+
30
55
  self.class.new(
31
56
  client: @client,
32
57
  input: input_omitted ? @input : input,
@@ -35,6 +60,16 @@ class Proc
35
60
  )
36
61
  end
37
62
 
63
+ # [public] Returns a composition from this composition and one or more other callables.
64
+ #
65
+ def compose(*others)
66
+ composed = dup
67
+ others.each { |other| composed << other }
68
+ composed
69
+ end
70
+
71
+ # [public] Returns a composition built from this composition and another callable.
72
+ #
38
73
  def >>(other)
39
74
  composed = dup
40
75
  composed << other
@@ -78,7 +113,9 @@ class Proc
78
113
  end
79
114
 
80
115
  private def serialize_value(value)
81
- if value.respond_to?(:serialize)
116
+ if value.is_a?(Symbol)
117
+ ["@@", value.to_s, {}]
118
+ elsif value.respond_to?(:serialize)
82
119
  value.serialize
83
120
  else
84
121
  ["%%", value]
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Proc
4
+ class Enumerator
5
+ include Enumerable
6
+
7
+ attr_reader :values, :next_block
8
+
9
+ def initialize(values, &next_block)
10
+ @values = values
11
+ @next_block = next_block
12
+ end
13
+
14
+ # [public] Calls the given block once for each value.
15
+ #
16
+ def each(enumerable = self, &block)
17
+ return to_enum(:each) unless block
18
+
19
+ while enumerable
20
+ enumerable.values.each(&block)
21
+ enumerable = enumerable.next_block&.call
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class BigDecimal
4
+ def to_msgpack(packer)
5
+ if precision > 16
6
+ packer.write(Proc::Msgpack::Types::Decimal.new(self))
7
+ packer
8
+ else
9
+ to_f.to_msgpack(packer)
10
+ end
11
+ end
12
+ end
13
+
14
+ class Proc
15
+ module Msgpack
16
+ module Types
17
+ class Decimal
18
+ class << self
19
+ def from_msgpack_ext(data)
20
+ BigDecimal(data)
21
+ end
22
+ end
23
+
24
+ def initialize(value)
25
+ @value = value
26
+ end
27
+
28
+ def to_msgpack_ext
29
+ @value.to_s
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/proc/version.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Proc
4
- VERSION = "0.7.0"
4
+ VERSION = "0.10.0"
5
5
 
6
+ # [public]
7
+ #
6
8
  def self.version
7
9
  VERSION
8
10
  end
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Powell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-16 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: async-http
14
+ name: core-async
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.54.1
19
+ version: 0.5.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.54.1
26
+ version: 0.5.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: core-async
28
+ name: http
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.0
33
+ version: '4.4'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.1.0
40
+ version: '4.4'
41
41
  - !ruby/object:Gem::Dependency
42
- name: oj
42
+ name: msgpack
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.11'
47
+ version: '1.4'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.11'
54
+ version: '1.4'
55
55
  description: Proc client library.
56
56
  email: bryan@metabahn.com
57
57
  executables: []
@@ -65,13 +65,12 @@ files:
65
65
  - lib/proc/callable.rb
66
66
  - lib/proc/client.rb
67
67
  - lib/proc/composition.rb
68
- - lib/proc/http/client.rb
69
- - lib/proc/http/request.rb
70
- - lib/proc/http/response.rb
68
+ - lib/proc/enumerator.rb
69
+ - lib/proc/msgpack/types/decimal.rb
71
70
  - lib/proc/version.rb
72
71
  homepage: https://proc.dev/
73
72
  licenses:
74
- - MIT
73
+ - MPL-2.0
75
74
  metadata: {}
76
75
  post_install_message:
77
76
  rdoc_options: []
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "async"
4
- require "async/http/internet"
5
-
6
- require "core/async"
7
-
8
- require_relative "request"
9
- require_relative "response"
10
-
11
- class Proc
12
- module Http
13
- class Client
14
- include Is::Async
15
-
16
- def call(method, uri, params: {}, headers: {}, body: nil)
17
- internet = Async::HTTP::Internet.new
18
- request = Request.new(method: method, uri: uri, params: params, headers: headers, body: body)
19
-
20
- await {
21
- begin
22
- response = make_request(internet, request)
23
-
24
- yield response
25
- ensure
26
- response&.close
27
- internet&.close
28
- end
29
- }
30
- end
31
-
32
- private def make_request(internet, request)
33
- async_response = internet.call(*request.callable)
34
- wrap_async_response_for_request(async_response, request)
35
- end
36
-
37
- private def wrap_async_response_for_request(async_response, request)
38
- Response.new(request, async_response)
39
- end
40
- end
41
- end
42
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Proc
4
- module Http
5
- class Request
6
- attr_reader :method, :uri, :params, :headers, :body
7
-
8
- def initialize(method:, uri:, params:, headers:, body:)
9
- @method = method
10
- @uri = uri
11
- @params = params
12
- @headers = headers
13
- @body = body
14
- end
15
-
16
- def callable
17
- [@method.to_s.upcase, finalize_uri(@uri, @params), @headers, @body]
18
- end
19
-
20
- private def finalize_uri(uri, params)
21
- "#{uri}#{query_string(params)}"
22
- end
23
-
24
- private def query_string(params)
25
- params = params.compact
26
-
27
- if params.any?
28
- "?" + params.compact.map { |key, value|
29
- "#{key}=#{value}"
30
- }.join("&")
31
- else
32
- ""
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "forwardable"
4
-
5
- class Proc
6
- module Http
7
- class Response
8
- extend Forwardable
9
- def_delegators :@response, :status, :version, :headers, :read, :close
10
-
11
- attr_reader :request
12
-
13
- def initialize(request, response)
14
- @request = request
15
- @response = response
16
- end
17
- end
18
- end
19
- end