fmrest-core 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FmRest
4
+ module V1
5
+ module Utils
6
+ VALID_SCRIPT_KEYS = [:prerequest, :presort, :after].freeze
7
+
8
+ # Converts custom script options to a hash with the Data API's expected
9
+ # JSON script format.
10
+ #
11
+ # If script_options is a string or symbol it will be passed as the name
12
+ # of the script to execute (after the action, e.g. save).
13
+ #
14
+ # If script_options is an array the first element will be the name of the
15
+ # script to execute (after the action) and the second element (if any)
16
+ # will be its param value.
17
+ #
18
+ # If script_options is a hash it will expect to contain one or more of
19
+ # the following keys: :prerequest, :presort, :after
20
+ #
21
+ # Any of those keys should contain either a string/symbol or array, which
22
+ # will be treated as described above, except for their own script
23
+ # execution order (prerequest, presort or after action).
24
+ #
25
+ # @param script_options [String, Hash, Array] The script parameters to
26
+ # convert to canonical form
27
+ #
28
+ # @return [Hash] The converted script parameters
29
+ #
30
+ # @example
31
+ # convert_script_params("My Script")
32
+ # # => { "script": "My Script" }
33
+ #
34
+ # convert_script_params(["My Script", "the param"])
35
+ # # => { "script": "My Script", "script.param": "the param" }
36
+ #
37
+ # convert_script_params(after: "After Script", prerequest: "Prerequest Script")
38
+ # # => { "script": "After Script", "script.prerequest": "Prerequest Script" }
39
+ #
40
+ # convert_script_params(presort: ["Presort Script", "foo"], prerequest: "Prerequest Script")
41
+ # # => {
42
+ # # "script.presort": "After Script",
43
+ # # "script.presort.param": "foo",
44
+ # # "script.prerequest": "Prerequest Script"
45
+ # # }
46
+ #
47
+ def convert_script_params(script_options)
48
+ params = {}
49
+
50
+ case script_options
51
+ when String, Symbol
52
+ params[:script] = script_options.to_s
53
+
54
+ when Array
55
+ params.merge!(convert_script_arguments(script_options))
56
+
57
+ when Hash
58
+ script_options.each_key do |key|
59
+ next if VALID_SCRIPT_KEYS.include?(key)
60
+ raise ArgumentError, "Invalid script option #{key.inspect}"
61
+ end
62
+
63
+ if script_options.has_key?(:prerequest)
64
+ params.merge!(convert_script_arguments(script_options[:prerequest], :prerequest))
65
+ end
66
+
67
+ if script_options.has_key?(:presort)
68
+ params.merge!(convert_script_arguments(script_options[:presort], :presort))
69
+ end
70
+
71
+ if script_options.has_key?(:after)
72
+ params.merge!(convert_script_arguments(script_options[:after]))
73
+ end
74
+ end
75
+
76
+ params
77
+ end
78
+
79
+ # Borrowed from `ERB::Util`
80
+ #
81
+ # This method is preferred to escape Data API URIs components over
82
+ # `URI.encode_www_form_component` (and similar methods) because the
83
+ # latter converts spaces to `+` instead of `%20`, which the Data API
84
+ # doesn't seem to like.
85
+ #
86
+ # @param s [String] An URL component to encode
87
+ # @return [String] The URL-encoded string
88
+ def url_encode(s)
89
+ s.to_s.b.gsub(/[^a-zA-Z0-9_\-.]/n) { |m|
90
+ sprintf("%%%02X", m.unpack("C")[0])
91
+ }
92
+ end
93
+
94
+ private
95
+
96
+ def convert_script_arguments(script_arguments, suffix = nil)
97
+ base = suffix ? "script.#{suffix}".to_sym : :script
98
+
99
+ {}.tap do |params|
100
+ case script_arguments
101
+ when String, Symbol
102
+ params[base] = script_arguments.to_s
103
+ when Array
104
+ params[base] = script_arguments.first.to_s
105
+ params["#{base}.param".to_sym] = script_arguments[1] if script_arguments[1]
106
+ else
107
+ raise ArgumentError, "Script arguments are expected as a String, Symbol or Array"
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FmRest
4
+ VERSION = "0.13.0"
5
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fmrest-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.13.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Carbajal
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday_middleware
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.9.1
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.9.1
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '2.0'
53
+ description: fmrest-core is a FileMaker Data API client built with Faraday. It handles
54
+ authentication as well as providing many utilities for working with the Data API.
55
+ An ORM library built on top of fmrest-core is also available.
56
+ email:
57
+ - pedro_c@beezwax.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".yardopts"
63
+ - CHANGELOG.md
64
+ - LICENSE.txt
65
+ - README.md
66
+ - lib/fmrest-core.rb
67
+ - lib/fmrest-spyke.rb
68
+ - lib/fmrest.rb
69
+ - lib/fmrest/connection_settings.rb
70
+ - lib/fmrest/errors.rb
71
+ - lib/fmrest/string_date.rb
72
+ - lib/fmrest/token_store.rb
73
+ - lib/fmrest/token_store/active_record.rb
74
+ - lib/fmrest/token_store/base.rb
75
+ - lib/fmrest/token_store/memory.rb
76
+ - lib/fmrest/token_store/moneta.rb
77
+ - lib/fmrest/token_store/null.rb
78
+ - lib/fmrest/token_store/redis.rb
79
+ - lib/fmrest/v1.rb
80
+ - lib/fmrest/v1/auth.rb
81
+ - lib/fmrest/v1/connection.rb
82
+ - lib/fmrest/v1/container_fields.rb
83
+ - lib/fmrest/v1/dates.rb
84
+ - lib/fmrest/v1/paths.rb
85
+ - lib/fmrest/v1/raise_errors.rb
86
+ - lib/fmrest/v1/token_session.rb
87
+ - lib/fmrest/v1/token_store/active_record.rb
88
+ - lib/fmrest/v1/token_store/memory.rb
89
+ - lib/fmrest/v1/type_coercer.rb
90
+ - lib/fmrest/v1/utils.rb
91
+ - lib/fmrest/version.rb
92
+ homepage: https://github.com/beezwax/fmrest-ruby
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.2.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: FileMaker Data API client using Faraday, core library
115
+ test_files: []