Class: Proc::Client
- Inherits:
-
Object
- Object
- Proc::Client
- Defined in:
- lib/proc/client.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ "accept" => "application/vnd.proc+json", "content-type" => "application/vnd.proc+json" }.freeze
Instance Attribute Summary collapse
-
#authorization ⇒ Object
readonly
Returns the value of attribute authorization.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Instance Method Summary collapse
- #[](proc) ⇒ Object
- #argument(name, **options) ⇒ Object (also: #arg)
- #call(proc = nil, input = Proc.undefined, **arguments, &block) ⇒ Object
- #close ⇒ Object
-
#initialize(authorization, scheme: "https", host: "proc.dev") ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, input = input_omitted = true, **arguments) ⇒ Object
- #remaining ⇒ Object
- #resets_at ⇒ Object
- #respond_to_missing?(name) ⇒ Boolean
Constructor Details
#initialize(authorization, scheme: "https", host: "proc.dev") ⇒ Client
Returns a new instance of Client.
50 51 52 53 54 55 |
# File 'lib/proc/client.rb', line 50 def initialize(, scheme: "https", host: "proc.dev") = @scheme = scheme @host = host @internal = Http::Client.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, input = input_omitted = true, **arguments) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/proc/client.rb', line 144 def method_missing(name, input = input_omitted = true, *, **arguments) if input_omitted Callable.new(name, client: self, arguments: arguments) else Callable.new(name, client: self, input: input, arguments: arguments) end end |
Instance Attribute Details
#authorization ⇒ Object (readonly)
Returns the value of attribute authorization.
48 49 50 |
# File 'lib/proc/client.rb', line 48 def end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
48 49 50 |
# File 'lib/proc/client.rb', line 48 def host @host end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
48 49 50 |
# File 'lib/proc/client.rb', line 48 def scheme @scheme end |
Instance Method Details
#[](proc) ⇒ Object
57 58 59 |
# File 'lib/proc/client.rb', line 57 def [](proc) Callable.new(proc, client: self) end |
#argument(name, **options) ⇒ Object Also known as: arg
156 157 158 |
# File 'lib/proc/client.rb', line 156 def argument(name, **) Argument.new(name, **) end |
#call(proc = nil, input = Proc.undefined, **arguments, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/proc/client.rb', line 82 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 headers = { "authorization" => "bearer #{@authorization}" }.merge(DEFAULT_HEADERS) status, headers, payload = get_payload(proc: proc, headers: headers, body: body) case status when 200 result = extract_output(payload) if (cursor = headers["x-cursor"]) enumerator = if cursor.empty? Enumerator.new(result) else 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 raise Proc::Invalid, (payload) when 401 raise Proc::, (payload) when 403 raise Proc::Forbidden, (payload) when 404 raise Proc::Undefined, (payload) when 408 raise Proc::Timeout, (payload) when 413 raise Proc::Invalid, (payload) when 429 raise Proc::Limited, (payload) when 500 raise Proc::Error, (payload) when 508 raise Proc::Error, (payload) else raise Proc::Error, "unhandled" end end |
#close ⇒ Object
161 162 163 |
# File 'lib/proc/client.rb', line 161 def close @internal.close end |
#remaining ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/proc/client.rb', line 61 def remaining unless defined?(@remaining) self["ping"].call end @remaining end |
#resets_at ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/proc/client.rb', line 69 def resets_at unless defined?(@resets_at) self["ping"].call end @resets_at end |
#respond_to_missing?(name) ⇒ Boolean
152 153 154 |
# File 'lib/proc/client.rb', line 152 def respond_to_missing?(name, *) true end |