Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7 body parameters #8

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ source 'https://rubygems.org'
gemspec

group :test do
gem 'debug'
gem 'rake'
gem "factory_girl", "~> 4.0"
gem "rails", "~> 4.2"
gem "factory_girl" #, "~> 4.0"
gem "rails" #, "~> 4.2"
end
14 changes: 7 additions & 7 deletions grape-middleware-lograge.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

Gem::Specification.new do |spec|
spec.name = 'grape-middleware-lograge'
spec.version = '1.2.3'
spec.version = '1.3.1'
spec.platform = Gem::Platform::RUBY
spec.authors = ['Ryan Buckley', 'Paul Chavard']
spec.email = ['[email protected]', '[email protected]']
spec.authors = ['Ryan Buckley', 'Paul Chavard','Josh Buermann']
spec.email = ['[email protected]', '[email protected]','[email protected]']
spec.summary = %q{A logger for the Grape framework}
spec.description = %q{Logging middleware for the Grape framework, that uses Lograge}
spec.homepage = 'https://github.com/tchak/grape-middleware-lograge'
Expand All @@ -16,10 +16,10 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_dependency 'grape', '>= 0.14', '< 1'
spec.add_dependency 'grape', '>= 0.14'
spec.add_dependency 'lograge', '~> 0.3'

spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '>= 3.2', '< 4'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
end
28 changes: 22 additions & 6 deletions lib/grape/middleware/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ def initialize(_, options = {})
def before
super

@db_duration = 0
@db_runtime = 0

@db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
@db_duration += event.duration
@db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, start, ending, _transaction_id, _payload|
@db_runtime += 1000.0 * (ending - start) if ending && start
end if defined?(ActiveRecord)

ActiveSupport::Notifications.instrument("start_processing.grape", raw_payload)
Expand Down Expand Up @@ -58,15 +57,27 @@ def call!(env)
after(payload, response.status)
end

# Parse request parameters from the PUT/POST body and include them in the params payload
body = env[Grape::Env::API_REQUEST_INPUT]
if body.present?
fmt = request.media_type ? mime_types[request.media_type] : options[:default_format]
parser = Grape::Parser.parser_for fmt
if content_type_for(fmt)
body_params = parser.call(body, env)
payload[:params].merge!(body_params)
end
end

@app_response
end
end

def after(payload, status)
ActiveSupport::Notifications.unsubscribe(@db_subscription) if @db_subscription
payload[:status] = status
payload[:format] = env['api.format']
payload[:version] = env['api.version']
payload[:db_runtime] = @db_duration
payload[:db_runtime] = @db_runtime
end

def after_exception(payload, e)
Expand All @@ -90,8 +101,9 @@ def after_failure(payload, error)
end

def parameters
request_params = env[Grape::Env::GRAPE_REQUEST_PARAMS].to_hash
request_params = env[Grape::Env::GRAPE_REQUEST_PARAMS] #Grape::Env::GRAPE_REQUEST].params
request_params.merge!(env['action_dispatch.request.request_parameters'.freeze] || {}) # for Rails

if @options[:filter]
@options[:filter].filter(request_params)
else
Expand All @@ -113,6 +125,10 @@ def raw_payload
}
end

def request
env[Grape::Env::GRAPE_REQUEST]
end

def endpoint
env[Grape::Env::API_ENDPOINT]
end
Expand Down
10 changes: 8 additions & 2 deletions lib/grape/middleware/lograge/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class Grape::Middleware::Lograge::Railtie < Rails::Railtie
initializer 'grape.middleware.lograge', after: :load_config_initializers do
Grape::Middleware::Lograge.filter = ActionDispatch::Http::ParameterFilter.new Rails.application.config.filter_parameters
filter_class = Rails::VERSION::MAJOR > 5 ? ActiveSupport::ParameterFilter : ActionDispatch::Http::ParameterFilter

::Lograge::RequestLogSubscriber.attach_to :grape
Grape::Middleware::Lograge.filter = filter_class.new(Rails.application.config.filter_parameters)

if Gem::Version.new( Lograge::VERSION ) > Gem::Version.new('0.10.9')
Lograge::LogSubscribers::Base.attach_to :grape
else
::Lograge::RequestLogSubscriber.attach_to :grape
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/grape/middleware/lograge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
expect(app).to receive(:call).with(env).and_return(app_response)
allow(subject).to receive(:before)
allow(subject).to receive(:after)
expect(subject.call!(env)).to eq app_response
expect(subject.call!(env).to_a).to eq app_response
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'ostruct'
require 'debug'
require 'factory_girl'
require 'grape/middleware/lograge'

Expand Down