Sha256: 7729c8cff4a9c47f03764806c11788f74bfa2d66d4481542848b53ac91715243

Contents?: true

Size: 769 Bytes

Versions: 8

Compression:

Stored size: 769 Bytes

Contents

require 'time'
require "faraday/response"

module ZendeskAPI
  module Middleware
    module Response
      # Parse ISO dates from response body
      class ParseIsoDates < Faraday::Response::Middleware
        def call(env)
          response = @app.call(env)
          parse_dates! response.env[:body]
          response
        end

        private

        def parse_dates!(value)
          case value
          when Hash then value.each { |key,element| value[key] = parse_dates!(element) }
          when Array then value.each_with_index { |element, index| value[index] = parse_dates!(element) }
          when /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z\Z/m then Time.parse(value)
          else
            value
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
zendesk_api-0.1.7 lib/zendesk_api/middleware/response/parse_iso_dates.rb
zendesk_api-0.1.6 lib/zendesk_api/middleware/response/parse_iso_dates.rb
zendesk_api-0.1.5 lib/zendesk_api/middleware/response/parse_iso_dates.rb
zendesk_api-0.1.4 lib/zendesk_api/middleware/response/parse_iso_dates.rb
zendesk_api-0.1.3 lib/zendesk_api/middleware/response/parse_iso_dates.rb
zendesk_api-0.1.2 lib/zendesk_api/middleware/response/parse_iso_dates.rb
zendesk_api-0.1.1 lib/zendesk_api/middleware/response/parse_iso_dates.rb
zendesk_api-0.0.9 lib/zendesk_api/middleware/response/parse_iso_dates.rb