class Yoda::Server::FileStore

Public Class Methods

new() click to toggle source
# File lib/yoda/server/file_store.rb, line 6
def initialize
  @cache = Concurrent::Map.new
end
path_of_uri(uri_string) click to toggle source

@param uri_string [String]

# File lib/yoda/server/file_store.rb, line 41
def self.path_of_uri(uri_string)
  uri = URI.parse(uri_string)
  return nil unless uri.scheme == 'file'
  uri.path
rescue URI::InvalidURIError
  nil
end
uri_of_path(path) click to toggle source

@param path [String]

# File lib/yoda/server/file_store.rb, line 36
def self.uri_of_path(path)
  "file://#{File.expand_path(path)}"
end

Public Instance Methods

get(uri_string) click to toggle source

@param uri_string [String] @return [String, nil]

# File lib/yoda/server/file_store.rb, line 12
def get(uri_string)
  @cache[uri_string]
end
load(uri_string) click to toggle source

@param uri_string [String]

# File lib/yoda/server/file_store.rb, line 24
def load(uri_string)
  store(uri_string, read(uri_string))
end
program_file_uri?(uri_string) click to toggle source

@param uri_string [String]

# File lib/yoda/server/file_store.rb, line 50
def program_file_uri?(uri_string)
  %w(.c .rb).include?(File.extname(URI.parse(uri_string).path))
rescue URI::InvalidURIError => _e
  false
end
read(uri_string) click to toggle source

@param uri_string [String]

# File lib/yoda/server/file_store.rb, line 29
def read(uri_string)
  path = self.class.path_of_uri(uri_string)
  fail ArgumentError unless path
  File.read(path)
end
store(uri_string, text) click to toggle source

@param uri_string [String] @param text [String]

# File lib/yoda/server/file_store.rb, line 18
def store(uri_string, text)
  return unless program_file_uri?(uri_string)
  @cache[uri_string] = text
end