Skip to content

Commit

Permalink
fix export request
Browse files Browse the repository at this point in the history
  • Loading branch information
jvigne committed Jul 17, 2023
1 parent 58e2cf1 commit 6495206
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 38 deletions.
91 changes: 79 additions & 12 deletions lib/ad_localize/mappers/options_to_export_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,85 @@ module AdLocalize
module Mappers
class OptionsToExportRequest
def map(options:)
Requests::ExportRequest.new(
locales: options[:locales],
bypass_empty_values: options[:'non-empty-values'],
csv_paths: options[:csv_paths],
merge_policy: options[:'merge-policy'],
output_path: options[:'target-dir'],
platforms: options[:only],
spreadsheet_id: options[:'drive-key'],
sheet_ids: options[:sheets],
export_all: options[:'export-all-sheets'],
verbose: options[:debug]
)
args = options_to_args(options)
Requests::ExportRequest.new(args)
end

private

def options_to_args(hash)
{
locales: sanitized_locales(hash[:locales]),
bypass_empty_values: sanitized_bypass_empty_values(hash[:'non-empty-values']),
csv_paths: sanitized_csv_paths(hash[:csv_paths]),
merge_policy: sanitized_merge_policy(hash[:'merge-policy']),
output_path: sanitized_output_path(hash[:'target-dir']),
platforms: sanitized_platforms(hash[:only]),
spreadsheet_id: sanitized_spreadsheet_id(hash[:'drive-key']),
sheet_ids: sanitized_sheet_ids(hash[:sheets]),
export_all: sanitized_export_all(hash[:'export-all-sheets']),
verbose: sanitized_verbose(hash[:debug])
}
end

def sanitized_locales(locales_value)
return Requests::ExportRequest::DEFAULTS[:locales] if locales_value.blank?

Array(locales_value)
end

def sanitized_bypass_empty_values(bypass_empty_flag)
return Requests::ExportRequest::DEFAULTS[:bypass_empty_values] if bypass_empty_flag.nil?

bypass_empty_flag
end

def sanitized_csv_paths(csv_path_list)
return Requests::ExportRequest::DEFAULTS[:csv_paths] if csv_path_list.blank?

Array(csv_path_list)
end

def sanitized_merge_policy(merge_policy_flag)
return Requests::ExportRequest::DEFAULTS[:merge_policy] if merge_policy_flag.nil?

merge_policy_flag
end

def sanitized_output_path(output_path_value)
return Requests::ExportRequest::DEFAULTS[:output_path] if output_path_value.blank?

Pathname.new(output_path_value)
end

def sanitized_platforms(platform_list)
return Requests::ExportRequest::DEFAULTS[:platforms] if platform_list.blank?

Array(platform_list) & Entities::Platform::SUPPORTED_PLATFORMS
end

def sanitized_spreadsheet_id(spreadsheet_id_value)
return Requests::ExportRequest::DEFAULTS[:spreadsheet_id] if spreadsheet_id_value.blank?

spreadsheet_id_value
end

def sanitized_sheet_ids(sheet_id_list)
return Requests::ExportRequest::DEFAULTS[:sheet_ids] if sheet_id_list.blank?

Array(sheet_id_list)
end

def sanitized_export_all(export_all_sheets_flag)
return Requests::ExportRequest::DEFAULTS[:export_all] if export_all_sheets_flag.nil?

export_all_sheets_flag
end

def sanitized_verbose(verbose_flag)
return Requests::ExportRequest::DEFAULTS[:verbose] if verbose_flag.nil?

verbose_flag
end
end
end
Expand Down
32 changes: 14 additions & 18 deletions lib/ad_localize/requests/export_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ExportRequest
bypass_empty_values: false,
csv_paths: [],
merge_policy: Interactors::MergeWordings::DEFAULT_POLICY,
output_path: 'exports',
output_path: Pathname.new('exports'),
spreadsheet_id: nil,
sheet_ids: %w[0],
export_all: false,
Expand All @@ -31,23 +31,19 @@ class ExportRequest
:verbose
)

def initialize(**args)
@locales = Array(value_for(optional: args[:locales], default_value: DEFAULTS[:locales]))
@bypass_empty_values = value_for(optional: args[:bypass_empty_values], default_value: DEFAULTS[:bypass_empty_values])
@csv_paths = Array(value_for(optional: args[:csv_paths], default_value: DEFAULTS[:csv_paths]))
@merge_policy = value_for(optional: args[:merge_policy], default_value: DEFAULTS[:merge_policy])
@output_path = Pathname.new(value_for(optional: args[:output_path], default_value: DEFAULTS[:output_path]))
raw_platforms = value_for(optional: args[:platforms], default_value: DEFAULTS[:platforms])
@platforms = Array(raw_platforms) & Entities::Platform::SUPPORTED_PLATFORMS
@spreadsheet_id = value_for(optional: args[:spreadsheet_id], default_value: DEFAULTS[:spreadsheet_id])
@sheet_ids = Array(value_for(optional: args[:sheet_ids], default_value: DEFAULTS[:sheet_ids]))
@export_all = value_for(optional: args[:export_all], default_value: DEFAULTS[:export_all])
@verbose = value_for(optional: args[:verbose], default_value: DEFAULTS[:verbose])
@downloaded_csvs = Array(value_for(optional: args[:downloaded_csvs], default_value: DEFAULTS[:downloaded_csvs]))
end

def value_for(optional:, default_value:)
optional.presence || default_value
def initialize(args)
apply_defaults
@locales = args[:locales] unless args[:locales].blank?
@bypass_empty_values = args[:bypass_empty_values] unless args[:bypass_empty_values].blank?
@csv_paths = Array(args[:csv_paths]).map { |path| Pathname.new(path) } unless args[:csv_paths].blank?
@merge_policy = args[:merge_policy] unless args[:merge_policy].blank?
@output_path = Pathname.new(args[:output_path]) unless args[:output_path].blank?
@platforms = Array(args[:platforms]) & DEFAULTS[:platforms] unless args[:platforms].blank?
@spreadsheet_id = args[:spreadsheet_id] unless args[:spreadsheet_id].blank?
@sheet_ids = Array(args[:sheet_ids]) unless args[:sheet_ids].blank?
@export_all = args[:export_all] unless args[:export_all].blank?
@verbose = args[:verbose] unless args[:verbose].blank?
@downloaded_csvs = Array(args[:downloaded_csvs]) unless args[:downloaded_csvs].blank?
end

def has_sheets?
Expand Down
12 changes: 6 additions & 6 deletions test/requests/export_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ExportRequestTest < TestCase
assert_empty request.csv_paths

request = ExportRequest.new(csv_paths: %w[csv_1 csv_2])
assert_equal %w[csv_1 csv_2], request.csv_paths
assert_equal [Pathname.new('csv_1'), Pathname.new('csv_2')], request.csv_paths

request = ExportRequest.new(csv_paths: nil)
assert_empty request.csv_paths
Expand Down Expand Up @@ -75,17 +75,17 @@ class ExportRequestTest < TestCase
request = ExportRequest.new(platforms: '')
assert_equal Entities::Platform::SUPPORTED_PLATFORMS, request.platforms

request = ExportRequest.new(platforms: %w(ios android))
assert_equal %w(ios android), request.platforms
request = ExportRequest.new(platforms: %w[ios android])
assert_equal %w[ios android], request.platforms

request = ExportRequest.new(platforms: 'json')
assert_equal %w(json), request.platforms
assert_equal %w[json], request.platforms

request = ExportRequest.new(platforms: nil)
assert_equal Entities::Platform::SUPPORTED_PLATFORMS, request.platforms

request = ExportRequest.new(platforms: %w(foo bar ios))
assert_equal %w(ios), request.platforms
request = ExportRequest.new(platforms: %w[foo bar ios])
assert_equal %w[ios], request.platforms
end

test 'should get spreadsheet id' do
Expand Down
5 changes: 3 additions & 2 deletions test/sanitizers/ios_to_android_sanitizer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class IOSToAndroidSanitizerTest < TestCase
end

test 'should map list of autorized characters for android' do
password_specialchar_error = "Caractères spéciaux autorisés : - / : ; ( ) € & @ . , ? ! ' [ ] { } # % ^ * + = _ | ~ < > $ £ ¥ ` ° \""
password_specialchar_error = "Caractères spéciaux autorisés - / : ; ( ) € & @ . , ? ! ' [ ] { } # % ^ * + = _ | ~ < > $ £ ¥ ` ° \""
sanitized_value = IOSToAndroidSanitizer.new.sanitize(value: password_specialchar_error)
expected_value = "\"Caractères spéciaux autorisés : - / : ; ( ) € \\&#38; @ . , ? ! \\&#39; [ ] { } # % ^ * + = _ | ~ \\&lt; \\&gt; $ £ ¥ ` ° \\&#34;\""
expected_value = "\"Caractères spéciaux autorisés " \
"- / : ; ( ) € \\&#38; @ . , ? ! \\&#39; [ ] { } # % ^ * + = _ | ~ \\&lt; \\&gt; $ £ ¥ ` ° \\&#34;\""
assert_equal expected_value, sanitized_value
end

Expand Down

0 comments on commit 6495206

Please sign in to comment.