Class: Sinatra::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/herbert/Jsonify.rb

Instance Method Summary collapse

Instance Method Details

#body(rack = false) ⇒ Object

Overrides Rack::Request.body, returns native #Hash Preserves access to underlying @env #IO.String



43
44
45
46
47
48
49
50
# File 'lib/herbert/Jsonify.rb', line 43

def body(rack = false)
  if rack then
					super()
  else
    ensure_encoded
					@body_decoded
  end
end

#body_rawObject

Encapsulates Rack::Request.body in order to remove #IO.String and therefore to enable repeated reads



24
25
26
27
# File 'lib/herbert/Jsonify.rb', line 24

def body_raw
  @body_raw ||= body(true).read
  @body_raw
end

#ensure_encoded(strict = true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/herbert/Jsonify.rb', line 29

def ensure_encoded(strict = true)
  if !@is_json then
    begin
      @body_decoded ||= ActiveSupport::JSON.decode(body_raw)
      @is_json = true;
    rescue StandardError
      @is_json = false;
      raise ::Herbert::Error::ApplicationError.new(1000) if strict
    end
  end
end

#json?Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/herbert/Jsonify.rb', line 52

def json?
  ensure_encoded(false)
  @is_json
end