Class: Yoda::Server::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/server/file_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileStore

Returns a new instance of FileStore.



6
7
8
# File 'lib/yoda/server/file_store.rb', line 6

def initialize
  @cache = Concurrent::Map.new
end

Class Method Details

.path_of_uri(uri_string) ⇒ Object

Parameters:

  • uri_string (String)


41
42
43
44
45
46
47
# 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) ⇒ Object

Parameters:

  • path (String)


36
37
38
# File 'lib/yoda/server/file_store.rb', line 36

def self.uri_of_path(path)
  "file://#{File.expand_path(path)}"
end

Instance Method Details

#get(uri_string) ⇒ String?

Parameters:

  • uri_string (String)

Returns:

  • (String, nil)


12
13
14
# File 'lib/yoda/server/file_store.rb', line 12

def get(uri_string)
  @cache[uri_string]
end

#load(uri_string) ⇒ Object

Parameters:

  • uri_string (String)


24
25
26
# 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) ⇒ Boolean

Parameters:

  • uri_string (String)

Returns:

  • (Boolean)


50
51
52
53
54
# 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) ⇒ Object

Parameters:

  • uri_string (String)


29
30
31
32
33
# 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) ⇒ Object

Parameters:

  • uri_string (String)
  • text (String)


18
19
20
21
# 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