Skip to content

Commit

Permalink
Use BCP 47 library to resolve language codes to labels
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo committed Nov 29, 2023
1 parent 905b756 commit 31d19ec
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
22 changes: 8 additions & 14 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config --auto-gen-only-exclude`
# on 2023-11-21 19:15:26 UTC using RuboCop version 1.57.2.
# on 2023-11-29 18:35:51 UTC using RuboCop version 1.57.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -18,11 +18,10 @@ Capybara/SpecificFinders:
Exclude:
- 'spec/features/embed_this_panel_spec.rb'

# Offense count: 2
# Offense count: 1
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes, Max.
Metrics/AbcSize:
Exclude:
- 'lib/bcp47/registry.rb'
- 'lib/embed/media_duration.rb'

# Offense count: 1
Expand All @@ -31,26 +30,23 @@ Metrics/ClassLength:
Exclude:
- 'lib/embed/request.rb'

# Offense count: 2
# Offense count: 1
# Configuration parameters: AllowedMethods, AllowedPatterns, Max.
Metrics/CyclomaticComplexity:
Exclude:
- 'lib/bcp47/parser.rb'
- 'lib/embed/media_duration.rb'

# Offense count: 7
# Offense count: 5
# Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Exclude:
- 'app/models/embed/purl/file_xml_deserializer.rb'
- 'app/models/embed/was_time_map.rb'
- 'app/viewers/embed/viewer/geo.rb'
- 'app/viewers/embed/viewer/m3_viewer.rb'
- 'lib/bcp47/parser.rb'
- 'lib/bcp47/record.rb'
- 'lib/embed/request.rb'

# Offense count: 16
# Offense count: 15
RSpec/AnyInstance:
Exclude:
- 'spec/components/embed/was_seed_component_spec.rb'
Expand Down Expand Up @@ -91,7 +87,7 @@ RSpec/IteratedExpectation:
Exclude:
- 'spec/models/embed/was_time_map_spec.rb'

# Offense count: 38
# Offense count: 33
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Expand All @@ -104,7 +100,6 @@ RSpec/MessageSpies:
- 'spec/lib/embed/viewer/common_viewer_spec.rb'
- 'spec/lib/embed/viewer/file_spec.rb'
- 'spec/lib/embed/viewer/m3_viewer_spec.rb'
- 'spec/lib/embed/viewer/media_spec.rb'
- 'spec/lib/embed/viewer/was_seed_spec.rb'
- 'spec/models/embed/was_time_map_spec.rb'

Expand Down Expand Up @@ -137,18 +132,17 @@ RSpec/SpecFilePathFormat:
Exclude:
- 'spec/lib/embed/viewer/virtex_3d_viewer_spec.rb'

# Offense count: 15
# Offense count: 10
RSpec/StubbedMock:
Exclude:
- 'spec/lib/embed/request_spec.rb'
- 'spec/lib/embed/response_spec.rb'
- 'spec/lib/embed/viewer/common_viewer_spec.rb'
- 'spec/lib/embed/viewer/file_spec.rb'
- 'spec/lib/embed/viewer/m3_viewer_spec.rb'
- 'spec/lib/embed/viewer/media_spec.rb'
- 'spec/lib/embed/viewer/was_seed_spec.rb'

# Offense count: 66
# Offense count: 64
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false
12 changes: 1 addition & 11 deletions app/models/embed/purl/resource_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ module Embed
class Purl
class ResourceFile
NON_DOWNLOADABLE_ROLES = %w[thumbnail].freeze
# Hardcoding some language code to label mappings, based on the mappings we currently need.
# This approach should be revisited once we have more robust BCP 47 code to label mapping integrated.
FILE_LANGUAGE_CAPTION_LABELS = {
'en' => 'English',
'ru' => 'Russian',
'de' => 'German',
'et' => 'Estonian',
'lv' => 'Latvian',
'es' => 'Spanish'
}.freeze

def initialize(attributes = {})
self.attributes = attributes
Expand Down Expand Up @@ -58,7 +48,7 @@ def label_or_filename
end

def language_label
FILE_LANGUAGE_CAPTION_LABELS.fetch(language_code, 'Unknown')
Bcp47::Registry.resolve(language_code) || 'Unknown'
end

def caption_label
Expand Down
3 changes: 1 addition & 2 deletions lib/bcp47/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def initialize(attributes = [])
end

define_method("#{field}=") do |new_value|
current_value = public_send(field)
case current_value
case (current_value = public_send(field))
when NilClass
instance_variable_set("@#{field}", new_value)
when String
Expand Down
2 changes: 1 addition & 1 deletion lib/bcp47/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.file_date
records.find(&:file_date).file_date
end

def self.resolve(string) # rubocop:disable Metrics/MethodLength
def self.resolve(string) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
string
.split(CODE_DELIMITER)
.filter_map
Expand Down

0 comments on commit 31d19ec

Please sign in to comment.