Class: RemoteTranslationLoader::Fetchers::BaseFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_translation_loader/fetchers/base_fetcher.rb

Direct Known Subclasses

FileFetcher, HttpFetcher, S3Fetcher

Instance Method Summary collapse

Instance Method Details

#fetch(_source) ⇒ Object

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/remote_translation_loader/fetchers/base_fetcher.rb', line 9

def fetch(_source)
  raise NotImplementedError, "Subclasses must implement the `fetch` method"
end

#format_for(source) ⇒ Object

Infers :json vs :yaml from a source's file extension. Defaults to :yaml (covers .yml/.yaml and extension-less sources like plain URLs).



26
27
28
# File 'lib/remote_translation_loader/fetchers/base_fetcher.rb', line 26

def format_for(source)
  File.extname(source.to_s).delete_prefix(".").downcase == "json" ? :json : :yaml
end

#parse(content, format: :yaml) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/remote_translation_loader/fetchers/base_fetcher.rb', line 13

def parse(content, format: :yaml)
  case format
  when :json
    JSON.parse(content)
  else
    YAML.safe_load(content, aliases: true)
  end
rescue Psych::SyntaxError, Psych::DisallowedClass, JSON::ParserError => e
  raise ParseError, "Failed to parse content: #{e.message}"
end