Skip to content

Rake helpers extended #2

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

Open
wants to merge 3 commits into
base: fix-rake-global-namespace-pollution
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
51 changes: 8 additions & 43 deletions lib/tasks/graphiti.rake
Original file line number Diff line number Diff line change
@@ -1,62 +1,27 @@
namespace :graphiti do
module Graphiti
module Rails
module RakeHelpers
extend RescueRegistry::RailsTestHelpers

module_function

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

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
end

desc "Execute request without web server."
task :request, [:path, :debug] => [:environment] do |_, args|
Graphiti::Rails::RakeHelpers.setup_rails!
require_relative "rake_helpers"
extend Graphiti::Rails::RakeHelpers

Graphiti.logger = Graphiti.stdout_logger
Graphiti::Debugger.preserve = true
require "pp"
path, debug = args[:path], args[:debug]
puts "Graphiti Request: #{path}"
json = Graphiti::Rails::RakeHelpers.make_request(path, debug)
json = 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|
Graphiti::Rails::RakeHelpers.setup_rails!
require_relative "rake_helpers"
extend Graphiti::Rails::RakeHelpers

took = Benchmark.ms {
args[:requests].to_i.times do
Graphiti::Rails::RakeHelpers.make_request(args[:path])
make_request(args[:path])
end
}
puts "Took: #{(took / args[:requests].to_f).round(2)}ms"
Expand Down
46 changes: 46 additions & 0 deletions lib/tasks/rake_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Graphiti::Rails::RakeHelpers
extend RescueRegistry::RailsTestHelpers

def self.included(klass)
klass.class_eval do
setup_rails!
end
end

def self.extended(other)
other.instance_eval do
setup_rails!
end
end

module_function

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

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