Class: Proc::Client
- Inherits:
-
Http::Client
- Object
- Http::Client
- Proc::Client
- Defined in:
- lib/proc/client.rb
Constant Summary collapse
- DEFAULT_HEADERS =
{ "content-type" => "application/json" }.freeze
Instance Method Summary collapse
- #[](proc) ⇒ Object
- #compose(&block) ⇒ Object
-
#initialize(authorization, scheme: "https", host: "proc.dev") ⇒ Client
constructor
A new instance of Client.
- #perform(proc, input, **arguments) ⇒ Object
- #remaining ⇒ Object
- #resets_at ⇒ Object
Constructor Details
#initialize(authorization, scheme: "https", host: "proc.dev") ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 37 38 |
# File 'lib/proc/client.rb', line 32 def initialize(, scheme: "https", host: "proc.dev") = @scheme = scheme @host = host super() end |
Instance Method Details
#[](proc) ⇒ Object
40 41 42 |
# File 'lib/proc/client.rb', line 40 def [](proc) Callable.new(proc, client: self) end |
#compose(&block) ⇒ Object
60 61 62 |
# File 'lib/proc/client.rb', line 60 def compose(&block) Composition.new(client: self, &block) end |
#perform(proc, input, **arguments) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 |
# File 'lib/proc/client.rb', line 68 def perform(proc, input, **arguments) Async(logger: NullLogger) { |task| body = { input: input, arguments: arguments } headers = { "authorization" => "Bearer #{@authorization}" }.merge(DEFAULT_HEADERS) begin response = call(:post, build_uri(proc), headers: headers, body: Oj.dump(body, mode: :json), task: task) @remaining = response.headers["x-rate-limit-remaining"].to_s.to_i @resets_at = Time.at(response.headers["x-rate-limit-reset"].to_s.to_i) payload = Oj.load(response.read, mode: :strict) rescue => error raise Proc::Unavailable, error. ensure response&.close end case response.status when 200 payload["value"] when 400 raise Proc::ArgumentError, payload.dig("error", "message") when 403 raise Proc::, payload.dig("error", "message") when 404 raise Proc::Undefined, payload.dig("error", "message") when 429 raise Proc::RateLimited, payload.dig("error", "message") when 500 raise Proc::Error, payload.dig("error", "message") end }.wait end |
#remaining ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/proc/client.rb', line 44 def remaining unless defined?(@remaining) self["ping"].call end @remaining end |
#resets_at ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/proc/client.rb', line 52 def resets_at unless defined?(@resets_at) self["ping"].call end @resets_at end |