Class: Proc::Client

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



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

def initialize(authorization, scheme: "https", host: "proc.dev")
  @authorization = authorization
  @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



115
116
117
118
119
120
121
# File 'lib/proc/client.rb', line 115

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 Method Details

#[](proc) ⇒ Object



47
48
49
# File 'lib/proc/client.rb', line 47

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

#argument(name, **options) ⇒ Object Also known as: arg



127
128
129
# File 'lib/proc/client.rb', line 127

def argument(name, **options)
  Argument.new(name, **options)
end

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



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 72

def call(proc = nil, input = Proc.undefined, **arguments)
  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, payload = get_payload(proc: proc, headers: headers, body: body)

  case status
  when 200
    extract_output(payload)
  when 400
    raise Proc::Invalid, extract_error_message(payload)
  when 401
    raise Proc::Unauthorized, extract_error_message(payload)
  when 403
    raise Proc::Forbidden, extract_error_message(payload)
  when 404
    raise Proc::Undefined, extract_error_message(payload)
  when 408
    raise Proc::Timeout, extract_error_message(payload)
  when 413
    raise Proc::Invalid, extract_error_message(payload)
  when 429
    raise Proc::Limited, extract_error_message(payload)
  when 500
    raise Proc::Error, extract_error_message(payload)
  when 508
    raise Proc::Error, extract_error_message(payload)
  else
    raise Proc::Error, "unhandled"
  end
end

#closeObject



132
133
134
# File 'lib/proc/client.rb', line 132

def close
  @internal.close
end

#remainingObject



51
52
53
54
55
56
57
# File 'lib/proc/client.rb', line 51

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

  @remaining
end

#resets_atObject



59
60
61
62
63
64
65
# File 'lib/proc/client.rb', line 59

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

  @resets_at
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/proc/client.rb', line 123

def respond_to_missing?(name, *)
  true
end