Class: Jerakia::Datasource::File

Inherits:
Instance
  • Object
show all
Defined in:
lib/jerakia/datasource/file.rb,
lib/jerakia/datasource/file/json.rb,
lib/jerakia/datasource/file/yaml.rb

Overview

Jerakia::Datasource.define(:file) do

Defined Under Namespace

Modules: Json, Yaml

Instance Attribute Summary

Attributes inherited from Instance

#options, #request, #response

Instance Method Summary collapse

Methods inherited from Instance

#answer, feature, #features?, #initialize, option, set_options, validate_options

Constructor Details

This class inherits a constructor from Jerakia::Datasource::Instance

Instance Method Details

#cacheObject


29
30
31
# File 'lib/jerakia/datasource/file.rb', line 29

def cache
  Jerakia::Cache::File
end

#extensionObject


25
26
27
# File 'lib/jerakia/datasource/file.rb', line 25

def extension
  options[:extension] || format_handler::EXTENSION
end

#format_handlerObject


20
21
22
23
# File 'lib/jerakia/datasource/file.rb', line 20

def format_handler
  format = options[:format]
  eval "Jerakia::Datasource::File::#{format.to_s.capitalize}"
end

#get_file(diskname) ⇒ Object


38
39
40
# File 'lib/jerakia/datasource/file.rb', line 38

def get_file(diskname)
  ::File.read(diskname) if ::File.exists?(diskname)
end

#get_file_with_cache(diskname) ⇒ Object


33
34
35
36
# File 'lib/jerakia/datasource/file.rb', line 33

def get_file_with_cache(diskname)
  Jerakia.log.debug("Querying cache for file #{diskname}")
  cache.retrieve(diskname)
end

#list_fragments(prefix, extension) ⇒ Object


42
43
44
# File 'lib/jerakia/datasource/file.rb', line 42

def list_fragments(prefix, extension)
  Dir["#{prefix}.d/*.#{extension}"] if ::File.directory?("#{prefix}.d")
end

#load_format_handlerObject


14
15
16
17
18
# File 'lib/jerakia/datasource/file.rb', line 14

def load_format_handler
  format = options[:format]
  require "jerakia/datasource/file/#{format.to_s}"
  eval "extend Jerakia::Datasource::File::#{format.to_s.capitalize}"
end

#lookupObject


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jerakia/datasource/file.rb', line 81

def lookup
  Jerakia.log.debug("Searching key #{request.key} from file format #{options[:format]}")

  load_format_handler
  paths=options[:searchpath].flatten

  answer do |response|
    path = paths.shift
    break unless path
    data = read_from_file(path)
    if data.has_key?(request.key)
      response.submit data[request.key]
    end
  end
end

#read_from_file(fname) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jerakia/datasource/file.rb', line 46

def read_from_file(fname)
  docroot = options[:docroot]
  namespace = request.namespace
  cached = options[:enable_caching]
  
  fpath = []
  fpath << docroot unless fname[0] == '/'
  fpath << [fname, namespace]

  diskname_prefix = ::File.join(fpath.flatten).gsub(/\/$/, '').to_s
  diskname = "#{diskname_prefix}.#{extension}"

  files = [diskname]
  files << list_fragments(diskname_prefix, extension)

  raw_data = ''

  files.flatten.compact.each do |f|
    Jerakia.log.debug("read_from_file()  #{f}")
    file_contents = get_file_with_cache(f)
    if cached
      file_contents = get_file_with_cache(f)
    else
      file_content = get_file(f)
    end
    raw_data << file_contents if file_contents
  end

  begin
    convert(raw_data)
  rescue Jerakia::FileParseError => e
    raise Jerakia::FileParseError, "While parsing #{diskname}: #{e.message}"
  end
end