Class: Proc::Client

Inherits:
Http::Client
  • Object
show all
Defined in:
lib/proc/client.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  "accept" => "application/json",
  "content-type" => "application/json"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(authorization, scheme: "https", host: "proc.dev") ⇒ Client

Returns a new instance of Client.



35
36
37
38
39
40
41
# File 'lib/proc/client.rb', line 35

def initialize(authorization, scheme: "https", host: "proc.dev")
  @authorization = authorization
  @scheme = scheme
  @host = host

  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, input = nil, **arguments) ⇒ Object



115
116
117
# File 'lib/proc/client.rb', line 115

def method_missing(name, input = nil, **arguments)
  Callable.new(name, client: self, input: input, arguments: arguments)
end

Instance Method Details

#[](proc) ⇒ Object



43
44
45
# File 'lib/proc/client.rb', line 43

def [](proc)
  Callable.new(proc, client: self)
end

#call(proc = nil, input = nil, **arguments) ⇒ Object



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
107
108
109
110
111
112
113
# File 'lib/proc/client.rb', line 69

def call(proc = nil, input = nil, **arguments)
  Async(logger: NullLogger) { |task|
    body = { "<<" => serialize_value(input) }

    arguments.each_pair do |key, value|
      body[key.to_s] = serialize_value(value)
    end

    headers = {
      "authorization" => "bearer #{@authorization}"
    }.merge(DEFAULT_HEADERS)

    begin
      response = super(:post, build_uri(proc), headers: headers, body: Oj.dump(body, mode: :custom), 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: :compat)
    rescue => error
      raise Proc::Unavailable, error.message
    ensure
      response&.close
    end

    case response.status
    when 200
      payload[">>"]
    when 400
      raise Proc::ArgumentError, payload.dig("error", "message")
    when 403
      raise Proc::Unauthorized, payload.dig("error", "message")
    when 404
      raise Proc::Undefined, payload.dig("error", "message")
    when 408
      raise Proc::Timeout, payload.dig("error", "message")
    when 429
      raise Proc::RateLimited, payload.dig("error", "message")
    when 500
      raise Proc::Error, payload.dig("error", "message")
    else
      raise Proc::Error, "unhandled"
    end
  }.wait
end

#remainingObject



47
48
49
50
51
52
53
# File 'lib/proc/client.rb', line 47

def remaining
  unless defined?(@remaining)
    self["ping"].call
  end

  @remaining
end

#resets_atObject



55
56
57
58
59
60
61
# File 'lib/proc/client.rb', line 55

def resets_at
  unless defined?(@resets_at)
    self["ping"].call
  end

  @resets_at
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/proc/client.rb', line 119

def respond_to_missing?(name, *)
  true
end