cookpad/trice

Name: trice

Owner: Cookpad Inc.

Description: Provides reference time concept to application. Use it instead of ad-hoc `Time.now`.

Created: 2016-02-03 09:39:04.0

Updated: 2018-05-22 14:29:10.0

Pushed: 2017-04-17 00:33:12.0

Homepage: null

Size: 48

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Trice

Build Status

Provides reference time concept to application. Use it instead of ad-hoc Time.now.

Setting consistent reference time
Installation

Add this line to your application's Gemfile:

'trice'

And then execute:

ndle
Usage
With Rails controller

This gem aims to serve consistency of time handling using reference time to Rails application. The layer which should set reference time is controller layer, because reference time is one of external input.

Include Trice::ControllerMethods to your controller

s ApplicationController < AC::Base
clude Trice::ControllerMethods

Then your controller and view gets an accessor method to access consistent time object.

Include helper module outside of controller

Include Trice::ReferenceTime add #reference_time method to lookup current reference time.

Use it in Rails model.

s MyWork
clude Trice::ReferenceTime

f do!(at: nil)
self.done_at = at || reference_time
d

Setting consistent reference time

Set reference time with Trice.reference_time = _time_ or Trice.with_reference_time(_time_, &block). Accessible by Trice.reference_time..

me.now
016-02-01 11:25:37 +0900

e.with_reference_time = Time.iso8601('2016-02-01T09:00:00Z')
ice.reference_time
 2016-02-01 09:00:00 UTC

e.with_reference_time(Time.iso8601('2016-02-01T10:00:00Z')) do
Trice.reference_time
=> 2016-02-01 10:00:00 UTC


e.with_reference_time = nil
ice.reference_time
 raise Trice::NoReferenceTime

The time is stored in thread local variable.

Time Stubbing

Trice allows you to stub reference time to run travelled time-testing and / or previewing your app in future time.

Set _requested_at=<timish> query parameter like below

rl https://example.com/campaigns/12345?_requested_at=201602151300

Or can set HTTP header, useful for tests.

QUESTED-AT: 2016-02-15T13:00:00+09:00

Value format, which specified both query parameter and header, should be Time.parse parasable.

Enable/Disable stubbing

Toggle requested at stubbing in config/initializers. The default is below, enabled unless Rails.env.production?.

e.support_requested_at_stubbing = !Rails.env.production?

Setting callable object let you choice enable/disable dynamically by seeing request.

office_network = IPAddr.new('203.0.113.0/24')

e.support_requested_at_stubbing = ->(controller) {
xt true unless Rails.env.production?

r_office_network.include?(controller.request.remote_ip)

Test helpers

There is a test helper method for feature spec.

c.configure do |config|
nfig.extend Trice::SpecHelper

I recommend to pass reference time to a model by method and/or constructor argument because reference time is an external input, should be handled controller layer. But sometimes it is required from deep inside of model logics and tests for them.

Model unit spec has with_reference_time and set_now_to_reference_time declaration method to set Trice.reference_time in an example.

ribe MyModel do
t(:reference_time) { Time.zone.parse('2016/02/03 12:00') }
ntext  do
set_reference_time { reference_time }

let(:model) { MyModel.find_by_something(key) }

specify do
  # can accessible `reference_time` in MyModel#do_something
  expect { model.do_something }.not_to raise(Trice::NoReferenceTime)
end
d

Feature specs (or other Capybara based E2E tests) also has helper method using stubbing mechanism. stub_requested_at <timish> set X-Trice-Requested-At automatically.

ext 'on ???? day' do
ub_requested_at Time.zone.parse('2016-03-03 10:00')

enario 'See Hinamatsuri special banner at 3/3 request' do
visit root_path
within '#custom-header' do
  expect(page).to contain '????'
end
d

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/cookpad/trice.

License

The gem is available as open source under the terms of the MIT License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.