proc 0.9.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46f47f39b1b2b4f495b59575a14f1d8df1d837eada9048562ed5455a3d0fbc06
4
- data.tar.gz: e93f11f26736d3504fc4e025be2fece6e9dafad9e66d6fac4c2d362470d3b207
3
+ metadata.gz: b102aa8b4a6c4f99758cab2f6aefff1800948b60197d920f6279cb76f26decbb
4
+ data.tar.gz: 37b8a0aa99c4089a584b2b7197ab7e9e9ec7bc555a59bdaf1bbce76f4ab466ac
5
5
  SHA512:
6
- metadata.gz: c82d1cb2697caf43077d2e1a909602401cb7bf8a8518be6f21d5160a38889d49aceb93d1d578c124ca8077dec21aa7e6b5fdec31ebb5e4137e64ab278e58e124
7
- data.tar.gz: ca6f582486efebd0817b30228e997da049c482e7c5b42f08b52128e76534d3da19080aa995e0124d15e011677f5af066c0310a7a67671788cf44cffc040e62d5
6
+ metadata.gz: 33e2381e9f9d5ff7a6ff10c7ad4a75cc2d0687a4e6890916d4443563ddcaf883d7205b971e7dee63637f81731cea2c5caf80df1a980718db65c78b94db19c0ca
7
+ data.tar.gz: fc7497fce516a9b003ed4dbf617d26c9c7ca5e6b2696f324cba275f765f85f6f878baaea2e7006d3e2e023a61de73b506aff743d1d3ae1451fa9226c2c9040d4
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/README.md CHANGED
@@ -1,80 +1,58 @@
1
- The `proc` gem lets you call codeless web functions through the proc.dev service.
1
+ **Superpowers to help you ship web endpoints faster.**
2
2
 
3
- ## Getting Started
3
+ Proc is an all-in-one toolchain for building, deploying, and calling custom behavior from any website or app.
4
4
 
5
- Install `proc` using the `gem` command from the command line:
5
+ * [Learn more about Proc](https://proc.dev)
6
+ * [Browse packages](https://proc.dev/packages)
7
+ * [Read the docs](https://proc.dev/docs)
8
+ * [Chat with us](https://discord.gg/aRu8qvkCmy)
6
9
 
7
- ```
8
- gem install proc
9
- ```
10
-
11
- You will also need a proc.dev account. Create a free account in seconds at [proc.dev](https://proc.dev/).
12
-
13
- ## Connecting
10
+ ## Install
14
11
 
15
- Sign in to your proc.dev account and locate your secret key. Use the secret to create a client connection:
12
+ Install with `gem install proc`:
16
13
 
17
- ```ruby
18
- client = Proc.connect("secret-key")
19
14
  ```
20
-
21
- ## Calling Procs
22
-
23
- You can call available procs through a connected client:
24
-
25
- ```ruby
26
- client.core.string.reverse.call("hello")
27
- => "olleh"
15
+ gem install proc
28
16
  ```
29
17
 
30
- Requests are sent to proc.dev anytime the `call` method is invoked.
18
+ ## Usage
31
19
 
32
- ## Callable Contexts
33
-
34
- Proc lookups create contexts that can be called later:
20
+ Connect to proc using an account secret or limited api key:
35
21
 
36
22
  ```ruby
37
- string = client.core.string
38
-
39
- string.reverse.call("hello")
40
- => "olleh"
23
+ require "proc"
41
24
 
42
- string.truncate.call("hello", length: 3)
43
- => "hel"
25
+ client = Proc.connect("{your-proc-authorization}")
44
26
  ```
45
27
 
46
- Contexts can be configured with default input and arguments using the `with` method:
28
+ Now you can call procs just like local code:
47
29
 
48
30
  ```ruby
49
- truncate = client.core.string.truncate.with("default", length: 1)
31
+ client.type.number.add.call(1, {value: 1});
50
32
 
51
- truncate.call
52
- => "d"
33
+ => 2
53
34
  ```
54
35
 
55
- Default input and/or arguments can be overidden for a specific call:
36
+ Build more complex behavior by composing procs together:
56
37
 
57
38
  ```ruby
58
- truncate.call(length: 3)
59
- => "def"
60
- ```
39
+ time = client.time
61
40
 
62
- Procs can also be looked up using the hash key syntax:
41
+ composition = time.now >> time.format(string: "%A")
63
42
 
64
- ```ruby
65
- client["core.string.truncate"].call("hello", length: 3)
66
- => "hel"
67
- ```
43
+ composition.call
68
44
 
69
- ## Compositions
45
+ => "Tuesday"
46
+ ````
70
47
 
71
- Procs can be composed together to build more complex behavior:
48
+ Instantly deploy your behavior to a private endpoint and call it from anywhere:
72
49
 
73
50
  ```ruby
74
- composition = client.core.string.reverse >> client.core.string.truncate(length: 3) >> client.core.string.capitalize
51
+ client.proc.create.call(name: "day_of_week", proc: composition)
52
+
53
+ client.self.day_of_week.call
75
54
 
76
- composition.call("hello")
77
- => "Oll"
55
+ => "Tuesday"
78
56
  ```
79
57
 
80
- Compositions are sent to proc.dev in a single request.
58
+ Learn more at [proc.dev](https://proc.dev). See you around!
data/lib/proc.rb CHANGED
@@ -4,15 +4,19 @@ require_relative "proc/client"
4
4
  require_relative "proc/version"
5
5
 
6
6
  class Proc
7
- def self.connect(authorization, **options)
8
- Client.new(authorization, **options)
9
- 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
10
13
 
11
- def self.undefined
12
- @_undefined ||= ::Object.new
13
- end
14
+ def undefined
15
+ @_undefined ||= ::Object.new
16
+ end
14
17
 
15
- def self.undefined?(value)
16
- value == undefined
18
+ def undefined?(value)
19
+ value == undefined
20
+ end
17
21
  end
18
22
  end
data/lib/proc/callable.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Proc
4
- class Callable
4
+ class Callable < BasicObject
5
5
  attr_reader :proc, :input, :arguments
6
6
 
7
- def initialize(proc, client:, input: Proc.undefined, arguments: {})
7
+ def initialize(proc, client:, input: ::Proc.undefined, arguments: {})
8
8
  @proc = proc.to_s
9
9
  @client = client
10
10
  @input = input
@@ -16,8 +16,29 @@ class Proc
16
16
  @arguments = arguments.dup
17
17
  end
18
18
 
19
- def call(input = input_omitted = true, **arguments, &block)
20
- callable = self.class.new(
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
+ #
23
+ def call(input = input_omitted = true, **arguments)
24
+ if ::Kernel.block_given?
25
+ arguments[:proc] = yield
26
+ end
27
+
28
+ callable = ::Proc::Callable.new(
29
+ @proc,
30
+ client: @client,
31
+ input: input_omitted ? @input : input,
32
+ arguments: @arguments.merge(arguments)
33
+ )
34
+
35
+ @client.call(@proc, callable.input, **callable.arguments)
36
+ end
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 = ::Proc::Callable.new(
21
42
  @proc,
22
43
  client: @client,
23
44
  input: input_omitted ? @input : input,
@@ -27,8 +48,14 @@ class Proc
27
48
  @client.call(@proc, callable.input, **callable.arguments, &block)
28
49
  end
29
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)
31
- self.class.new(
54
+ if ::Kernel.block_given?
55
+ arguments[:proc] = yield
56
+ end
57
+
58
+ ::Proc::Callable.new(
32
59
  @proc,
33
60
  client: @client,
34
61
  input: input_omitted ? @input : input,
@@ -36,8 +63,19 @@ 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 = ::Proc::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
- composed = Composition.new(client: @client, input: @input)
78
+ composed = ::Proc::Composition.new(client: @client, input: @input)
41
79
  composed << self
42
80
  composed << other
43
81
  composed
@@ -46,7 +84,7 @@ class Proc
46
84
  def serialize(unwrapped: false)
47
85
  serialized = ["()", @proc]
48
86
 
49
- unless Proc.undefined?(@input)
87
+ unless ::Proc.undefined?(@input)
50
88
  serialized << [">>", serialized_input]
51
89
  end
52
90
 
@@ -69,22 +107,38 @@ 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)
73
- Callable.new(
113
+ arguments = if ::Kernel.block_given?
114
+ duped = @arguments.dup
115
+ duped[:proc] = yield
116
+ duped
117
+ else
118
+ @arguments
119
+ end
120
+
121
+ ::Proc::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
87
- Callable.new(
137
+ if ::Kernel.block_given?
138
+ arguments[:proc] = yield
139
+ end
140
+
141
+ ::Proc::Callable.new(
88
142
  [@proc, name].join("."),
89
143
  client: @client,
90
144
  input: input_omitted ? @input : input,
@@ -102,7 +156,10 @@ class Proc
102
156
  end
103
157
 
104
158
  private def serialize_value(value)
105
- if value.respond_to?(:serialize)
159
+ case value
160
+ when ::Symbol
161
+ ["@@", value.to_s, {}]
162
+ when ::Proc::Argument, ::Proc::Callable, ::Proc::Composition
106
163
  value.serialize
107
164
  else
108
165
  ["%%", value]
data/lib/proc/client.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "async"
3
+ require "core/async"
4
+ require "http"
4
5
  require "msgpack"
5
6
 
6
7
  require_relative "msgpack/types/decimal"
@@ -12,74 +13,132 @@ require_relative "callable"
12
13
  require_relative "composition"
13
14
  require_relative "enumerator"
14
15
 
15
- require_relative "http/client"
16
-
17
- Console.logger.off!
18
-
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
 
44
- class Client
45
- attr_reader :authorization, :scheme, :host
57
+ # [public] Connection to proc, configured with an authorization.
58
+ #
59
+ class Client < BasicObject
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
+ attr_reader :response
79
+
80
+ DEFAULT_HEADERS = {
81
+ "accept" => "application/vnd.proc+msgpack",
82
+ "content-type" => "application/vnd.proc+msgpack"
83
+ }.freeze
46
84
 
47
85
  def initialize(authorization, scheme: "https", host: "proc.dev")
48
86
  @authorization = authorization
49
87
  @scheme = scheme
50
88
  @host = host
51
- @internal = Http::Client.new
52
- end
89
+ @request_count = 0
53
90
 
54
- def [](proc)
55
- Callable.new(proc, client: self)
91
+ @__base_url = "#{@scheme}://#{host}"
92
+ @__headers = {
93
+ "authorization" => "bearer #{@authorization}"
94
+ }.merge(DEFAULT_HEADERS)
56
95
  end
57
96
 
58
- def remaining
59
- unless defined?(@remaining)
60
- self["ping"].call
97
+ # [public] Returns a callable context for `proc`.
98
+ #
99
+ def [](proc)
100
+ if ::Kernel.block_given?
101
+ ::Proc::Callable.new(proc, client: self, arguments: {proc: yield})
102
+ else
103
+ ::Proc::Callable.new(proc, client: self)
61
104
  end
105
+ end
62
106
 
63
- @remaining
107
+ # [public] Returns the current rate limit.
108
+ #
109
+ def rate_limit
110
+ refresh_rate_limit
111
+ @rate_limit
64
112
  end
65
113
 
66
- def resets_at
67
- unless defined?(@resets_at)
68
- self["ping"].call
69
- end
114
+ # [public] Returns the current rate limit window.
115
+ #
116
+ def rate_limit_window
117
+ refresh_rate_limit
118
+ @rate_limit_window
119
+ end
70
120
 
71
- @resets_at
121
+ # [public] Returns the time at which the current rate limit will reset.
122
+ #
123
+ def rate_limit_reset
124
+ refresh_rate_limit
125
+ @rate_limit_reset
72
126
  end
73
127
 
74
- DEFAULT_HEADERS = {
75
- "accept" => "application/vnd.proc+msgpack",
76
- "content-type" => "application/vnd.proc+msgpack"
77
- }.freeze
128
+ private def refresh_rate_limit
129
+ unless defined?(@rate_limit)
130
+ self["core.ping"].call
131
+ end
132
+ end
78
133
 
79
- def call(proc = nil, input = Proc.undefined, **arguments, &block)
134
+ # [public] Calls a proc with the given input and arguments.
135
+ #
136
+ # If a block is passed and the proc returns an enumerable, the block will be called with each value.
137
+ #
138
+ def call(proc = nil, input = ::Proc.undefined, **arguments, &block)
80
139
  body = []
81
140
 
82
- unless Proc.undefined?(input)
141
+ unless ::Proc.undefined?(input)
83
142
  body << [">>", serialize_value(input)]
84
143
  end
85
144
 
@@ -87,11 +146,7 @@ class Proc
87
146
  body << ["$$", key.to_s, serialize_value(value)]
88
147
  end
89
148
 
90
- headers = {
91
- "authorization" => "bearer #{@authorization}"
92
- }.merge(DEFAULT_HEADERS)
93
-
94
- status, headers, payload = get_payload(proc: proc, headers: headers, body: body)
149
+ status, headers, payload = get_payload(proc: proc, body: body)
95
150
 
96
151
  case status
97
152
  when 200
@@ -99,9 +154,9 @@ class Proc
99
154
 
100
155
  if (cursor = headers["x-cursor"])
101
156
  enumerator = if cursor.empty?
102
- Enumerator.new(result)
157
+ ::Proc::Enumerator.new(result)
103
158
  else
104
- Enumerator.new(result) {
159
+ ::Proc::Enumerator.new(result) {
105
160
  arguments[:cursor] = cursor.to_s
106
161
  call(proc, input, **arguments)
107
162
  }
@@ -116,33 +171,35 @@ class Proc
116
171
  result
117
172
  end
118
173
  when 400
119
- raise Proc::Invalid, extract_error_message(payload)
174
+ ::Kernel.raise ::Proc::Invalid, extract_error_message(payload)
120
175
  when 401
121
- raise Proc::Unauthorized, extract_error_message(payload)
176
+ ::Kernel.raise ::Proc::Unauthorized, extract_error_message(payload)
122
177
  when 403
123
- raise Proc::Forbidden, extract_error_message(payload)
178
+ ::Kernel.raise ::Proc::Forbidden, extract_error_message(payload)
124
179
  when 404
125
- raise Proc::Undefined, extract_error_message(payload)
180
+ ::Kernel.raise ::Proc::Undefined, extract_error_message(payload)
126
181
  when 408
127
- raise Proc::Timeout, extract_error_message(payload)
182
+ ::Kernel.raise ::Proc::Timeout, extract_error_message(payload)
128
183
  when 413
129
- raise Proc::Invalid, extract_error_message(payload)
184
+ ::Kernel.raise ::Proc::Invalid, extract_error_message(payload)
130
185
  when 429
131
- raise Proc::Limited, extract_error_message(payload)
186
+ ::Kernel.raise ::Proc::Limited, extract_error_message(payload)
132
187
  when 500
133
- raise Proc::Error, extract_error_message(payload)
188
+ ::Kernel.raise ::Proc::Error, extract_error_message(payload)
134
189
  when 508
135
- raise Proc::Error, extract_error_message(payload)
190
+ ::Kernel.raise ::Proc::Error, extract_error_message(payload)
136
191
  else
137
- raise Proc::Error, "unhandled"
192
+ ::Kernel.raise ::Proc::Error, "unhandled"
138
193
  end
139
194
  end
140
195
 
196
+ # [public] Allows callable contexts to be built through method lookups.
197
+ #
141
198
  def method_missing(name, input = input_omitted = true, *, **arguments)
142
199
  if input_omitted
143
- Callable.new(name, client: self, arguments: arguments)
200
+ ::Proc::Callable.new(name, client: self, arguments: arguments)
144
201
  else
145
- Callable.new(name, client: self, input: input, arguments: arguments)
202
+ ::Proc::Callable.new(name, client: self, input: input, arguments: arguments)
146
203
  end
147
204
  end
148
205
 
@@ -150,37 +207,58 @@ class Proc
150
207
  true
151
208
  end
152
209
 
210
+ # [public] Builds a named argument with options.
211
+ #
153
212
  def argument(name, **options)
154
- Argument.new(name, **options)
213
+ ::Proc::Argument.new(name, **options)
155
214
  end
156
215
  alias_method :arg, :argument
157
216
 
158
- def close
159
- @internal.close
160
- end
161
-
162
217
  private def build_uri(proc)
163
- host_and_path = File.join(@host, proc.to_s.split(".").join("/"))
164
-
165
- "#{@scheme}://#{host_and_path}"
218
+ ::File.join(@__base_url, proc.to_s.split(".").join("/"))
166
219
  end
167
220
 
168
221
  private def serialize_value(value)
169
- if value.respond_to?(:serialize)
222
+ case value
223
+ when ::Symbol
224
+ ["@@", value.to_s, {}]
225
+ when ::Proc::Argument, ::Proc::Callable, ::Proc::Composition
170
226
  value.serialize
171
227
  else
172
228
  ["%%", value]
173
229
  end
174
230
  end
175
231
 
176
- private def get_payload(proc:, headers:, body:)
177
- @internal.call(:post, build_uri(proc), headers: headers, body: MessagePack.pack(body)) do |response|
178
- @remaining = response.headers["x-rate-limit-remaining"].to_s.to_i
179
- @resets_at = Time.at(response.headers["x-rate-limit-reset"].to_s.to_i)
180
- [response.status, response.headers, MessagePack.unpack(response.read)]
181
- end
232
+ private def get_payload(proc:, body:)
233
+ await {
234
+ @request_count += 1
235
+
236
+ response = ::HTTP.headers(@__headers).post(build_uri(proc), body: ::MessagePack.pack(body))
237
+
238
+ update_rate_limit(response)
239
+ @response = response
240
+
241
+ [response.status, response.headers, ::MessagePack.unpack(response.to_s)]
242
+ }
182
243
  rescue
183
- raise Proc::Unavailable
244
+ ::Kernel.raise ::Proc::Unavailable
245
+ end
246
+
247
+ private def update_rate_limit(response)
248
+ split_limit = response.headers["x-rate-limit"].to_s.split(";window=")
249
+
250
+ @rate_limit = split_limit[0].to_i
251
+
252
+ @rate_limit_window = case split_limit[1]
253
+ when "60"
254
+ :minute
255
+ when "1"
256
+ :second
257
+ end
258
+
259
+ @rate_limit_reset = if (reset = response.headers["x-rate-limit-reset"])
260
+ ::Time.at(reset.to_s.to_i)
261
+ end
184
262
  end
185
263
 
186
264
  private def extract_output(payload)
@@ -15,7 +15,13 @@ class Proc
15
15
  @callables = @callables.dup
16
16
  end
17
17
 
18
- def call(input = input_omitted = true, **arguments, &block)
18
+ # [public] Dispatches this composition to proc using the client.
19
+ #
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,
@@ -23,10 +29,29 @@ class Proc
23
29
  arguments: @arguments.merge(arguments)
24
30
  )
25
31
 
26
- @client.call("exec", Proc.undefined, proc: callable, &block)
32
+ @client.call("core.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("core.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,10 @@ class Proc
78
113
  end
79
114
 
80
115
  private def serialize_value(value)
81
- if value.respond_to?(:serialize)
116
+ case value
117
+ when Symbol
118
+ ["@@", value.to_s, {}]
119
+ when Argument, Callable, Composition
82
120
  value.serialize
83
121
  else
84
122
  ["%%", value]
@@ -4,18 +4,21 @@ class Proc
4
4
  class Enumerator
5
5
  include Enumerable
6
6
 
7
+ attr_reader :values, :next_block
8
+
7
9
  def initialize(values, &next_block)
8
10
  @values = values
9
11
  @next_block = next_block
10
12
  end
11
13
 
12
- def each(&block)
14
+ # [public] Calls the given block once for each value.
15
+ #
16
+ def each(enumerable = self, &block)
13
17
  return to_enum(:each) unless block
14
18
 
15
- @values.each(&block)
16
-
17
- if @next_block
18
- @next_block.call.each(&block)
19
+ while enumerable
20
+ enumerable.values.each(&block)
21
+ enumerable = enumerable.next_block&.call
19
22
  end
20
23
  end
21
24
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bigdecimal"
4
+
3
5
  class BigDecimal
4
6
  def to_msgpack(packer)
5
7
  if precision > 16
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.9.0"
4
+ VERSION = "0.12.0"
5
5
 
6
+ # [public]
7
+ #
6
8
  def self.version
7
9
  VERSION
8
10
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.12.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-03-16 00:00:00.000000000 Z
11
+ date: 2021-05-06 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
42
  name: msgpack
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,14 +66,11 @@ files:
66
66
  - lib/proc/client.rb
67
67
  - lib/proc/composition.rb
68
68
  - lib/proc/enumerator.rb
69
- - lib/proc/http/client.rb
70
- - lib/proc/http/request.rb
71
- - lib/proc/http/response.rb
72
69
  - lib/proc/msgpack/types/decimal.rb
73
70
  - lib/proc/version.rb
74
- homepage: https://proc.dev/
71
+ homepage: https://proc.dev
75
72
  licenses:
76
- - MIT
73
+ - MPL-2.0
77
74
  metadata: {}
78
75
  post_install_message:
79
76
  rdoc_options: []
@@ -83,14 +80,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
80
  requirements:
84
81
  - - ">="
85
82
  - !ruby/object:Gem::Version
86
- version: 2.5.0
83
+ version: 3.0.0
87
84
  required_rubygems_version: !ruby/object:Gem::Requirement
88
85
  requirements:
89
86
  - - ">="
90
87
  - !ruby/object:Gem::Version
91
88
  version: '0'
92
89
  requirements: []
93
- rubygems_version: 3.2.4
90
+ rubygems_version: 3.2.15
94
91
  signing_key:
95
92
  specification_version: 4
96
93
  summary: Proc client library.
@@ -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