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

Fix rake task helpers polluting global namespace #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 37 additions & 29 deletions lib/tasks/graphiti.rake
Original file line number Diff line number Diff line change
@@ -1,54 +1,62 @@
namespace :graphiti do
include RescueRegistry::RailsTestHelpers
module Graphiti
module Rails
module RakeHelpers
extend RescueRegistry::RailsTestHelpers

def session
@session ||= ActionDispatch::Integration::Session.new(Rails.application)
end
module_function

def setup_rails!
Rails.application.eager_load!
Rails.application.config.cache_classes = true
Rails.application.config.action_controller.perform_caching = false
end
def session
@session ||= ActionDispatch::Integration::Session.new(::Rails.application)
end

def make_request(path, debug = false)
if path.split("/").length == 2
path = "#{ApplicationResource.endpoint_namespace}#{path}"
end
path << if path.include?("?")
"&cache=bust"
else
"?cache=bust"
end
path = "#{path}&debug=true" if debug
handle_request_exceptions do
headers = {
"Authorization": ENV['AUTHORIZATION_HEADER']
}.compact
session.get(path.to_s, headers: headers)
def setup_rails!
::Rails.application.eager_load!
::Rails.application.config.cache_classes = true
::Rails.application.config.action_controller.perform_caching = false
end

def make_request(path, debug = false)
if path.split("/").length == 2
path = "#{ApplicationResource.endpoint_namespace}#{path}"
end
path << if path.include?("?")
"&cache=bust"
else
"?cache=bust"
end
path = "#{path}&debug=true" if debug
handle_request_exceptions do
headers = {
"Authorization": ENV['AUTHORIZATION_HEADER']
}.compact
session.get(path.to_s, headers: headers)
end
JSON.parse(session.response.body)
end
end
end
JSON.parse(session.response.body)
end

desc "Execute request without web server."
task :request, [:path, :debug] => [:environment] do |_, args|
setup_rails!
Graphiti::Rails::RakeHelpers.setup_rails!
Graphiti.logger = Graphiti.stdout_logger
Graphiti::Debugger.preserve = true
require "pp"
path, debug = args[:path], args[:debug]
puts "Graphiti Request: #{path}"
json = make_request(path, debug)
json = Graphiti::Rails::RakeHelpers.make_request(path, debug)
pp json
Graphiti::Debugger.flush if debug
end

desc "Execute benchmark without web server."
task :benchmark, [:path, :requests] => [:environment] do |_, args|
setup_rails!
Graphiti::Rails::RakeHelpers.setup_rails!
took = Benchmark.ms {
args[:requests].to_i.times do
make_request(args[:path])
Graphiti::Rails::RakeHelpers.make_request(args[:path])
end
}
puts "Took: #{(took / args[:requests].to_f).round(2)}ms"
Expand Down