parameter_substitution 0.3.0.pre.1 → 1.1.0.pre.1

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.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/lib/parameter_substitution.rb +1 -2
  3. data/lib/parameter_substitution/configuration.rb +39 -0
  4. data/lib/parameter_substitution/context.rb +74 -0
  5. data/lib/parameter_substitution/encoder.rb +86 -0
  6. data/lib/parameter_substitution/expression.rb +146 -0
  7. data/lib/parameter_substitution/formatters/add_prefix.rb +19 -0
  8. data/lib/parameter_substitution/formatters/base.rb +46 -0
  9. data/lib/parameter_substitution/formatters/blank_if_nil.rb +11 -0
  10. data/lib/parameter_substitution/formatters/cgi_unescape.rb +11 -0
  11. data/lib/parameter_substitution/formatters/compare_string.rb +21 -0
  12. data/lib/parameter_substitution/formatters/date_time_custom.rb +22 -0
  13. data/lib/parameter_substitution/formatters/date_time_format.rb +53 -0
  14. data/lib/parameter_substitution/formatters/date_time_iso8601.rb +11 -0
  15. data/lib/parameter_substitution/formatters/date_time_iso8601_zulu.rb +11 -0
  16. data/lib/parameter_substitution/formatters/date_time_strftime.rb +19 -0
  17. data/lib/parameter_substitution/formatters/date_time_unix_timestamp.rb +11 -0
  18. data/lib/parameter_substitution/formatters/date_time_us_all_slashes.rb +11 -0
  19. data/lib/parameter_substitution/formatters/date_time_us_normal.rb +11 -0
  20. data/lib/parameter_substitution/formatters/date_time_us_seconds.rb +11 -0
  21. data/lib/parameter_substitution/formatters/date_time_us_short_am_pm.rb +11 -0
  22. data/lib/parameter_substitution/formatters/date_time_us_short_year.rb +11 -0
  23. data/lib/parameter_substitution/formatters/date_time_utc_year_first_dashes_seconds.rb +11 -0
  24. data/lib/parameter_substitution/formatters/date_us_dashes.rb +11 -0
  25. data/lib/parameter_substitution/formatters/date_us_normal.rb +11 -0
  26. data/lib/parameter_substitution/formatters/date_year_first_dashes.rb +11 -0
  27. data/lib/parameter_substitution/formatters/downcase.rb +11 -0
  28. data/lib/parameter_substitution/formatters/duration_as_seconds.rb +11 -0
  29. data/lib/parameter_substitution/formatters/duration_as_time.rb +11 -0
  30. data/lib/parameter_substitution/formatters/duration_grouped_by_description.rb +23 -0
  31. data/lib/parameter_substitution/formatters/greater_than_value.rb +21 -0
  32. data/lib/parameter_substitution/formatters/if_nil.rb +19 -0
  33. data/lib/parameter_substitution/formatters/if_truthy.rb +36 -0
  34. data/lib/parameter_substitution/formatters/in_timezone.rb +23 -0
  35. data/lib/parameter_substitution/formatters/json_parse.rb +26 -0
  36. data/lib/parameter_substitution/formatters/left.rb +19 -0
  37. data/lib/parameter_substitution/formatters/lookup.rb +19 -0
  38. data/lib/parameter_substitution/formatters/lower.rb +11 -0
  39. data/lib/parameter_substitution/formatters/manager.rb +35 -0
  40. data/lib/parameter_substitution/formatters/md5.rb +11 -0
  41. data/lib/parameter_substitution/formatters/mid.rb +20 -0
  42. data/lib/parameter_substitution/formatters/parse_time.rb +23 -0
  43. data/lib/parameter_substitution/formatters/right.rb +23 -0
  44. data/lib/parameter_substitution/formatters/sha256.rb +11 -0
  45. data/lib/parameter_substitution/formatters/split_after_colon.rb +11 -0
  46. data/lib/parameter_substitution/formatters/split_and_find.rb +20 -0
  47. data/lib/parameter_substitution/formatters/split_before_colon.rb +11 -0
  48. data/lib/parameter_substitution/formatters/time_with_seconds.rb +11 -0
  49. data/lib/parameter_substitution/formatters/trim.rb +11 -0
  50. data/lib/parameter_substitution/formatters/upper.rb +11 -0
  51. data/lib/parameter_substitution/method_call_expression.rb +58 -0
  52. data/lib/parameter_substitution/parse_error.rb +6 -0
  53. data/lib/parameter_substitution/parser.rb +109 -0
  54. data/lib/parameter_substitution/substitution_expression.rb +79 -0
  55. data/lib/parameter_substitution/text_expression.rb +52 -0
  56. data/lib/parameter_substitution/transform.rb +99 -0
  57. data/lib/parameter_substitution/version.rb +5 -0
  58. metadata +117 -10
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::CgiUnescape < ParameterSubstitution::Formatters::Base
4
+ def self.description
5
+ "Url-decodes the string, preserves nil."
6
+ end
7
+
8
+ def self.format(value)
9
+ value && CGI.unescape(value.to_s)
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::CompareString < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "Compares the field to a string and returns results based on the comparison"
6
+ end
7
+
8
+ def self.has_parameters?
9
+ true
10
+ end
11
+
12
+ def initialize(compare_value, true_value, false_value)
13
+ @compare_value = compare_value
14
+ @true_value = true_value
15
+ @false_value = false_value
16
+ end
17
+
18
+ def format(value)
19
+ value.to_s.casecmp(@compare_value) == 0 ? @true_value : @false_value
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require "parameter_substitution/formatters/date_time_format"
3
+
4
+ class ParameterSubstitution::Formatters::DateTimeCustom < ParameterSubstitution::Formatters::DateTimeFormat
5
+ def self.has_parameters?
6
+ true
7
+ end
8
+
9
+ def self.description
10
+ "Formats a Date String with the provided formatting, converts it's time zone, and then converts it to the desired formatting string."
11
+ end
12
+
13
+ def initialize(from_formatting_string, to_formatting_string, from_time_zone, to_time_zone)
14
+ @parse_options = { from_formatting: from_formatting_string, from_time_zone: from_time_zone }
15
+ @to_formatting = to_formatting_string
16
+ @to_time_zone = to_time_zone
17
+ end
18
+
19
+ def format(value)
20
+ self.class.parse_to_time(value, @parse_options)&.in_time_zone(@to_time_zone)&.strftime(@to_formatting).to_s # rubocop:disable Lint/SafeNavigationChain
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeFormat < ParameterSubstitution::Formatters::Base
4
+ MINIMUM_INTEGER_TIME = Time.new(2000, 0o1, 0o1).to_i
5
+
6
+ class << self
7
+ def parse_to_time(value, parse_options = nil)
8
+ from_custom_time(value, parse_options) ||
9
+ from_yyyymmddhhmmssss(value) ||
10
+ from_unix_time_ms(value) ||
11
+ from_unix_time_sec(value) ||
12
+ from_parse(value)
13
+ rescue ArgumentError => ex
14
+ ex.message =~ /invalid date/ || ex.message =~ /argument out of range/ or raise
15
+ nil
16
+ end
17
+
18
+ def from_yyyymmddhhmmssss(value)
19
+ candidate = value.to_s.strip
20
+ candidate =~ /\d{14}/ && DateTime.strptime(candidate[0, 14], '%Y%m%d%H%M%S')
21
+ end
22
+
23
+ def from_unix_time_sec(value)
24
+ (value.to_f > MINIMUM_INTEGER_TIME) && Time.zone.at(value.to_f)
25
+ end
26
+
27
+ def from_unix_time_ms(value)
28
+ (value.to_i / 1000 > MINIMUM_INTEGER_TIME) && Time.zone.at(value.to_f / 1000)
29
+ end
30
+
31
+ def from_parse(value)
32
+ Time.zone.parse(value.to_s)
33
+ end
34
+
35
+ def from_custom_time(value, parse_options)
36
+ # parse_options = { from_formatting: "%m-%d-%Y %h %m %z", from_time_zone: "Pacific Time (US & Canada)" }
37
+ if parse_options && parse_options[:from_formatting]
38
+ from_time_zone_with_dst = parse_options[:from_time_zone].presence && parse_time_zone_offset(parse_options[:from_time_zone])
39
+ DateTime.strptime("#{value} #{from_time_zone_with_dst}", parse_options[:from_formatting])
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def parse_time_zone_offset(from_time_zone)
46
+ if (time_zone = ActiveSupport::TimeZone.new(from_time_zone))
47
+ time_zone.now.formatted_offset
48
+ else
49
+ raise "Invalid from_time_zone argument #{from_time_zone} See ActiveSupport::TimeZone"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeIso8601 < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "iso8601 - 2017-04-11T15:55:10+00:00"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.iso8601
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeIso8601Zulu < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "iso8601 - 2017-04-11T07:00Z"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%Y-%m-%dT%TZ")
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeStrftime < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.has_parameters?
5
+ true
6
+ end
7
+
8
+ def self.description
9
+ "Formats a DateTime with the provided format string."
10
+ end
11
+
12
+ def initialize(formatting_string)
13
+ @formatting_string = formatting_string
14
+ end
15
+
16
+ def format(value)
17
+ self.class.parse_to_time(value)&.strftime(@formatting_string).to_s
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeUnixTimestamp < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "unix timestamp"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.to_i
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeUsAllSlashes < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "MM/DD/YYYY/hh/mm/ss"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%m/%d/%Y/%H/%M/%S").to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeUsNormal < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "MM/DD/YYYY hh:mm"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%m/%d/%Y %H:%M").to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeUsSeconds < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "MM/DD/YYYY hh:mm:ss"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%m/%d/%Y %H:%M:%S").to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeUsShortAmPm < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "MM/DD/YYYY hh:mm PM"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime('%-m/%-d/%y %l:%M %p').to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeUsShortYear < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "MM/DD/YY hh:mm"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%m/%d/%y %H:%M").to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateTimeUtcYearFirstDashesSeconds < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "2017-04-11 15:55:10 (UTC)"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.utc&.strftime('%Y-%m-%d %H:%M:%S')
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateUsDashes < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "MM-DD-YYYY"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%m-%d-%Y").to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateUsNormal < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "MM/DD/YYYY"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%m/%d/%Y").to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DateYearFirstDashes < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "YYYY-MM-DD"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_to_time(value)&.strftime("%Y-%m-%d").to_s
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::Downcase < ParameterSubstitution::Formatters::Base
4
+ def self.description
5
+ "Converts to string and downcases the values, preserves nil."
6
+ end
7
+
8
+ def self.format(value)
9
+ value&.to_s&.downcase
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DurationAsSeconds < ParameterSubstitution::Formatters::Base
4
+ def self.description
5
+ "Converts a duration to an integer value of seconds"
6
+ end
7
+
8
+ def self.format(value)
9
+ parse_duration(value)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DurationAsTime < ParameterSubstitution::Formatters::Base
4
+ def self.description
5
+ "Converts a duration in seconds into hh:mm:ss"
6
+ end
7
+
8
+ def self.format(duration)
9
+ Time.at(parse_duration(duration)).utc.strftime("%H:%M:%S")
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::DurationGroupedByDescription < ParameterSubstitution::Formatters::Base
4
+ DURATION_DESCRIPTIONS = [
5
+ [30.seconds, "<30sec"],
6
+ [60.seconds, "30-60sec"],
7
+ [5.minutes, "1-5min"],
8
+ [10.minutes, "5-10min"],
9
+ [20.minutes, "10-20min"],
10
+ [30.minutes, "20-30min"],
11
+ [60.minutes, "30-60min"],
12
+ [nil, ">60min"]
13
+ ].freeze
14
+
15
+ def self.description
16
+ "Converts a duration in seconds into one of the following: #{DURATION_DESCRIPTIONS.map(&:last).join(',')}"
17
+ end
18
+
19
+ def self.format(duration)
20
+ fixed_duration = parse_duration(duration)
21
+ DURATION_DESCRIPTIONS.find { |max_duration, _description| !max_duration || fixed_duration.to_i < max_duration }.last
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::GreaterThanValue < ParameterSubstitution::Formatters::Base
4
+ def self.description
5
+ "Compares numerical values and returns results based on the comparison"
6
+ end
7
+
8
+ def self.has_parameters?
9
+ true
10
+ end
11
+
12
+ def initialize(compare_value, true_value, false_value)
13
+ @compare_value = compare_value
14
+ @true_value = true_value
15
+ @false_value = false_value
16
+ end
17
+
18
+ def format(value)
19
+ self.class.parse_duration(value) > @compare_value.to_i ? @true_value : @false_value
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::IfNil < ParameterSubstitution::Formatters::Base
4
+ def self.description
5
+ "Takes one new_value parameter. If the input is nil, the input is replaced with new_value."
6
+ end
7
+
8
+ def self.has_parameters?
9
+ true
10
+ end
11
+
12
+ def initialize(new_value)
13
+ @new_value = new_value
14
+ end
15
+
16
+ def format(value)
17
+ value.nil? ? @new_value : value
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::IfTruthy < ParameterSubstitution::Formatters::Base
4
+ TRUTHY_VALUES = [true, "true", "t", 1, "1", "on", "yes"].freeze
5
+
6
+ def self.description
7
+ "If the input is truthy (i.e. #{TRUTHY_VALUES.inspect}) then the input is replaced with the first argument. Otherwise, the input is replaced with the second argument."
8
+ end
9
+
10
+ def self.has_parameters?
11
+ true
12
+ end
13
+
14
+ def initialize(value_if_true, value_if_false)
15
+ @value_if_true = value_if_true
16
+ @value_if_false = value_if_false
17
+ end
18
+
19
+ def format(value)
20
+ if TRUTHY_VALUES.include?(downcase_if_string(value))
21
+ @value_if_true
22
+ else
23
+ @value_if_false
24
+ end
25
+ end
26
+
27
+ :private
28
+
29
+ def downcase_if_string(value)
30
+ if value.is_a?(String)
31
+ value.downcase
32
+ else
33
+ value
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ParameterSubstitution::Formatters::InTimezone < ParameterSubstitution::Formatters::DateTimeFormat
4
+ def self.description
5
+ "Converts a date time to the specified time zone."
6
+ end
7
+
8
+ def self.has_parameters?
9
+ true
10
+ end
11
+
12
+ def initialize(destination_timezone)
13
+ @destination_timezone = destination_timezone
14
+ end
15
+
16
+ def format(value)
17
+ if (value_as_time = self.class.parse_to_time(value))
18
+ value_as_time.in_time_zone(@destination_timezone).strftime('%Y-%m-%d %H:%M:%S')
19
+ else
20
+ value
21
+ end
22
+ end
23
+ end