class FmRest::V1::TypeCoercer

Constants

COERCE_FULL
COERCE_HYBRID
JULIAN_ZERO_DAY

We use this date to represent a FileMaker time for consistency with ginjo-rfm

Public Class Methods

new(app, settings) click to toggle source

@param app [#call] @param settings [FmRest::ConnectionSettings]

Calls superclass method
# File lib/fmrest/v1/type_coercer.rb, line 17
def initialize(app, settings)
  super(app)
  @settings = settings
end

Public Instance Methods

on_complete(env) click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 22
def on_complete(env)
  return unless enabled?
  return unless env.body.kind_of?(Hash)

  data = env.body.dig("response", "data") || env.body.dig(:response, :data)

  return unless data

  data.each do |record|
    field_data = record["fieldData"] || record[:fieldData]
    portal_data = record["portalData"] || record[:portalData]

    coerce_fields(field_data)

    portal_data.try(:each_value) do |portal_records|
      portal_records.each do |pr|
        coerce_fields(pr)
      end
    end
  end
end

Private Instance Methods

coerce_dates() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 185
def coerce_dates
  @settings.coerce_dates
end
Also aliased as: enabled?
coerce_fields(hash) click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 46
def coerce_fields(hash)
  hash.each do |k, v|
    next unless v.is_a?(String)
    next if k == "recordId" || k == :recordId || k == "modId" || k == :modId

    if quick_check_timestamp(v)
      begin
        hash[k] = coerce_timestamp(v)
        next
      rescue ArgumentError
      end
    end

    if quick_check_date(v)
      begin
        hash[k] = date_class.strptime(v, date_strptime_format)
        next
      rescue ArgumentError
      end
    end

    if quick_check_time(v)
      begin
        hash[k] = datetime_class.strptime("#{JULIAN_ZERO_DAY} #{v}", time_strptime_format)
        next
      rescue ArgumentError
      end
    end
  end
end
coerce_timestamp(str) click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 77
def coerce_timestamp(str)
  str_timestamp = DateTime.strptime(str, datetime_strptime_format)

  if local_timezone?
    # Change the DateTime to the local timezone, keeping the same
    # time and just modifying the timezone
    offset = FmRest::V1.local_offset_for_datetime(str_timestamp)
    str_timestamp = str_timestamp.new_offset(offset) - offset
  end

  if datetime_class == StringDateTime
    str_timestamp = StringDateTime.new(str, str_timestamp)
  end

  str_timestamp
end
date_class() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 94
def date_class
  @date_class ||=
    case coerce_dates
    when *COERCE_HYBRID
      StringDate
    when *COERCE_FULL
      Date
    end
end
date_fm_format() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 114
def date_fm_format
  @settings.date_format
end
date_strptime_format() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 126
def date_strptime_format
  FmRest::V1.fm_date_to_strptime_format(date_fm_format)
end
datetime_class() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 104
def datetime_class
  @datetime_class ||=
    case coerce_dates
    when *COERCE_HYBRID
      StringDateTime
    when *COERCE_FULL
      DateTime
    end
end
datetime_strptime_format() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 130
def datetime_strptime_format
  FmRest::V1.fm_date_to_strptime_format(timestamp_fm_format)
end
enabled?()
Alias for: coerce_dates
local_timezone?() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 181
def local_timezone?
  @local_timezone ||= @settings.timezone.try(:to_sym) == :local
end
quick_check_date(v) click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 160
def quick_check_date(v)
  v.length == date_fm_format.length && FmRest::V1::fm_date_to_regexp(date_fm_format).match?(v)
end
quick_check_time(v) click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 164
def quick_check_time(v)
  v.length == time_fm_format.length && FmRest::V1::fm_date_to_regexp(time_fm_format).match?(v)
end
quick_check_timestamp(v) click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 156
def quick_check_timestamp(v)
  v.length == timestamp_fm_format.length && FmRest::V1::fm_date_to_regexp(timestamp_fm_format).match?(v)
end
time_fm_format() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 122
def time_fm_format
  @settings.time_format
end
time_strptime_format() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 134
def time_strptime_format
  @time_strptime_format ||=
    "%Y/%m/%d " + FmRest::V1.fm_date_to_strptime_format(time_fm_format)
end
timestamp_fm_format() click to toggle source
# File lib/fmrest/v1/type_coercer.rb, line 118
def timestamp_fm_format
  @settings.timestamp_format
end