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

Instance Method Details

#bodyObject



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

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



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

def code
  # wait unless returned?
  response.code
end

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



40
41
42
43
44
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
# File 'lib/faastruby-rpc/function.rb', line 40

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

#headersObject



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

def headers
  # wait unless returned?
  response.headers
end

#klassObject



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

def klass
  # wait unless returned?
  response.klass
end

#responseObject



76
77
78
79
# File 'lib/faastruby-rpc/function.rb', line 76

def response
  wait unless returned?
  @response
end

#returned?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/faastruby-rpc/function.rb', line 72

def returned?
  !@response.nil?
end

#to_sObject



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

def to_s
  body.to_s || ""
end