Sha256: 5ab7bb2cb1064cedc348886e5de6a16bdc9fed3d4283b4a7ba4277c40b29df2c

Contents?: true

Size: 816 Bytes

Versions: 38

Compression:

Stored size: 816 Bytes

Contents

require 'chronic'

module Gris
  module DateTimeHelpers
    extend ActiveSupport::Concern

    # define keys that need to be converted from strings
    # to datetime via Chronic
    def datetime_params(*keys_to_convert)
      @datetime_keys_to_convert = Set.new(keys_to_convert)
    end

    def keys_to_convert
      @datetime_keys_to_convert
    end

    def process_datetime_params(params)
      return unless keys_to_convert
      params.each_key do |key|
        if keys_to_convert.include?(key)
          params[key] = string_to_datetime(params[key])
        end
      end
    end

    def string_to_datetime(string)
      return string if string.blank?
      if string.to_s =~ /^\d+$/
        Time.at(string.to_i).to_datetime.utc
      else
        Chronic.parse(string).try(:utc)
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
gris-0.6.8 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.6.7 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.6.6 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.6.5 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.6.2 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.6.1 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.6.0 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.9 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.8 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.7 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.6 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.5 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.4 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.3 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.2 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.1 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.5.0 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.4.9 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.4.8 lib/gris/grape_extensions/date_time_helpers.rb
gris-0.4.7 lib/gris/grape_extensions/date_time_helpers.rb