Class: Proc::Client
- Inherits:
- BasicObject
- Includes:
- Is::Async
- 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
Instance Attribute Summary collapse
-
#authorization ⇒ Object
readonly
- public
-
The configured authorization.
-
#host ⇒ Object
readonly
- public
-
The configured host.
-
#request_count ⇒ Object
readonly
- public
-
The number of requests this client has performed.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#scheme ⇒ Object
readonly
- public
-
The configured scheme.
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.undefined, **arguments, &block) ⇒ Object
- public
-
Calls a proc with the given input and arguments.
-
#initialize(authorization, scheme: "https", host: "proc.dev") ⇒ Client
constructor
A new instance of Client.
-
#method_missing(name, input = input_omitted = true, **arguments) ⇒ 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
Constructor Details
#initialize(authorization, scheme: "https", host: "proc.dev") ⇒ Client
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/proc/client.rb', line 85 def initialize(, scheme: "https", host: "proc.dev") = @scheme = scheme @host = host @request_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, **arguments) ⇒ Object
- public
-
Allows callable contexts to be built through method lookups.
198 199 200 201 202 203 204 |
# File 'lib/proc/client.rb', line 198 def method_missing(name, input = input_omitted = true, *, **arguments) if 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.
64 65 66 |
# File 'lib/proc/client.rb', line 64 def end |
#host ⇒ Object (readonly)
- public
-
The configured host.
72 73 74 |
# File 'lib/proc/client.rb', line 72 def host @host end |
#request_count ⇒ Object (readonly)
- public
-
The number of requests this client has performed.
76 77 78 |
# File 'lib/proc/client.rb', line 76 def request_count @request_count end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
78 79 80 |
# File 'lib/proc/client.rb', line 78 def response @response end |
#scheme ⇒ Object (readonly)
- public
-
The configured scheme.
68 69 70 |
# File 'lib/proc/client.rb', line 68 def scheme @scheme end |
Instance Method Details
#[](proc) ⇒ Object
- public
-
Returns a callable context for
proc.
99 100 101 102 103 104 105 |
# File 'lib/proc/client.rb', line 99 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.
212 213 214 |
# File 'lib/proc/client.rb', line 212 def argument(name, **) ::Proc::Argument.new(name, **) end |
#call(proc = nil, input = ::Proc.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.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/proc/client.rb', line 138 def call(proc = nil, input = ::Proc.undefined, **arguments, &block) body = [] unless ::Proc.undefined?(input) body << [">>", serialize_value(input)] end arguments.each_pair do |key, value| body << ["$$", key.to_s, serialize_value(value)] end status, headers, payload = get_payload(proc: proc, body: body) case status when 200 result = extract_output(payload) if (cursor = headers["x-cursor"]) enumerator = if cursor.empty? ::Proc::Enumerator.new(result) else ::Proc::Enumerator.new(result) { arguments[:cursor] = cursor.to_s call(proc, input, **arguments) } end if block enumerator.each(&block) else enumerator end else result end when 400 ::Kernel.raise ::Proc::Invalid, (payload) when 401 ::Kernel.raise ::Proc::, (payload) when 403 ::Kernel.raise ::Proc::Forbidden, (payload) when 404 ::Kernel.raise ::Proc::Undefined, (payload) when 408 ::Kernel.raise ::Proc::Timeout, (payload) when 413 ::Kernel.raise ::Proc::Invalid, (payload) when 429 ::Kernel.raise ::Proc::Limited, (payload) when 500 ::Kernel.raise ::Proc::Error, (payload) when 508 ::Kernel.raise ::Proc::Error, (payload) else ::Kernel.raise ::Proc::Error, "unhandled" end end |
#rate_limit ⇒ Object
- public
-
Returns the current rate limit.
109 110 111 112 |
# File 'lib/proc/client.rb', line 109 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.
123 124 125 126 |
# File 'lib/proc/client.rb', line 123 def rate_limit_reset refresh_rate_limit @rate_limit_reset end |
#rate_limit_window ⇒ Object
- public
-
Returns the current rate limit window.
116 117 118 119 |
# File 'lib/proc/client.rb', line 116 def rate_limit_window refresh_rate_limit @rate_limit_window end |
#respond_to_missing?(name) ⇒ Boolean
206 207 208 |
# File 'lib/proc/client.rb', line 206 def respond_to_missing?(name, *) true end |