Sha256: 40505594553b268a2a7a7bcc58c0c22ecab794c5c3ea093549ff1f07067d688b

Contents?: true

Size: 865 Bytes

Versions: 9

Compression:

Stored size: 865 Bytes

Contents

require "time"
require "faraday"

module FaradayMiddleware
  # 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}(\.\d+)?(Z|((\+|-)\d{2}:?\d{2}))\Z/m

    def initialize(app, options = {})
      @regexp = options[:match] || ISO_DATE_FORMAT
      super(app)
    end

    def call(env)
      response = @app.call(env)
      parse_dates! response.env[:body]
      response
    end

    private

    def parse_dates!(value)
      case value
      when Hash
        value.each do |key, element|
          value[key] = parse_dates!(element)
        end
      when Array
        value.each_with_index do |element, index|
          value[index] = parse_dates!(element)
        end
      when @regexp
        Time.parse(value)
      else
        value
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
faraday_middleware-0.14.0 lib/faraday_middleware/response/parse_dates.rb
faraday_middleware-0.13.1 lib/faraday_middleware/response/parse_dates.rb
faraday_middleware-0.13.0 lib/faraday_middleware/response/parse_dates.rb
faraday_middleware-0.12.2 lib/faraday_middleware/response/parse_dates.rb
faraday_middleware-0.12.0 lib/faraday_middleware/response/parse_dates.rb
faraday_middleware_safeyaml-0.12.pre.safeyaml lib/faraday_middleware/response/parse_dates.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/faraday_middleware-0.11.0.1/lib/faraday_middleware/response/parse_dates.rb
faraday_middleware-0.11.0.1 lib/faraday_middleware/response/parse_dates.rb
faraday_middleware-0.10.1 lib/faraday_middleware/response/parse_dates.rb