Module: FmRest::V1::Utils

Included in:
FmRest::V1
Defined in:
lib/fmrest/v1/utils.rb

Constant Summary collapse

VALID_SCRIPT_KEYS =
[:prerequest, :presort, :after].freeze
FM_FIND_OPERATORS_RE =
/([@\*#\?!=<>"])/
FULLY_QUALIFIED_FIELD_NAME_MATCHER =
/\A[^:]+::[^:]+\Z/.freeze

Instance Method Summary collapse

Instance Method Details

#convert_script_params(script_options) ⇒ Hash

Converts custom script options to a hash with the Data API's expected JSON script format.

If script_options is a string or symbol it will be passed as the name of the script to execute (after the action, e.g. save).

If script_options is an array the first element will be the name of the script to execute (after the action) and the second element (if any) will be its param value.

If script_options is a hash it will expect to contain one or more of the following keys: :prerequest, :presort, :after

Any of those keys should contain either a string/symbol or array, which will be treated as described above, except for their own script execution order (prerequest, presort or after action).

convert to canonical form

Examples:

convert_script_params("My Script")
# => { "script": "My Script" }

convert_script_params(["My Script", "the param"])
# => { "script": "My Script", "script.param": "the param" }

convert_script_params(after: "After Script", prerequest: "Prerequest Script")
# => { "script": "After Script", "script.prerequest": "Prerequest Script" }

convert_script_params(presort: ["Presort Script", "foo"], prerequest: "Prerequest Script")
# => {
#      "script.presort": "After Script",
#      "script.presort.param": "foo",
#      "script.prerequest": "Prerequest Script"
#    }

Parameters:

  • script_options (String, Hash, Array)

    The script parameters to

Returns:

  • (Hash)

    The converted script parameters



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fmrest/v1/utils.rb', line 52

def convert_script_params(script_options)
  params = {}

  case script_options
  when String, Symbol
    params[:script] = script_options.to_s

  when Array
    params.merge!(convert_script_arguments(script_options))

  when Hash
    script_options.each_key do |key|
      next if VALID_SCRIPT_KEYS.include?(key)
      raise ArgumentError, "Invalid script option #{key.inspect}"
    end

    if script_options.has_key?(:prerequest)
      params.merge!(convert_script_arguments(script_options[:prerequest], :prerequest))
    end

    if script_options.has_key?(:presort)
      params.merge!(convert_script_arguments(script_options[:presort], :presort))
    end

    if script_options.has_key?(:after)
      params.merge!(convert_script_arguments(script_options[:after]))
    end
  end

  params
end

#escape_find_operators(s) ⇒ String

Escapes FileMaker find operators from the given string in order to use it in a find request.

Parameters:

  • s (String)

    The string to escape

Returns:

  • (String)

    A new string with find operators escaped with backslashes



105
106
107
# File 'lib/fmrest/v1/utils.rb', line 105

def escape_find_operators(s)
  s.gsub(FM_FIND_OPERATORS_RE, "\\\\\\1")
end

#is_fully_qualified?(field_name) ⇒ Boolean

Returns whether the given FileMaker field name is a fully-qualified name. In other words, whether it contains the string "::".

Note that this is a simple naive check which doesn't account for invalid field names.

Parameters:

  • field_name (String)

    The field name to test

Returns:

  • (Boolean)

    Whether the field is a FQN



117
118
119
# File 'lib/fmrest/v1/utils.rb', line 117

def is_fully_qualified?(field_name)
  FULLY_QUALIFIED_FIELD_NAME_MATCHER === field_name.to_s
end

#url_encode(s) ⇒ String

Borrowed from ERB::Util

This method is preferred to escape Data API URIs components over URI.encode_www_form_component (and similar methods) because the latter converts spaces to + instead of %20, which the Data API doesn't seem to like.

Parameters:

  • s (String)

    An URL component to encode

Returns:

  • (String)

    The URL-encoded string



93
94
95
96
97
# File 'lib/fmrest/v1/utils.rb', line 93

def url_encode(s)
  s.to_s.b.gsub(/[^a-zA-Z0-9_\-.]/n) { |m|
    sprintf("%%%02X", m.unpack("C")[0])
  }
end