Skip to content

Commit

Permalink
Dropping Time instances support
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasas committed Aug 6, 2014
1 parent 2931f40 commit c4769e8
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 71 deletions.
17 changes: 0 additions & 17 deletions README.md
Expand Up @@ -501,23 +501,6 @@ class HttpCacheControlController
end
```

It allows you specify value directives using `Time` instances:

```ruby
require 'lotus/controller'
require 'lotus/action/cache_control'

class HttpCacheControlController
include Lotus::Action
include Lotus::Action::CacheControl

def call(params)
# ...
cache_control :public, max_age: Time.now + 600 # => Cache-Control: public, max-age=600
end
end
```

You can set the Expires header too:

```ruby
Expand Down
9 changes: 2 additions & 7 deletions lib/lotus/action/cache_control.rb
Expand Up @@ -98,13 +98,8 @@ def cache_control(*values)
# end
#
def expires(amount, *values)
if amount.is_a? Integer
time = Time.now + amount.to_i
max_age = amount
else
time = amount
max_age = time - Time.now
end
time = Time.now + amount.to_i
max_age = amount

directives = *values
directives << { max_age: max_age }
Expand Down
6 changes: 1 addition & 5 deletions lib/lotus/action/cache_control/directives.rb
Expand Up @@ -22,16 +22,12 @@ def initialize(name, value)
end

def to_str
"#{@name.to_s.tr('_', '-')}=#{value.to_i}"
"#{@name.to_s.tr('_', '-')}=#{@value.to_i}"
end

def valid?
VALUE_DIRECTIVES.include? @name
end

def value
@value.is_a?(Time) ? @value - Time.now : @value
end
end

class NonValueDirective
Expand Down
29 changes: 0 additions & 29 deletions test/integration/cache_control_test.rb
Expand Up @@ -5,15 +5,13 @@
get '/symbol', to: 'cache_control#symbol'
get '/symbols', to: 'cache_control#symbols'
get '/hash', to: 'cache_control#hash'
get '/hash-containing-time', to: 'cache_control#hash_containing_time'
get '/private-and-public', to: 'cache_control#private_public'
end

ExpiresRoutes = Lotus::Router.new do
get '/symbol', to: 'expires#symbol'
get '/symbols', to: 'expires#symbols'
get '/hash', to: 'expires#hash'
get '/hash-containing-time', to: 'expires#hash_containing_time'
end

ConditionalGetRoutes = Lotus::Router.new do
Expand Down Expand Up @@ -43,12 +41,6 @@ def call(params)
end
end

action 'HashContainingTime' do
def call(params)
cache_control :public, :no_store, max_age: (Time.now + 900), s_maxage: (Time.now + 86400), min_fresh: (Time.now + 500), max_stale: (Time.now + 700)
end
end

action 'PrivatePublic' do
def call(params)
cache_control :private, :public
Expand Down Expand Up @@ -76,12 +68,6 @@ def call(params)
expires 900, :public, :no_store, s_maxage: 86400, min_fresh: 500, max_stale: 700
end
end

action 'HashContainingTime' do
def call(params)
expires (Time.now + 900), :public, :no_store, s_maxage: (Time.now + 86400), min_fresh: (Time.now + 500), max_stale: (Time.now + 700)
end
end
end

class ConditionalGetController
Expand Down Expand Up @@ -128,13 +114,6 @@ def call(params)
end
end

it 'accepts a Hash containing Time objects' do
Time.stub(:now, Time.now) do
response = @app.get('/hash-containing-time')
response.headers.fetch('Cache-Control').split(', ').must_equal %w(public no-store max-age=900 s-maxage=86400 min-fresh=500 max-stale=700)
end
end

describe "private and public directives" do
it "ignores public directive" do
response = @app.get('/private-and-public')
Expand Down Expand Up @@ -171,14 +150,6 @@ def call(params)
response.headers.fetch('Cache-Control').split(', ').must_equal %w(public no-store s-maxage=86400 min-fresh=500 max-stale=700 max-age=900)
end
end

it 'accepts a Hash containing Time objects' do
Time.stub(:now, Time.now) do
response = @app.get('/hash-containing-time')
response.headers.fetch('Expires').must_equal (Time.now + 900).httpdate
response.headers.fetch('Cache-Control').split(', ').must_equal %w(public no-store s-maxage=86400 min-fresh=500 max-stale=700 max-age=900)
end
end
end

describe 'Fresh' do
Expand Down
13 changes: 0 additions & 13 deletions test/unit/cache_control/directives_test.rb
Expand Up @@ -112,19 +112,6 @@
subject = Lotus::Action::CacheControl::ValueDirective.new(:max_age, 600)
subject.to_str.must_equal('max-age=600')
end

describe 'value is a time instance' do
before do
@now = Time.now
end

it 'returns as http cache format with value as integer' do
Time.stub(:now, @now) do
subject = Lotus::Action::CacheControl::ValueDirective.new(:max_age, (@now + 1600))
subject.to_str.must_equal('max-age=1600')
end
end
end
end
end

Expand Down

0 comments on commit c4769e8

Please sign in to comment.