Class: JumpCloud::Middleware::JSONRequest

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/jump_cloud/middleware.rb

Overview

This middleware adds a “Content-Type: application/json” HTTP header

Instance Method Summary collapse

Instance Method Details

#add_header(headers) ⇒ Object



44
45
46
# File 'lib/jump_cloud/middleware.rb', line 44

def add_header(headers)
  headers.merge! "Content-Type" => "application/json"
end

#call(env) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/jump_cloud/middleware.rb', line 49

def call(env)
  puts "#{env.method}: #{env.url.to_s}"
  add_header(env[:request_headers])
  unless env.method == :get
    env[:body] = encode env[:body] unless env[:body].respond_to?(:to_str)
  end
  puts "params: #{env.params}"
  puts "body: #{env.body}"
  @app.call(env)
end

#encode(data) ⇒ Object



60
61
62
# File 'lib/jump_cloud/middleware.rb', line 60

def encode(data)
  ::JSON.dump data
end