Class: JsonRefs::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/json_refs/loader.rb

Defined Under Namespace

Classes: Json, Yaml

Constant Summary collapse

EXTENSIONS =
{
  'json' => JsonRefs::Loader::Json,
  'yaml' => JsonRefs::Loader::Yaml,
  'yml' => JsonRefs::Loader::Yaml,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handle(filename) ⇒ Object



25
26
27
# File 'lib/json_refs/loader.rb', line 25

def self.handle(filename)
  new.handle(filename)
end

Instance Method Details

#handle(filename) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/json_refs/loader.rb', line 29

def handle(filename)
  f = open(filename)
  @body = f.read
  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) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/json_refs/loader.rb', line 37

def json?(file_body)
  JSON.load(file_body)
  true
rescue JSON::ParserError => e
  false
end