rack-timetraveler 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7cdf12fd5a721f577239b323c8343225e44ab06f
4
+ data.tar.gz: b7533e551ef358d4f58fbbddee663d35efc59932
5
+ SHA512:
6
+ metadata.gz: f062f8d175666f4cb83fecb5ea5379d95a1bb138db0aa4adcaffedde4d153eb2e8b7924dae5c2271c31ecd49175e2527822bb764e5667aabfb5b73bee1807c5d
7
+ data.tar.gz: 06819ddd27cc5580af9fedb97231d0d16dfed4c3d1b3bc32ecc2ca51b64c76f04cdf186c15020aa88862aae7a37b1aaca7f280301054b8a6f946be6dfc5720e4
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+
24
+ /vendor/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ - 2.3.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-timetraveler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 luckypool
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Rack::TimeTraveler
2
+
3
+ [![Build Status](https://travis-ci.org/luckypool/rack-timetraveler.svg?branch=master)](https://travis-ci.org/luckypool/rack-timetraveler)
4
+ [![Coverage Status](https://coveralls.io/repos/github/luckypool/rack-timetraveler/badge.svg?branch=master)](https://coveralls.io/github/luckypool/rack-timetraveler?branch=master)
5
+ [![Dependency Status](https://gemnasium.com/luckypool/rack-timetraveler.svg)](https://gemnasium.com/luckypool/rack-timetraveler)
6
+
7
+
8
+ Rack::TimeTraveler is a Rack::Middleware, and provides "time travel" capabilities.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'rack-timetraveler'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install rack-timetraveler
23
+
24
+ ## Usage
25
+
26
+ Simple usage
27
+
28
+ ```ruby
29
+ use Rack::TimeTraveler
30
+ ```
31
+
32
+ If you needs customized usage
33
+
34
+ ```ruby
35
+ @fetcher = lambda do |env|
36
+ # You can fetch customized timestamp.
37
+ # Default is fetch from ENV['RACK_TIME_TRAVELER_TIMESTAMP']
38
+ 0
39
+ end
40
+
41
+ use Rack::TimeTraveler,
42
+ timestamp_fetcher: @fetcher, # You can also fetch from cache/DB
43
+ enabled_environments: [:development] # enabled only development
44
+ ```
45
+
46
+ #### Examples
47
+
48
+ See [examples/app_1.ru](./examples/app_1.ru)
49
+
50
+ $ bundle exec rackup examples/app_1.ru
51
+ $ curl localhost:9292
52
+ $ curl localhost:9292 -H 'RACK_TIME_TRAVELER_TIMESTAMP:0'
53
+ Time is 1970-01-01 09:00:00 +0900
54
+
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/luckypool/rack-timetraveler/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/examples/app_1.ru ADDED
@@ -0,0 +1,20 @@
1
+ $:.unshift Pathname(__FILE__).dirname.parent + 'lib'
2
+
3
+ require 'rack'
4
+ require 'rack-timetraveler'
5
+
6
+ class App
7
+ def call(env)
8
+ [200, {'Content-Type' => 'text/plain'}, ["Time is #{Time.now.to_s}"]]
9
+ end
10
+ end
11
+
12
+ use Rack::TimeTraveler
13
+ run App.new
14
+
15
+ # Check below:
16
+ #
17
+ # $ bundle exec rackup examples/app_1.ru
18
+ #
19
+ # $ curl localhost:9292
20
+ # $ curl localhost:9292 -H 'RACK_TIME_TRAVELER_TIMESTAMP:0'
data/examples/app_2.ru ADDED
@@ -0,0 +1,22 @@
1
+ $:.unshift Pathname(__FILE__).dirname.parent + 'lib'
2
+
3
+ require 'rack'
4
+ require 'rack-timetraveler'
5
+
6
+ class App
7
+ def call(env)
8
+ [200, {'Content-Type' => 'text/plain'}, ["Time is #{Time.now.to_s}"]]
9
+ end
10
+ end
11
+
12
+ @fetcher = lambda do |env|
13
+ # You can fetch customized timestamp.
14
+ # Default is fetch from ENV['RACK_TIME_TRAVELER_TIMESTAMP']
15
+ 0
16
+ end
17
+
18
+ use Rack::TimeTraveler,
19
+ timestamp_fetcher: @fetcher,
20
+ enabled_environments: [:development]
21
+
22
+ run App.new
@@ -0,0 +1 @@
1
+ require 'rack/timetraveler'
@@ -0,0 +1,49 @@
1
+ require 'rack'
2
+ require 'timecop'
3
+
4
+ class Rack::TimeTraveler
5
+ attr_reader :app, :options
6
+
7
+ def initialize(app, options = {})
8
+ @app, @options = app, options
9
+
10
+ default_fetcher = lambda { |env| fetch_from_env(env) }
11
+ @options[:timestamp_fetcher] ||= default_fetcher
12
+
13
+ @options[:enabled_environments] ||= [:development, :test]
14
+ end
15
+
16
+ def call(env)
17
+ return @app.call(env) unless enabled?
18
+
19
+ time = validate_timestamp(env)
20
+
21
+ return @app.call(env) if time.nil?
22
+
23
+ Timecop.travel(time) { @app.call(env) }
24
+ end
25
+
26
+ private
27
+
28
+ def enabled?
29
+ @options[:enabled_environments].map(&:to_s).include?(ENV['RACK_ENV'])
30
+ end
31
+
32
+ def validate_timestamp(env)
33
+ timestamp = fetch_timestamp(env)
34
+ return if timestamp.nil?
35
+ Time.at(Integer(timestamp))
36
+ rescue
37
+ nil
38
+ end
39
+
40
+ def fetch_timestamp(env)
41
+ fetcher = @options[:timestamp_fetcher]
42
+ fetcher.call(env)
43
+ end
44
+
45
+ def fetch_from_env(env)
46
+ env['HTTP_RACK_TIME_TRAVELER_TIMESTAMP'] ||
47
+ env['RACK_TIME_TRAVELER_TIMESTAMP']
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rack-timetraveler'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['luckypool']
9
+ spec.email = ['luckypool314@gmail.com']
10
+ spec.description = 'You can be a Time Traveler.'
11
+ spec.summary = spec.description
12
+ spec.homepage = 'https://github.com/luckypool/rack-timetraveler'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_runtime_dependency 'timecop', '~> 0.8.0'
21
+ spec.add_runtime_dependency 'rack'
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'pry-byebug'
25
+ spec.add_development_dependency 'rack-test'
26
+ spec.add_development_dependency 'rspec', '~>3.3'
27
+ spec.add_development_dependency 'coveralls'
28
+ spec.add_development_dependency 'simplecov'
29
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::TimeTraveler do
4
+ def build_app(options = {})
5
+ Rack::Builder.new {
6
+ use Rack::TimeTraveler, options
7
+ run lambda {|env| [200, {}, ["Time is #{Time.now.to_i}"]]}
8
+ }.to_app
9
+ end
10
+
11
+ def app
12
+ build_app
13
+ end
14
+
15
+ context 'when using default fetcer (using HEADER)' do
16
+ let(:timestamp) { 0 }
17
+ it 'travels to the epocktime' do
18
+ get '/', {}, 'RACK_TIME_TRAVELER_TIMESTAMP' => timestamp
19
+ expect(last_response.status).to eq 200
20
+ expect(last_response.body).to eq "Time is #{timestamp}"
21
+ end
22
+
23
+ context 'given invalid timestamp' do
24
+ let(:timestamp) { 'invalid' }
25
+ it 'travels to the epocktime' do
26
+ get '/', {}, 'RACK_TIME_TRAVELER_TIMESTAMP' => timestamp
27
+ expect(last_response.status).to eq 200
28
+ expect(last_response.body).to eq "Time is #{Time.now.to_i}"
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ context 'when disabled traveling' do
35
+ def app
36
+ build_app(enabled_environments: [:not_test])
37
+ end
38
+
39
+ it 'travels to the epocktime' do
40
+ get '/', {}, 'RACK_TIME_TRAVELER_TIMESTAMP' => 0
41
+ expect(last_response.status).to eq 200
42
+ expect(last_response.body).to eq "Time is #{Time.now.to_i}"
43
+ end
44
+ end
45
+
46
+ context 'when using custom fetcher' do
47
+ def app
48
+ fetcher = lambda { |env| 0 }
49
+ build_app(timestamp_fetcher: fetcher)
50
+ end
51
+
52
+ let(:timestamp) { 0 }
53
+ it 'travels to the epocktime' do
54
+ get '/'
55
+ expect(last_response.status).to eq 200
56
+ expect(last_response.body).to eq "Time is #{timestamp}"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rack/test'
3
+ require 'rspec'
4
+
5
+ require 'simplecov'
6
+ require 'coveralls'
7
+
8
+ Coveralls.wear!
9
+
10
+ SimpleCov.formatters = [
11
+ SimpleCov::Formatter::HTMLFormatter,
12
+ Coveralls::SimpleCov::Formatter
13
+ ]
14
+
15
+ SimpleCov.start do
16
+ add_filter '.bundle/'
17
+ add_filter 'vendor'
18
+ end
19
+
20
+ require 'rack/timetraveler'
21
+
22
+ RSpec.configure do |conf|
23
+ ENV['RACK_ENV'] ||= 'test'
24
+ conf.include Rack::Test::Methods
25
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-timetraveler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - luckypool
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: timecop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: You can be a Time Traveler.
140
+ email:
141
+ - luckypool314@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".coveralls.yml"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - examples/app_1.ru
155
+ - examples/app_2.ru
156
+ - lib/rack-timetraveler.rb
157
+ - lib/rack/timetraveler.rb
158
+ - rack-timetraveler.gemspec
159
+ - spec/rack/timetraveler_spec.rb
160
+ - spec/spec_helper.rb
161
+ homepage: https://github.com/luckypool/rack-timetraveler
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.4.5
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: You can be a Time Traveler.
185
+ test_files:
186
+ - spec/rack/timetraveler_spec.rb
187
+ - spec/spec_helper.rb