Permalink
Browse files

make date-parsing generic and default to iso dates

  • Loading branch information...
1 parent f3f6de8 commit e148e09ef2cc2ce5b01a09a42a254bcf3797f812 @grosser grosser committed May 31, 2012
@@ -2,9 +2,12 @@
require "faraday"
module FaradayMiddleware
- # Parse ISO dates from response body
- class ParseIsoDates < ::Faraday::Response::Middleware
+ # Parse dates from response body
+ class ParseDates < ::Faraday::Response::Middleware
+ ISO_DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\Z/m
+
def initialize(app, options = {})
+ @regexp = options[:match] || ISO_DATE_FORMAT
super(app)
end
@@ -26,7 +29,7 @@ def parse_dates!(value)
value.each_with_index do |element, index|
value[index] = parse_dates!(element)
end
- when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\Z/m
+ when @regexp
Time.parse(value)
else
value
@@ -1,8 +1,8 @@
require 'helper'
-require 'faraday_middleware/response/parse_iso_dates'
+require 'faraday_middleware/response/parse_dates'
require 'json'
-describe FaradayMiddleware::ParseIsoDates, :type => :response do
+describe FaradayMiddleware::ParseDates, :type => :response do
# simulate the json-parse middleware being in front of this one
def process(json)
super(json.strip.empty? ? nil : JSON.parse(json))

0 comments on commit e148e09

Please sign in to comment.