Class: RailsErrorDashboard::Services::CurlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/services/curl_generator.rb

Overview

Pure algorithm: Assemble a curl command from an error log’s request data

Operates on data already stored in ErrorLog — zero runtime cost. Called at display time only.

Examples:

RailsErrorDashboard::Services::CurlGenerator.call(error)
# => "curl -X POST 'https://example.com/users' -H 'Content-Type: application/json' -d '{\"name\":\"test\"}'"

Constant Summary collapse

BODY_METHODS =
%w[ POST PUT PATCH ].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ CurlGenerator

Returns a new instance of CurlGenerator.



24
25
26
# File 'lib/rails_error_dashboard/services/curl_generator.rb', line 24

def initialize(error)
  @error = error
end

Class Method Details

.call(error) ⇒ String

Returns curl command string, or “” if insufficient data.

Parameters:

  • error (ErrorLog)

    An error log record

Returns:

  • (String)

    curl command string, or “” if insufficient data



18
19
20
21
22
# File 'lib/rails_error_dashboard/services/curl_generator.rb', line 18

def self.call(error)
  new(error).generate
rescue => e
  ""
end

Instance Method Details

#generateString

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails_error_dashboard/services/curl_generator.rb', line 29

def generate
  url = build_url
  return "" if url.blank?

  parts = [ "curl" ]

  method = @error.respond_to?(:http_method) && @error.http_method.presence
  parts << "-X #{method}" if method && method != "GET"

  parts << shell_quote(url)

  content_type = @error.respond_to?(:content_type) && @error.content_type.presence
  parts << "-H #{shell_quote("Content-Type: #{content_type}")}" if content_type

  user_agent = @error.respond_to?(:user_agent) && @error.user_agent.presence
  parts << "-H #{shell_quote("User-Agent: #{user_agent}")}" if user_agent

  if method && BODY_METHODS.include?(method.to_s.upcase)
    body = @error.respond_to?(:request_params) && @error.request_params.presence
    parts << "-d #{shell_quote(body)}" if body
  end

  parts.join(" ")
rescue => e
  ""
end