Class: Jets::CLI::Curl::Request

Inherits:
Jets::CLI::Call show all
Defined in:
lib/jets/cli/curl/request.rb

Constant Summary collapse

TRIM_MAX =
ENV["JETS_CURL_TRIM_MAX"] || 64

Instance Attribute Summary

Attributes inherited from Jets::CLI::Call

#event

Instance Method Summary collapse

Methods inherited from Jets::CLI::Call

#check_valid_json!, #function_name, #initialize, #invocation_type, #invoke, #load_event_from_file, #log_last_4kb, #verbose?

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

This class inherits a constructor from Jets::CLI::Call

Instance Method Details

#adapterObject



56
57
58
59
# File 'lib/jets/cli/curl/request.rb', line 56

def adapter
  # Only support Lambda URL for now.
  Adapter::Lambda.new(@options)
end

#payloadObject

interface method: override to convert cli curl-like options to Call payload



38
39
40
# File 'lib/jets/cli/curl/request.rb', line 38

def payload
  adapter.convert
end

#runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jets/cli/curl/request.rb', line 7

def run
  warn "Calling Lambda function #{function_name}"
  show_body
  result = invoke

  if result[:cookies] && @options[:cookie_jar]
    cookie_jar = Adapter::Cookies::Jar.new(result, @options[:cookie_jar])
    cookie_jar.write_to_file
  end
  if @options[:trim] || ENV["JETS_CURL_TRIM"]
    trim!(result, TRIM_MAX)
  else
    result
  end
end

#show_bodyObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jets/cli/curl/request.rb', line 23

def show_body
  return unless @options[:verbose] && @options[:data]
  hash = JSON.parse(payload)
  body = hash["body"]
  text = begin
    JSON.pretty_generate(JSON.parse(body))
  rescue JSON::ParserError
    body
  end

  warn "Request Body:"
  warn text
end

#trim!(hash, max_length) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jets/cli/curl/request.rb', line 42

def trim!(hash, max_length)
  hash.transform_values! do |value|
    if value.is_a?(String) && value.length > max_length
      value.truncate(max_length)
    elsif value.is_a?(Hash)
      trim!(value, max_length)
    elsif value.is_a?(Array)
      value.map! { |v| (v.is_a?(String) && v.length > max_length) ? v.truncate(max_length) : v }
    else
      value
    end
  end
end