class Cutlass::FunctionQuery

The purpose of this class is to trigger “Salesforce Functions” against a function compatible app built with a compatible CNB

This class is WIP, API is subject to change

app.start_container(expose_ports: [8080]) do |container|
  Cutlass::FunctionQuery.new(port: container.get_host_port(8080)).call.as_json
  # => { accounts: []}
end

Public Class Methods

new(port:, spec_version: nil, body: {}) click to toggle source
# File lib/cutlass/function_query.rb, line 16
def initialize(port:, spec_version: nil, body: {})
  @send_body = body
  @port = port
  @response = nil
  @spec_version = spec_version || "1.0"
end

Public Instance Methods

as_json() click to toggle source
# File lib/cutlass/function_query.rb, line 49
def as_json
  JSON.parse(body || "")
end
body() click to toggle source
# File lib/cutlass/function_query.rb, line 53
def body
  response&.body
end
call() click to toggle source
# File lib/cutlass/function_query.rb, line 23
def call
  @response = Excon.post(
    "http://localhost:#{@port}",
    body: JSON.dump(@send_body),
    headers: headers,
    idempotent: true,
    retry_limit: 5,
    retry_interval: 1
  )

  self
end
fail?() click to toggle source
# File lib/cutlass/function_query.rb, line 45
def fail?
  !success?
end
headers() click to toggle source
# File lib/cutlass/function_query.rb, line 57
def headers
  {
    "Content-Type" => "application/json",
    "ce-id" => "MyFunction-#{SecureRandom.hex(10)}",
    "ce-time" => "2020-09-03T20:56:28.297915Z",
    "ce-type" => "",
    "ce-source" => "",
    "ce-sfcontext" => sfcontext,
    "Authorization" => "",
    "ce-specversion" => @spec_version,
    "ce-sffncontext" => ssfcontext
  }
end
marshal_hash(value) click to toggle source
# File lib/cutlass/function_query.rb, line 107
def marshal_hash(value)
  Base64.strict_encode64(JSON.dump(value)).chomp
end
raw_sfcontext() click to toggle source
# File lib/cutlass/function_query.rb, line 87
def raw_sfcontext
  {
    "apiVersion" => "",
    "payloadVersion" => "",
    "userContext" =>
     {
       "orgId" => "",
       "userId" => "",
       "username" => "",
       "orgDomainUrl" => "",
       "onBehalfOfUserId" => nil,
       "salesforceBaseUrl" => ""
     }
  }
end
raw_ssfcontext() click to toggle source
# File lib/cutlass/function_query.rb, line 75
def raw_ssfcontext
  {
    "resource" => "",
    "requestId" => "",
    "accessToken" => "",
    "apexClassId" => nil,
    "apexClassFQN" => nil,
    "functionName" => "",
    "functionInvocationId" => nil
  }
end
response() click to toggle source
# File lib/cutlass/function_query.rb, line 36
def response
  raise "Must `call` first" if @response.nil?
  @response
end
sfcontext() click to toggle source
# File lib/cutlass/function_query.rb, line 103
def sfcontext
  marshal_hash(raw_sfcontext)
end
ssfcontext() click to toggle source
# File lib/cutlass/function_query.rb, line 71
def ssfcontext
  marshal_hash(raw_ssfcontext)
end
success?() click to toggle source
# File lib/cutlass/function_query.rb, line 41
def success?
  response&.status.to_s.start_with?("2")
end