Class: Proc::Client
- Inherits:
- BasicObject
- Includes:
- Is::Async, Is::Global, Is::Inspectable
- Defined in:
- lib/proc/client.rb
Overview
- public
-
Connection to proc, configured with an authorization.
Constant Summary collapse
- DEFAULT_HEADERS =
{ "accept" => "application/vnd.proc+msgpack", "content-type" => "application/vnd.proc+msgpack" }.freeze
- IGNORE_MISSING =
%i[ ].freeze
- KERNEL_DELEGATE =
%i[ class instance_variables instance_variable_get instance_variable_set object_id public_send respond_to? ].freeze
Instance Attribute Summary collapse
-
#authorization ⇒ Object
readonly
- public
-
The configured authorization.
-
#count ⇒ Object
readonly
- public
-
The number of requests this client has performed.
-
#host ⇒ Object
readonly
- public
-
The configured host.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#scheme ⇒ Object
readonly
- public
-
The configured scheme.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](proc) ⇒ Object
- public
-
Returns a callable context for ‘proc`.
-
#argument(name, **options) ⇒ Object
(also: #arg)
- public
-
Builds a named argument with options.
-
#call(proc = nil, input = ::Proc::Composer.undefined, **arguments, &block) ⇒ Object
- public
-
Calls a proc with the given input and arguments.
-
#initialize(authorization = ::Proc::Client.authorization, scheme: "https", host: "proc.run") ⇒ Client
constructor
A new instance of Client.
-
#method_missing(name, input = input_omitted = true, *parameters, **arguments, &block) ⇒ Object
- public
-
Allows callable contexts to be built through method lookups.
-
#rate_limit ⇒ Object
- public
-
Returns the current rate limit.
-
#rate_limit_reset ⇒ Object
- public
-
Returns the time at which the current rate limit will reset.
-
#rate_limit_window ⇒ Object
- public
-
Returns the current rate limit window.
- #respond_to_missing?(name) ⇒ Boolean
-
#safe_authorization ⇒ Object
- public
-
Returns a partial representation of the authorization that is safe to include in logs.
Constructor Details
#initialize(authorization = ::Proc::Client.authorization, scheme: "https", host: "proc.run") ⇒ Client
Returns a new instance of Client.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/proc/client.rb', line 106 def initialize( = ::Proc::Client., scheme: "https", host: "proc.run") @authorization = @scheme = scheme @host = host @count = 0 @__base_url = "#{@scheme}://#{host}" @__headers = { "authorization" => "bearer #{@authorization}" }.merge(DEFAULT_HEADERS) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, input = input_omitted = true, *parameters, **arguments, &block) ⇒ Object
- public
-
Allows callable contexts to be built through method lookups.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/proc/client.rb', line 238 def method_missing(name, input = input_omitted = true, *parameters, **arguments, &block) if IGNORE_MISSING.include?(name) super elsif KERNEL_DELEGATE.include?(name) if input_omitted ::Kernel.instance_method(name).bind_call(self, *parameters, **arguments, &block) else ::Kernel.instance_method(name).bind_call(self, input, *parameters, **arguments, &block) end elsif input_omitted ::Proc::Callable.new(name, client: self, arguments: arguments) else ::Proc::Callable.new(name, client: self, input: input, arguments: arguments) end end |
Instance Attribute Details
#authorization ⇒ Object (readonly)
- public
-
The configured authorization.
85 86 87 |
# File 'lib/proc/client.rb', line 85 def @authorization end |
#count ⇒ Object (readonly)
- public
-
The number of requests this client has performed.
97 98 99 |
# File 'lib/proc/client.rb', line 97 def count @count end |
#host ⇒ Object (readonly)
- public
-
The configured host.
93 94 95 |
# File 'lib/proc/client.rb', line 93 def host @host end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
99 100 101 |
# File 'lib/proc/client.rb', line 99 def response @response end |
#scheme ⇒ Object (readonly)
- public
-
The configured scheme.
89 90 91 |
# File 'lib/proc/client.rb', line 89 def scheme @scheme end |
Class Method Details
.authorization ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/proc/client.rb', line 65 def ::ENV.fetch("PROC_AUTH") { auth_file_path = ::Pathname.new("~/.proc/auth"). if auth_file_path.exist? auth_file_path.read else "" end }.strip end |
Instance Method Details
#[](proc) ⇒ Object
- public
-
Returns a callable context for ‘proc`.
120 121 122 123 124 125 126 |
# File 'lib/proc/client.rb', line 120 def [](proc) if ::Kernel.block_given? ::Proc::Callable.new(proc, client: self, arguments: {proc: yield}) else ::Proc::Callable.new(proc, client: self) end end |
#argument(name, **options) ⇒ Object Also known as: arg
- public
-
Builds a named argument with options.
260 261 262 |
# File 'lib/proc/client.rb', line 260 def argument(name, **) ::Proc::Composer::Argument.new(name, **) end |
#call(proc = nil, input = ::Proc::Composer.undefined, **arguments, &block) ⇒ Object
- public
-
Calls a proc with the given input and arguments.
If a block is passed and the proc returns an enumerable, the block will be called with each value.
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/proc/client.rb', line 159 def call(proc = nil, input = ::Proc::Composer.undefined, **arguments, &block) body = [] unless ::Proc::Composer.undefined?(input) body << [">>", serialize_value(input)] end arguments.each_pair do |key, value| body << ["$$", key.to_s, serialize_value(value)] end process(proc: proc, body: body, input: input, arguments: arguments, &block) end |
#rate_limit ⇒ Object
- public
-
Returns the current rate limit.
130 131 132 133 |
# File 'lib/proc/client.rb', line 130 def rate_limit refresh_rate_limit @rate_limit end |
#rate_limit_reset ⇒ Object
- public
-
Returns the time at which the current rate limit will reset.
144 145 146 147 |
# File 'lib/proc/client.rb', line 144 def rate_limit_reset refresh_rate_limit @rate_limit_reset end |
#rate_limit_window ⇒ Object
- public
-
Returns the current rate limit window.
137 138 139 140 |
# File 'lib/proc/client.rb', line 137 def rate_limit_window refresh_rate_limit @rate_limit_window end |
#respond_to_missing?(name) ⇒ Boolean
254 255 256 |
# File 'lib/proc/client.rb', line 254 def respond_to_missing?(name, *) true end |
#safe_authorization ⇒ Object
- public
-
Returns a partial representation of the authorization that is safe to include in logs.
267 268 269 270 |
# File 'lib/proc/client.rb', line 267 def return unless @authorization "#{@authorization[0..7]}...#{@authorization[-5..]}" end |