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
-
#convert_script_params(script_options) ⇒ Hash
Converts custom script options to a hash with the Data API's expected JSON script format.
-
#escape_find_operators(s) ⇒ String
Escapes FileMaker find operators from the given string in order to use it in a find request.
-
#is_fully_qualified?(field_name) ⇒ Boolean
Returns whether the given FileMaker field name is a fully-qualified name.
-
#url_encode(s) ⇒ String
Borrowed from
ERB::Util
.
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
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() params = {} case when String, Symbol params[:script] = .to_s when Array params.merge!(convert_script_arguments()) when Hash .each_key do |key| next if VALID_SCRIPT_KEYS.include?(key) raise ArgumentError, "Invalid script option #{key.inspect}" end if .has_key?(:prerequest) params.merge!(convert_script_arguments([:prerequest], :prerequest)) end if .has_key?(:presort) params.merge!(convert_script_arguments([:presort], :presort)) end if .has_key?(:after) params.merge!(convert_script_arguments([: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.
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.
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.
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 |