Class: DesignShell::SiteClient
- Inherits:
-
Object
- Object
- DesignShell::SiteClient
show all
- Defined in:
- lib/designshell/site_client.rb
Overview
Constant Summary
collapse
- DAV_METHODS =
[:find]
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#delete(aPath) ⇒ Object
-
#delete_files(aPaths, aFromPath = nil, aToPath = nil) ⇒ Object
-
#deploy_status ⇒ Object
-
#deploy_status=(aObject) ⇒ Object
-
#ensure_folder_path(aPath) ⇒ Object
-
#exists?(aPath) ⇒ Boolean
-
#full_path(aRelativePath) ⇒ Object
-
#get_file(aRemotePath, aLocalFile) ⇒ Object
-
#get_string(aPath) ⇒ Object
-
#initialize(aConfig) ⇒ SiteClient
constructor
A new instance of SiteClient.
-
#list_files(aPath = nil, aRecursive = false) ⇒ Object
-
#ls(aPath = nil, aRecursive = false) ⇒ Object
-
#method_missing(sym, *args, &block) ⇒ Object
-
#mkdir(aPath) ⇒ Object
-
#put_file(aLocalFile, aRemotePath, aEnsureFolder = true) ⇒ Object
-
#put_string(aPath, aString) ⇒ Object
-
#upload_files(aLocalDir, aFiles, aFromPath = nil, aToPath = nil) ⇒ Object
Constructor Details
#initialize(aConfig) ⇒ SiteClient
Returns a new instance of SiteClient.
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/designshell/site_client.rb', line 11
def initialize(aConfig)
@server_path = MiscUtils.remove_slash(aConfig[:site_url])
site_username = aConfig[:site_username]
site_password = aConfig[:site_password]
@dav = Net::DAV.new(MiscUtils.append_slash(@server_path), :curl => false)
@dav.verify_server = false
@dav.credentials(site_username,site_password)
@deploy_status_file = '/content/.deploy-status.txt'
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/designshell/site_client.rb', line 25
def method_missing(sym, *args, &block)
if @dav && DAV_METHODS.include?(sym)
@dav.send sym, *args, &block
else
super
end
end
|
Instance Attribute Details
#deploy_status_file ⇒ Object
Returns the value of attribute deploy_status_file.
9
10
11
|
# File 'lib/designshell/site_client.rb', line 9
def deploy_status_file
@deploy_status_file
end
|
Instance Method Details
#delete(aPath) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/designshell/site_client.rb', line 78
def delete(aPath)
begin
@dav.delete(full_path(aPath))
rescue Net::HTTPServerException => e
e.response.is_a?(Net::HTTPNotFound) ? nil : raise
end
end
|
#delete_files(aPaths, aFromPath = nil, aToPath = nil) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/designshell/site_client.rb', line 120
def delete_files(aPaths,aFromPath=nil,aToPath=nil)
if (aFromPath && aToPath)
aPaths.each do |p|
delete(MiscUtils.path_rebase(p,aFromPath,aToPath))
end
else
aPaths.each do |p|
delete(p)
end
end
end
|
#deploy_status ⇒ Object
86
87
88
89
|
# File 'lib/designshell/site_client.rb', line 86
def deploy_status
s = get_string(deploy_status_file)
s ? JSON.parse(s) : nil
end
|
#deploy_status=(aObject) ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/designshell/site_client.rb', line 91
def deploy_status=(aObject)
if aObject
s = JSON.generate(aObject)
put_string(deploy_status_file,s)
else
delete deploy_status_file
end
aObject
end
|
#ensure_folder_path(aPath) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/designshell/site_client.rb', line 66
def ensure_folder_path(aPath)
path_parts = aPath.bite('/').chomp('/').split('/')
last_part = path_parts.length-1
existing_part = nil
last_part.downto(0) do |i|
existing_part = i if !existing_part && exists?(path = '/'+path_parts[0..i].join('/'))
end
(existing_part ? existing_part+1 : 0).upto(last_part) do |i|
mkdir('/'+path_parts[0..i].join('/'))
end
end
|
#exists?(aPath) ⇒ Boolean
132
133
134
|
# File 'lib/designshell/site_client.rb', line 132
def exists?(aPath)
@dav.exists? full_path(aPath)
end
|
#full_path(aRelativePath) ⇒ Object
33
34
35
|
# File 'lib/designshell/site_client.rb', line 33
def full_path(aRelativePath)
File.join(@server_path, URI.escape(aRelativePath))
end
|
#get_file(aRemotePath, aLocalFile) ⇒ Object
107
108
109
110
|
# File 'lib/designshell/site_client.rb', line 107
def get_file(aRemotePath, aLocalFile)
s = get_string(aRemotePath)
MiscUtils.string_to_file(s,aLocalFile)
end
|
#get_string(aPath) ⇒ Object
52
53
54
55
56
57
58
59
60
|
# File 'lib/designshell/site_client.rb', line 52
def get_string(aPath)
begin
@dav.get(full_path(aPath))
rescue Net::HTTPServerException => e
e.response.is_a?(Net::HTTPNotFound) ? nil : raise
rescue Net::HTTPError => e
e.message.index('404') ? nil : raise
end
end
|
#list_files(aPath = nil, aRecursive = false) ⇒ Object
46
47
48
49
50
|
# File 'lib/designshell/site_client.rb', line 46
def list_files(aPath=nil,aRecursive=false)
result = ls(aPath,aRecursive)
result.delete_if {|f| f.ends_with? '/'}
result
end
|
#ls(aPath = nil, aRecursive = false) ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/designshell/site_client.rb', line 37
def ls(aPath=nil,aRecursive=false)
result = []
path = MiscUtils.append_slash(full_path(aPath||''))
@dav.find(path,:recursive=>aRecursive,:suppress_errors=>false) do | item |
result << item.url.to_s.bite(path)
end
result
end
|
#mkdir(aPath) ⇒ Object
136
137
138
|
# File 'lib/designshell/site_client.rb', line 136
def mkdir(aPath)
@dav.mkdir full_path(aPath)
end
|
#put_file(aLocalFile, aRemotePath, aEnsureFolder = true) ⇒ Object
101
102
103
104
105
|
# File 'lib/designshell/site_client.rb', line 101
def put_file(aLocalFile, aRemotePath, aEnsureFolder=true)
s = MiscUtils.string_from_file(aLocalFile)
ensure_folder_path(File.dirname(aRemotePath)) if aEnsureFolder
put_string(aRemotePath,s)
end
|
#put_string(aPath, aString) ⇒ Object
62
63
64
|
# File 'lib/designshell/site_client.rb', line 62
def put_string(aPath,aString)
@dav.put_string(full_path(aPath),aString)
end
|
#upload_files(aLocalDir, aFiles, aFromPath = nil, aToPath = nil) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/designshell/site_client.rb', line 112
def upload_files(aLocalDir,aFiles,aFromPath=nil,aToPath=nil)
aFiles.each do |f|
to = f
to = MiscUtils.path_rebase(to,aFromPath,aToPath) if aFromPath && aToPath
put_file(File.join(aLocalDir,f),to)
end
end
|