Class: TentClient::Middleware::ContentTypeHeader

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/tent-client/middleware/content_type_header.rb

Constant Summary collapse

CONTENT_TYPE_HEADER =
'Content-Type'.freeze
MEDIA_TYPE =
%(application/vnd.tent.post.v0+json; type="%s").freeze

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tent-client/middleware/content_type_header.rb', line 9

def call(env)
  if env[:body] && Hash === env[:body] && !env[:request_headers][CONTENT_TYPE_HEADER]
    env[:request_headers][CONTENT_TYPE_HEADER] = MEDIA_TYPE % (env[:body]['type'] || env[:body][:type])

    if env[:request]['tent.import']
      env[:request_headers][CONTENT_TYPE_HEADER] << %(; rel="https://tent.io/rels/import")
    elsif env[:request]['tent.notification']
      env[:request_headers][CONTENT_TYPE_HEADER] << %(; rel="https://tent.io/rels/notification")
    end
  end

  @app.call env
end