class JsonRefs::Loader

Constants

EXTENSIONS

Public Class Methods

handle(filename) click to toggle source
# File lib/json_refs/loader.rb, line 25
def self.handle(filename)
  new.handle(filename)
end

Public Instance Methods

handle(filename) click to toggle source
# File lib/json_refs/loader.rb, line 29
def handle(filename)
  @body = read_reference_file(filename)
  ext = File.extname(filename)[1..-1]
  ext ||= 'json' if json?(@body)
  ext && EXTENSIONS.include?(ext) ? EXTENSIONS[ext].new.call(@body) : @body
end
json?(file_body) click to toggle source
# File lib/json_refs/loader.rb, line 36
def json?(file_body)
  JSON.load(file_body)
  true
rescue JSON::ParserError => e
  false
end

Private Instance Methods

read_reference_file(filename) click to toggle source
# File lib/json_refs/loader.rb, line 45
def read_reference_file(filename)
  if RUBY_VERSION >= '2.7.0'
    URI.open(filename, 'r', &:read)
  else
    open(filename, 'r', &:read)
  end
end