Class: Livedate
- Inherits:
-
Object
- Object
- Livedate
- Defined in:
- lib/livedate.rb
Constant Summary collapse
- VERSION =
File.read(File.dirname(__FILE__) + "/../VERSION").strip
Instance Attribute Summary collapse
-
#date_format ⇒ Object
readonly
Returns the value of attribute date_format.
-
#datetime_format ⇒ Object
readonly
Returns the value of attribute datetime_format.
-
#invalid_format ⇒ Object
readonly
Returns the value of attribute invalid_format.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}, &block) ⇒ Livedate
constructor
A new instance of Livedate.
- #parse_date(input, format) ⇒ Object
- #respond_to_params(params) ⇒ Object
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, = {}, &block) @app = app @date_format = [:date_format] || '%Y-%m-%d' @datetime_format = [:datetime_format] || '%Y-%m-%d %H:%M' @invalid_format = [:invalid_format].to_s end |
Instance Attribute Details
#date_format ⇒ Object (readonly)
Returns the value of attribute date_format.
5 6 7 |
# File 'lib/livedate.rb', line 5 def date_format @date_format end |
#datetime_format ⇒ Object (readonly)
Returns the value of attribute datetime_format.
5 6 7 |
# File 'lib/livedate.rb', line 5 def datetime_format @datetime_format end |
#invalid_format ⇒ Object (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 |