Class: Cutlass::FunctionQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/cutlass/function_query.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port:, spec_version: nil, body: {}, io: Kernel) ⇒ FunctionQuery

Returns a new instance of FunctionQuery.



21
22
23
24
25
26
27
# File 'lib/cutlass/function_query.rb', line 21

def initialize(port:, spec_version: nil, body: {}, io: Kernel)
  @send_body = body
  @port = port
  @response = nil
  @spec_version = spec_version || "1.0"
  @io = io
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



19
20
21
# File 'lib/cutlass/function_query.rb', line 19

def io
  @io
end

Instance Method Details

#as_jsonObject



55
56
57
58
59
60
61
62
63
# File 'lib/cutlass/function_query.rb', line 55

def as_json
  JSON.parse(body || "")
rescue JSON::ParserError => e
  io.warn "Body: #{body}"
  io.warn "Code: #{response&.status}"
  io.warn "Headers: #{response&.headers.inspect}"
  io.warn "x-extra-info: #{CGI.unescape(response&.headers&.[]("x-extra-info") || "")}"
  raise e
end

#bodyObject



65
66
67
# File 'lib/cutlass/function_query.rb', line 65

def body
  response&.body
end

#callObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cutlass/function_query.rb', line 29

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?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/cutlass/function_query.rb', line 51

def fail?
  !success?
end

#headersObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cutlass/function_query.rb', line 69

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) ⇒ Object



119
120
121
# File 'lib/cutlass/function_query.rb', line 119

def marshal_hash(value)
  Base64.strict_encode64(JSON.dump(value)).chomp
end

#raw_sfcontextObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cutlass/function_query.rb', line 99

def raw_sfcontext
  {
    "apiVersion" => "",
    "payloadVersion" => "",
    "userContext" =>
     {
       "orgId" => "",
       "userId" => "",
       "username" => "",
       "orgDomainUrl" => "",
       "onBehalfOfUserId" => nil,
       "salesforceBaseUrl" => ""
     }
  }
end

#raw_ssfcontextObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cutlass/function_query.rb', line 87

def raw_ssfcontext
  {
    "resource" => "",
    "requestId" => "",
    "accessToken" => "",
    "apexClassId" => nil,
    "apexClassFQN" => nil,
    "functionName" => "",
    "functionInvocationId" => nil
  }
end

#responseObject



42
43
44
45
# File 'lib/cutlass/function_query.rb', line 42

def response
  raise "Must `call` first" if @response.nil?
  @response
end

#sfcontextObject



115
116
117
# File 'lib/cutlass/function_query.rb', line 115

def sfcontext
  marshal_hash(raw_sfcontext)
end

#ssfcontextObject



83
84
85
# File 'lib/cutlass/function_query.rb', line 83

def ssfcontext
  marshal_hash(raw_ssfcontext)
end

#success?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/cutlass/function_query.rb', line 47

def success?
  response&.status.to_s.start_with?("2")
end