Class: Jets::CLI::Curl::Adapter::Lambda

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/cli/curl/adapter/lambda.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Jets::CLI::Curl::Adapter::Base

Instance Method Details

#api_idObject



100
101
102
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 100

def api_id
  "dummy"
end

#aws_regionObject



104
105
106
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 104

def aws_region
  Jets.aws.region
end

#bodyObject



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

def body
  data = @options[:data]
  return unless data
  if data.starts_with?("@")
    file = data[1..]
    file = "#{Jets.root}/#{file}" unless file.starts_with?("/")
    IO.read(file) # IE: @data.json
  else
    data
  end
end

#convertObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 5

def convert
  {
    version: "2.0",
    routeKey: "$default",
    rawPath: path,
    rawQueryString: raw_query_string,
    cookies: cookies,
    body: body,
    headers: headers,
    requestContext: request_context,
    isBase64Encoded: false
  }.delete_if { |k, v| v.nil? }.to_json
end

#cookiesObject



19
20
21
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 19

def cookies
  Cookies::Parser.new(@options[:cookie]).parse if @options[:cookie]
end

#default_headersObject



39
40
41
42
43
44
45
46
47
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 39

def default_headers
  {
    host: host,
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "x-forwarded-for": "127.0.0.1",
    "user-agent": "jets curl (#{Jets::VERSION})"
  }
end

#default_hostObject



62
63
64
65
66
67
68
69
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 62

def default_host
  host = uri.host
  if host && (host.include?("amazonaws.com") || host.include?(".aws"))
    host
  else
    "#{api_id}.lambda-url.#{aws_region}.on.aws"
  end
end

#headersObject



35
36
37
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 35

def headers
  default_headers.merge(headers_option)
end

#headers_optionObject



49
50
51
52
53
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 49

def headers_option
  headers = @options[:headers] || {}
  headers["user-agent"] = user_agent if user_agent
  headers.transform_keys(&:downcase).transform_values(&:strip)
end

#hostObject



55
56
57
58
59
60
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 55

def host
  return default_host unless @options[:headers]
  deleted_host = @options[:headers].delete("Host")&.strip
  host = @options[:headers][:host] || deleted_host
  host || default_host
end

#http_methodObject



108
109
110
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 108

def http_method
  @options[:request] || "GET"
end

#parse_query_string(raw_query_string) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 126

def parse_query_string(raw_query_string)
  query_params = {}

  raw_query_string.split("&").each do |param|
    key, value = param.split("=")
    key = key.to_sym
    if query_params.key?(key)
      query_params[key] += ",#{value}"
    else
      query_params[key] = value
    end
  end

  query_params.transform_values! { |value| value.include?(",") ? value.split(",") : value }
end

#pathObject



116
117
118
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 116

def path
  uri.path
end

#raw_query_stringObject



120
121
122
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 120

def raw_query_string
  uri.query
end

#request_contextObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 78

def request_context
  now = Time.now.utc
  {
    accountId: "anonymous",
    apiId: api_id,
    domainName: host,
    domainPrefix: api_id,
    http: {
      method: http_method,
      path: path,
      protocol: "HTTP/1.1",
      sourceIp: "127.0.0.1",
      userAgent: user_agent
    },
    requestId: "3fca3afe-2fb7-4e93-ac3b-949519408c39",
    routeKey: "$default",
    stage: "$default",
    time: now.strftime("%d/%b/%Y:%H:%M:%S %z"),
    timeEpoch: now.to_i
  }
end

#uriObject



112
113
114
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 112

def uri
  URI.parse(@options[:path])
end

#user_agentObject



71
72
73
74
75
# File 'lib/jets/cli/curl/adapter/lambda.rb', line 71

def user_agent
  return unless @options[:headers]
  deleted_user_agent = @options[:headers].delete("User-Agent")&.strip
  @options[:headers]["user-agent"] || deleted_user_agent
end