Class: Yoda::Server::Session::FileStore

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileStore

Returns a new instance of FileStore.



53
54
55
# File 'lib/yoda/server/session.rb', line 53

def initialize
  @cache = {}
end

Class Method Details

.path_of_uri(uri_string) ⇒ Object

Parameters:

  • uri_string (String)


88
89
90
91
92
93
94
# File 'lib/yoda/server/session.rb', line 88

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)


83
84
85
# File 'lib/yoda/server/session.rb', line 83

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)


59
60
61
# File 'lib/yoda/server/session.rb', line 59

def get(uri_string)
  @cache[uri_string]
end

#load(uri_string) ⇒ Object

Parameters:

  • uri_string (String)


71
72
73
# File 'lib/yoda/server/session.rb', line 71

def load(uri_string)
  store(uri_string, read(uri_string))
end

#program_file_uri?(uri_string) ⇒ Boolean

Parameters:

  • uri_string (String)

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/yoda/server/session.rb', line 97

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)


76
77
78
79
80
# File 'lib/yoda/server/session.rb', line 76

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)


65
66
67
68
# File 'lib/yoda/server/session.rb', line 65

def store(uri_string, text)
  return unless program_file_uri?(uri_string)
  @cache[uri_string] = text
end