Class: State
- Inherits:
-
Object
- Object
- State
- Defined in:
- lib/droxi/state.rb
Overview
Encapsulates the session state of the client.
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Hashof remote file paths to cached file metadata. -
#exit_requested ⇒ Object
trueif the client has requested to quit,falseotherwise. -
#local_oldpwd ⇒ Object
The previous local working directory path.
-
#oldpwd ⇒ Object
readonly
The previous remote working directory path.
-
#pwd ⇒ Object
The remote working directory path.
Instance Method Summary collapse
-
#contents(path) ⇒ Object
Return an
Arrayof paths of files in a Dropbox directory. -
#directory?(path) ⇒ Boolean
Return
trueif the Dropbox path is a directory,falseotherwise. -
#expand_patterns(patterns) ⇒ Object
Expand an
Arrayof file globs into an anArrayof Dropbox file paths and return the result. -
#initialize(client) ⇒ State
constructor
Return a new application state that uses the given client.
-
#metadata(path) ⇒ Object
Return a
Hashof the Dropbox metadata for a file, ornilif the file does not exist. -
#resolve_path(path) ⇒ Object
Expand a Dropbox file path and return the result.
Constructor Details
#initialize(client) ⇒ State
Return a new application state that uses the given client. Starts at the Dropbox root and with an empty cache.
23 24 25 26 27 28 29 30 |
# File 'lib/droxi/state.rb', line 23 def initialize(client) @cache = {} @client = client @exit_requested = false @pwd = '/' @oldpwd = Settings[:oldpwd] || '/' @local_oldpwd = Dir.pwd end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Hash of remote file paths to cached file metadata.
7 8 9 |
# File 'lib/droxi/state.rb', line 7 def cache @cache end |
#exit_requested ⇒ Object
true if the client has requested to quit, false otherwise.
19 20 21 |
# File 'lib/droxi/state.rb', line 19 def exit_requested @exit_requested end |
#local_oldpwd ⇒ Object
The previous local working directory path.
16 17 18 |
# File 'lib/droxi/state.rb', line 16 def local_oldpwd @local_oldpwd end |
#oldpwd ⇒ Object (readonly)
The previous remote working directory path.
13 14 15 |
# File 'lib/droxi/state.rb', line 13 def oldpwd @oldpwd end |
#pwd ⇒ Object
The remote working directory path.
10 11 12 |
# File 'lib/droxi/state.rb', line 10 def pwd @pwd end |
Instance Method Details
#contents(path) ⇒ Object
Return an Array of paths of files in a Dropbox directory.
57 58 59 60 61 62 63 |
# File 'lib/droxi/state.rb', line 57 def contents(path) (path) path = "#{path}/".sub('//', '/') @cache.keys.select do |key| key.start_with?(path) && key != path && !key.sub(path, '').include?('/') end end |
#directory?(path) ⇒ Boolean
Return true if the Dropbox path is a directory, false otherwise.
66 67 68 69 70 |
# File 'lib/droxi/state.rb', line 66 def directory?(path) path = path.sub('//', '/') (File.dirname(path)) @cache.include?(path) && @cache[path]['is_dir'] end |
#expand_patterns(patterns) ⇒ Object
Expand an Array of file globs into an an Array of Dropbox file paths and return the result.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/droxi/state.rb', line 92 def (patterns) patterns.map do |pattern| final_pattern = resolve_path(pattern) matches = [] @client.(File.dirname(final_pattern))['contents'].each do |data| path = data['path'] matches << path if File.fnmatch(final_pattern, path) end if matches.empty? [final_pattern] else matches end end.flatten end |
#metadata(path) ⇒ Object
Return a Hash of the Dropbox metadata for a file, or nil if the file does not exist.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/droxi/state.rb', line 34 def (path) tokens = path.split('/').drop(1) for i in 0..tokens.length partial_path = '/' + tokens.take(i).join('/') unless have_all_info_for(partial_path) begin data = @cache[partial_path] = @client.(partial_path) rescue DropboxError return nil end if data.include?('contents') data['contents'].each do |datum| @cache[datum['path']] = datum end end end end @cache[path] end |
#resolve_path(path) ⇒ Object
Expand a Dropbox file path and return the result.
80 81 82 83 84 85 86 87 88 |
# File 'lib/droxi/state.rb', line 80 def resolve_path(path) path = "#{@pwd}/#{path}" unless path.start_with?('/') path.gsub!('//', '/') while path.sub!(/\/([^\/]+?)\/\.\./, '') end path.chomp!('/') path = '/' if path.empty? path end |