Class: Filezor::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/filezor/client.rb

Defined Under Namespace

Classes: InvalidArgument, Unauthorized

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, password, options = {}) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
# File 'lib/filezor/client.rb', line 22

def initialize(host, port, password, options = {})
  @root = "http://admin:#{password}@#{host}:#{port}"
  @options ||= stringify_keys(options)
  @options.update JSON.parse(post("info", options.to_json, "/"))
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/filezor/client.rb', line 12

def options
  @options
end

Class Method Details

.last_exceptionObject



14
15
16
# File 'lib/filezor/client.rb', line 14

def self.last_exception
  @e
end

.last_exception=(e) ⇒ Object



18
19
20
# File 'lib/filezor/client.rb', line 18

def self.last_exception=(e)
  @e = e
end

Instance Method Details

#app_rootObject



87
88
89
# File 'lib/filezor/client.rb', line 87

def app_root
  options["app_root"] || ""
end

#cached(*files) ⇒ Object



67
68
69
# File 'lib/filezor/client.rb', line 67

def cached(*files)
  JSON.parse(post("cached", files.flatten.map{|f| f.md5 }.to_json, "/"))
end

#caching?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/filezor/client.rb', line 91

def caching?
  options["caching"]
end

#get(path, prefix = "/file/") ⇒ Object



71
72
73
74
75
# File 'lib/filezor/client.rb', line 71

def get(path, prefix = "/file/")
  _get("#{http_root}#{prefix}#{path}")
rescue RestClient::Unauthorized
  raise Unauthorized
end

#http_rootObject



83
84
85
# File 'lib/filezor/client.rb', line 83

def http_root
  "#{@root}#{app_root}"
end

#pingObject



63
64
65
# File 'lib/filezor/client.rb', line 63

def ping
  get("ping", "/").code == 200
end

#post(path, params, prefix = "/file/") ⇒ Object



77
78
79
80
81
# File 'lib/filezor/client.rb', line 77

def post(path, params, prefix = "/file/")
  _post("#{http_root}#{prefix}#{path}", params)
rescue RestClient::Unauthorized
  raise Unauthorized
end

#put(*files) ⇒ Object

Simple file upload



50
51
52
53
54
55
56
57
# File 'lib/filezor/client.rb', line 50

def put(*files)
  test(files)
  params = files.flatten.inject({}) do |memo, file|
    memo[file.path] = file.tempfile
    memo
  end
  post("", params)
end

#set_options(options = {}) ⇒ Object



59
60
61
# File 'lib/filezor/client.rb', line 59

def set_options(options = {})
  @options = JSON.parse(post("info", options.to_json, "/"))
end

#sync(*files) ⇒ Object

This method acts like an rsync –delete. It deletes all files in the root, and then puts the new files in. TODO: make atomic



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/filezor/client.rb', line 30

def sync(*files)
  options = files.last.is_a?(Hash) ? files.pop : {}    
  files = files.flatten.uniq
  test(files)
  # 
  # files.each do |f|
  #   f.path = File.join(root, f.path)
  # end
  cached_md5s = caching? ? Set.new(cached(files)) : []
  params = files.inject({}) do |memo, file|
    memo[file.path] = cached_md5s.include?(file.md5) ? file.md5 : file.tempfile
    memo
  end
  
  params["--delete"] = options[:delete] if options[:delete]
  params["--root"] = options[:root] if options[:root]
  post("", params)
end