Class: FaaStRuby::RPC::Function

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

Instance Method Summary collapse

Constructor Details

#initialize(path, raise_errors: true) ⇒ Function

Returns a new instance of Function.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/faastruby-rpc/function.rb', line 19

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



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

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

Instance Method Details

#bodyObject

alias_method :read, :to_s



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

def body
  # wait unless returned?
  response.body
end

#call(*args) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/faastruby-rpc/function.rb', line 32

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



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

def code
  # wait unless returned?
  response.code
end

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



45
46
47
48
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
# File 'lib/faastruby-rpc/function.rb', line 45

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



40
41
42
43
# File 'lib/faastruby-rpc/function.rb', line 40

def get_endpoint(query_params)
  return "https://#{FAASTRUBY_WORKSPACE_BASE_HOST}/#{@path}#{query_params}" if FAASTRUBY_WORKSPACE_BASE_HOST
  return "http://localhost:3000/#{@path}#{query_params}"
end

#headersObject



111
112
113
114
# File 'lib/faastruby-rpc/function.rb', line 111

def headers
  # wait unless returned?
  response.headers
end

#klassObject



116
117
118
119
# File 'lib/faastruby-rpc/function.rb', line 116

def klass
  # wait unless returned?
  response.klass
end

#responseObject



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

def response
  wait unless returned?
  @response
end

#returned?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/faastruby-rpc/function.rb', line 81

def returned?
  !@response.nil?
end

#to_sObject



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

def to_s
  body.to_s || ""
end