Class: RemoteTranslationLoader::SourceResolver

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

Overview

Given a plain source string, resolves which Fetcher should handle it and what key/path/url to pass to that fetcher's #fetch. This lets a single Loader mix HTTP, local file, and S3 sources in one call without the caller having to construct fetchers manually.

s3://my-bucket/path/to/en.yml -> S3Fetcher
http(s)://...                 -> HttpFetcher
anything else                 -> FileFetcher

Constant Summary collapse

S3_URI =
%r{\As3://(?<bucket>[^/]+)/(?<key>.+)\z}.freeze
HTTP_URI =
%r{\Ahttps?://}i.freeze

Instance Method Summary collapse

Constructor Details

#initializeSourceResolver

Returns a new instance of SourceResolver.



16
17
18
19
20
# File 'lib/remote_translation_loader/source_resolver.rb', line 16

def initialize
  @s3_fetchers = {}
  @http_fetcher = Fetchers::HttpFetcher.new
  @file_fetcher = Fetchers::FileFetcher.new
end

Instance Method Details

#resolve(source) ⇒ Object

Returns [fetcher, key] where fetcher.fetch(key) retrieves the content.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/remote_translation_loader/source_resolver.rb', line 23

def resolve(source)
  str = source.to_s

  if (match = S3_URI.match(str))
    [s3_fetcher_for(match[:bucket]), match[:key]]
  elsif HTTP_URI.match?(str)
    [@http_fetcher, str]
  else
    [@file_fetcher, str]
  end
end