Class: Esplanade::Request::Raw::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/esplanade/request/raw/body.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_request, env) ⇒ Body

Returns a new instance of Body.



5
6
7
8
# File 'lib/esplanade/request/raw/body.rb', line 5

def initialize(raw_request, env)
  @raw_request = raw_request
  @env = env
end

Instance Method Details

#reduced_versionObject



24
25
26
27
28
29
30
# File 'lib/esplanade/request/raw/body.rb', line 24

def reduced_version
  @reduced_version ||= if to_string && to_string.size >= 1000
                         "#{to_string[0..499]}...#{to_string[500..-1]}"
                       else
                         to_string
                       end
end

#to_hashObject



18
19
20
21
22
# File 'lib/esplanade/request/raw/body.rb', line 18

def to_hash
  @hash ||= JSON.parse(to_string.to_s)
rescue JSON::ParserError
  raise BodyIsNotJson.new(**message)
end

#to_stringObject



10
11
12
13
14
15
16
# File 'lib/esplanade/request/raw/body.rb', line 10

def to_string
  return @string if @string

  @string = @env['rack.input'].read
  @env['rack.input'].rewind
  @string
end