Skip to content

Instantly share code, notes, and snippets.

@enoliglesias
Last active March 8, 2016 19:32
Show Gist options
  • Save enoliglesias/d79cbef0f87be2c13099 to your computer and use it in GitHub Desktop.
Save enoliglesias/d79cbef0f87be2c13099 to your computer and use it in GitHub Desktop.
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