Class: Livedate

Inherits:
Object
  • Object
show all
Defined in:
lib/livedate.rb

Constant Summary collapse

VERSION =
File.read(File.dirname(__FILE__) + "/../VERSION").strip

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ Livedate

Returns a new instance of Livedate.



7
8
9
10
11
12
# File 'lib/livedate.rb', line 7

def initialize(app, options = {}, &block)
  @app             = app
  @date_format     = options[:date_format]     || '%Y-%m-%d'
  @datetime_format = options[:datetime_format] || '%Y-%m-%d %H:%M'
  @invalid_format  = options[:invalid_format].to_s
end

Instance Attribute Details

#date_formatObject (readonly)

Returns the value of attribute date_format.



5
6
7
# File 'lib/livedate.rb', line 5

def date_format
  @date_format
end

#datetime_formatObject (readonly)

Returns the value of attribute datetime_format.



5
6
7
# File 'lib/livedate.rb', line 5

def datetime_format
  @datetime_format
end

#invalid_formatObject (readonly)

Returns the value of attribute invalid_format.



5
6
7
# File 'lib/livedate.rb', line 5

def invalid_format
  @invalid_format
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/livedate.rb', line 14

def call(env)
  if env["PATH_INFO"] =~ /^\/parsedate/
    params = Rack::Utils.parse_query env["QUERY_STRING"]
    [200, {"Content-Type" => "text/plain"}, [respond_to_params(params)]]
  else
    @app.call env
  end
end

#parse_date(input, format) ⇒ Object



28
29
30
31
32
33
# File 'lib/livedate.rb', line 28

def parse_date(input, format)
  require 'chronic'
  
  t = Chronic.parse(input)
  (t ? t.strftime(format) : invalid_format)
end

#respond_to_params(params) ⇒ Object



24
25
26
# File 'lib/livedate.rb', line 24

def respond_to_params(params)
  params['date'] ? parse_date(params['date'], date_format) : parse_date(params['datetime'], datetime_format)
end