Class: Tango::Request::JsonEncoded

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/tango/request/json_encoded.rb

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.mime_typeObject

Returns the value of attribute mime_type.



10
11
12
# File 'lib/tango/request/json_encoded.rb', line 10

def mime_type
  @mime_type
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
# File 'lib/tango/request/json_encoded.rb', line 14

def call(env)
  match_content_type(env) do |data|
    env[:body] = MultiJson.dump data
  end
  @app.call env
end

#match_content_type(env) ⇒ Object



21
22
23
24
25
26
# File 'lib/tango/request/json_encoded.rb', line 21

def match_content_type(env)
  if process_request?(env)
    env[:request_headers][CONTENT_TYPE] ||= self.class.mime_type
    yield env[:body] unless env[:body].respond_to?(:to_str)
  end
end

#process_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/tango/request/json_encoded.rb', line 28

def process_request?(env)
  type = request_type(env)
  env[:body] and (type.empty? or type == self.class.mime_type)
end

#request_type(env) ⇒ Object



33
34
35
36
37
# File 'lib/tango/request/json_encoded.rb', line 33

def request_type(env)
  type = env[:request_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end