Sha256: 736432b9137d9041937bb1c346575a77ff8aebcc80d1828fc400c5de5d189b1e

Contents?: true

Size: 782 Bytes

Versions: 2

Compression:

Stored size: 782 Bytes

Contents

# frozen_string_literal: true

module Zilla::LoaderFactory
  class UnknownLoader < Zilla::Error; end

  class << self
    def build(input, &block)
      case input
      when Hash
        Zilla::Loaders::Hash
      when String
        for_string(input)
      end.new(input, faraday_config_block: block)
    rescue NoMethodError
      raise UnknownLoader, "cannot find loader for #{input.inspect}"
    end

    private

    def for_string(string)
      if File.exist?(string)
        Zilla::Loaders::File
      elsif ["http", "https"].any? { string.start_with?(_1) }
        Zilla::Loaders::HTTP
      elsif json?(string)
        Zilla::Loaders::String
      end
    end

    def json?(string)
      Oj.load(string)
      true
    rescue Oj::ParseError
      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zilla-0.1.3 lib/zilla/loader_factory.rb
zilla-0.1.2 lib/zilla/loader_factory.rb