Class: FaaStRuby::RPC::Function
- Inherits:
-
Object
- Object
- FaaStRuby::RPC::Function
- Defined in:
- lib/faastruby-rpc/function.rb
Constant Summary collapse
- @@region =
ENV['FAASTRUBY_REGION']
- @@endpoint_base_host =
ENV['FAASTRUBY_WORKSPACE_BASE_HOST']
- @@endpoint_base_protocol =
ENV['FAASTRUBY_URL_PROTOCOL'] || 'https'
Class Method Summary collapse
Instance Method Summary collapse
-
#body ⇒ Object
alias_method :read, :to_s.
- #call(*args) ⇒ Object
- #code ⇒ Object
- #execute(req_body: nil, query_params: {}, headers: {}, method: 'post') ⇒ Object
- #get_endpoint(query_params) ⇒ Object
- #headers ⇒ Object
-
#initialize(path, raise_errors: true) ⇒ Function
constructor
A new instance of Function.
- #inspect ⇒ Object
- #klass ⇒ Object
- #method_missing(m, *args, &block) ⇒ Object
- #response ⇒ Object
- #returned? ⇒ Boolean
- #to_s ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(path, raise_errors: true) ⇒ Function
Returns a new instance of Function.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/faastruby-rpc/function.rb', line 24 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
94 95 96 97 |
# File 'lib/faastruby-rpc/function.rb', line 94 def method_missing(m, *args, &block) rsp = response.body rsp.send(m.to_sym, *args, &block) end |
Class Method Details
.workspace=(workspace) ⇒ Object
21 22 23 |
# File 'lib/faastruby-rpc/function.rb', line 21 def self.workspace=(workspace) @@workspace = workspace end |
Instance Method Details
#body ⇒ Object
alias_method :read, :to_s
114 115 116 117 |
# File 'lib/faastruby-rpc/function.rb', line 114 def body # wait unless returned? response.body end |
#call(*args) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/faastruby-rpc/function.rb', line 37 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 |
#code ⇒ Object
119 120 121 122 |
# File 'lib/faastruby-rpc/function.rb', line 119 def code # wait unless returned? response.code end |
#execute(req_body: nil, query_params: {}, headers: {}, method: 'post') ⇒ Object
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 84 |
# File 'lib/faastruby-rpc/function.rb', line 50 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., '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
45 46 47 48 |
# File 'lib/faastruby-rpc/function.rb', line 45 def get_endpoint(query_params) return "#{@@endpoint_base_protocol}://#{@@workspace}.#{@@region}.#{@@endpoint_base_host}/#{@path}#{query_params}" if @@endpoint_base_host && @@region && @@workspace return "http://localhost:3000/#{@path}#{query_params}" end |
#headers ⇒ Object
124 125 126 127 |
# File 'lib/faastruby-rpc/function.rb', line 124 def headers # wait unless returned? response.headers end |
#inspect ⇒ Object
108 109 110 |
# File 'lib/faastruby-rpc/function.rb', line 108 def inspect body.inspect || "" end |
#klass ⇒ Object
129 130 131 132 |
# File 'lib/faastruby-rpc/function.rb', line 129 def klass # wait unless returned? response.klass end |
#response ⇒ Object
99 100 101 102 |
# File 'lib/faastruby-rpc/function.rb', line 99 def response wait unless returned? @response end |
#returned? ⇒ Boolean
86 87 88 |
# File 'lib/faastruby-rpc/function.rb', line 86 def returned? !@response.nil? end |
#to_s ⇒ Object
104 105 106 |
# File 'lib/faastruby-rpc/function.rb', line 104 def to_s body.to_s || "" end |
#value ⇒ Object
90 91 92 |
# File 'lib/faastruby-rpc/function.rb', line 90 def value body end |