Class: FaaStRuby::RPC::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/faastruby-rpc/function.rb

Constant Summary collapse

@@region =
ENV['REGION']
@@endpoint_base_host =
ENV['ENDPOINT_BASE_HOST']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, raise_errors: true) ⇒ Function

Returns a new instance of Function.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/faastruby-rpc/function.rb', line 23

def initialize(path, raise_errors: true)
  @response = nil
  @path = path
  @methods = {
    'post' => Net::HTTP::Post,
    'get' => Net::HTTP::Get,
    'put' => Net::HTTP::Put,
    'patch' => Net::HTTP::Patch,
    'delete' => Net::HTTP::Delete
  }
  @raise_errors = raise_errors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



93
94
95
96
# File 'lib/faastruby-rpc/function.rb', line 93

def method_missing(m, *args, &block)
  rsp = response.body
  rsp.send(m.to_sym, *args, &block)
end

Class Method Details

.workspace=(workspace) ⇒ Object



20
21
22
# File 'lib/faastruby-rpc/function.rb', line 20

def self.workspace=(workspace)
  @@workspace = workspace
end

Instance Method Details

#bodyObject

alias_method :read, :to_s



113
114
115
116
# File 'lib/faastruby-rpc/function.rb', line 113

def body
  # wait unless returned?
  response.body
end

#call(*args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/faastruby-rpc/function.rb', line 36

def call(*args)
  @thread = Thread.new do
    output = args.any? ? call_with(*args) : execute(method: 'get')
    @response.body = yield(output) if block_given?
  end
  self
end

#codeObject



118
119
120
121
# File 'lib/faastruby-rpc/function.rb', line 118

def code
  # wait unless returned?
  response.code
end

#execute(req_body: nil, query_params: {}, headers: {}, method: 'post') ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/faastruby-rpc/function.rb', line 49

def execute(req_body: nil, query_params: {}, headers: {}, method: 'post')
  url = get_endpoint(convert_query_params(query_params))
  uri = URI.parse(url)
  use_ssl = uri.scheme == 'https' ? true : false
  function_response = fetch(use_ssl: use_ssl, uri: uri, headers: headers, method: @methods[method], req_body: req_body)
  resp_headers = {}
  function_response.each{|k,v| resp_headers[k] = v}
  case resp_headers['content-type']
  when 'application/json'
    begin
      resp_body = Oj.load(function_response.body)
    rescue Oj::ParseError => e
      if function_response.body.is_a?(String)
        resp_body = function_response.body
      else
        raise e if @raise_errors
        resp_body = {
          'error' => e.message,
          'location' => e.backtrace&.first
        }
      end
    end
  when 'application/yaml'
    resp_body = YAML.load(function_response.body)
  else
    resp_body = function_response.body
  end
  if function_response.code.to_i >= 400 && @raise_errors
    location = resp_body['location'] ? " @ #{resp_body['location']}" : nil
    error_msg = "#{resp_body['error']}#{location}"
    raise FaaStRuby::RPC::ExecutionError.new("Function '#{@path}' returned status code #{function_response.code}: #{error_msg}")
  end
  @response = FaaStRuby::RPC::Response.new(resp_body, function_response.code.to_i, resp_headers)
  self
end

#get_endpoint(query_params) ⇒ Object



44
45
46
47
# File 'lib/faastruby-rpc/function.rb', line 44

def get_endpoint(query_params)
  return "https://#{@@workspace}.#{@@region}.#{@@endpoint_base_host}/#{@path}#{query_params}" if @@endpoint_base_host && @@region && @@workspace
  return "http://localhost:3000/#{@path}#{query_params}"
end

#headersObject



123
124
125
126
# File 'lib/faastruby-rpc/function.rb', line 123

def headers
  # wait unless returned?
  response.headers
end

#inspectObject



107
108
109
# File 'lib/faastruby-rpc/function.rb', line 107

def inspect
  body.inspect || ""
end

#klassObject



128
129
130
131
# File 'lib/faastruby-rpc/function.rb', line 128

def klass
  # wait unless returned?
  response.klass
end

#responseObject



98
99
100
101
# File 'lib/faastruby-rpc/function.rb', line 98

def response
  wait unless returned?
  @response
end

#returned?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/faastruby-rpc/function.rb', line 85

def returned?
  !@response.nil?
end

#to_sObject



103
104
105
# File 'lib/faastruby-rpc/function.rb', line 103

def to_s
  body.to_s || ""
end

#valueObject



89
90
91
# File 'lib/faastruby-rpc/function.rb', line 89

def value
  body
end