Class: VcrStripeWebhook::Server::RequestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/vcr_stripe_webhook/server.rb

Constant Summary collapse

MAX_HEADER_LENGTH =
16 * 1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ RequestParser

Returns a new instance of RequestParser.



70
71
72
73
74
75
76
# File 'lib/vcr_stripe_webhook/server.rb', line 70

def initialize(socket)
  @method, @path = read_start_line(socket)
  @headers = read_headers(socket)
  return unless content_length

  @body = socket.read(content_length)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



68
69
70
# File 'lib/vcr_stripe_webhook/server.rb', line 68

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



68
69
70
# File 'lib/vcr_stripe_webhook/server.rb', line 68

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



68
69
70
# File 'lib/vcr_stripe_webhook/server.rb', line 68

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



68
69
70
# File 'lib/vcr_stripe_webhook/server.rb', line 68

def path
  @path
end

Instance Method Details

#content_lengthObject



78
79
80
# File 'lib/vcr_stripe_webhook/server.rb', line 78

def content_length
  @content_length ||= headers["content-length"]&.to_i
end

#content_typeObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/vcr_stripe_webhook/server.rb', line 82

def content_type
  return @content_type if defined?(@content_type)

  if headers["content-type"]
    @content_type, _attributes = headers["content-type"].split(/;\s+/, 2)
  else
    @content_type = nil
  end
  @content_type
end

#json?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/vcr_stripe_webhook/server.rb', line 93

def json?
  content_type == "application/json"
end

#json_valueObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vcr_stripe_webhook/server.rb', line 97

def json_value
  return @json_value if defined?(@json_value)

  @json_value =
    if json?
      begin
        JSON.parse(@body)
      rescue StandardError
        nil
      end
    end
end