Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Rack middleware to travel in time your rails application
class Dummy::TimeMachine
def initialize(app)
@app = app
end
def call(env)
Rails.logger.info("[TimeMachine] Starting time travel.")
Timecop.return
request = Rack::Request.new(env)
params = request.params
# Do whatever you need. A simple exapmle by passing time in params
if params["simulate_time"].present?
my_time = get_date_from_params(params)
Timecop.travel(my_time)
Rails.logger.info("[TimeMachine] Traveling to: #{my_time}")
end
status, headers, response = @app.call(env)
Timecop.return
Rails.logger.info("[TimeMachine] Ending time travel. Ensure reseting time.")
[status, headers, response]
end
def get_date_from_params(params)
time = params["simulate_time"]
Time.new(time["year"], time["month"], time["day"], time["hour"], time["minute"], time["second"])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment