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



49
50
51
52
53
54
55
56
# File 'lib/herbert/Jsonify.rb', line 49

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



30
31
32
33
# File 'lib/herbert/Jsonify.rb', line 30

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

#ensure_encoded(strict = true) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/herbert/Jsonify.rb', line 35

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)


58
59
60
61
# File 'lib/herbert/Jsonify.rb', line 58

def json?
  ensure_encoded(false)
  @is_json
end