Class: KenaiTools::DownloadsClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/kenai_tools/downloads_client.rb

Overview

Path arguments to public methods of this API work with Pathname objects as well as Strings

Constant Summary collapse

CONTENT_TYPE_KENAI_ENTRIES =
"application/vnd.com.kenai.entries+json"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, project, opts = {}) ⇒ DownloadsClient

Local options are :downloads_name, :log, :cloak_password Other options such as :timeout are forwarded DownloadClient.new(“kenai.com/”, “jruby”) DownloadClient.new(“testkenai.com/”, “my-project”, :downloads_name => ‘downloads2’, :log => $stderr

:timeout => 36000, :cloak_password => 'a_secret')


23
24
25
26
27
28
29
30
# File 'lib/kenai_tools/downloads_client.rb', line 23

def initialize(site, project, opts = {})
  @site = site
  @project = project
  @specified_downloads_name = opts.delete(:downloads_name)
  @cloak_password = opts.delete(:cloak_password)
  RestClient.log = opts.delete(:log)
  @kc = KenaiClient.new(site, opts)
end

Instance Attribute Details

#cloak_passwordObject

Returns the value of attribute cloak_password.



14
15
16
# File 'lib/kenai_tools/downloads_client.rb', line 14

def cloak_password
  @cloak_password
end

#projectObject

Returns the value of attribute project.



14
15
16
# File 'lib/kenai_tools/downloads_client.rb', line 14

def project
  @project
end

Instance Method Details

#delete_feature(confirm = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/kenai_tools/downloads_client.rb', line 42

def delete_feature(confirm = nil)
  unless confirm == 'yes'
    fail "Confirm delete project downloads feature with an argument of 'yes'"
  else
    # Flush cache
    orig_downloads_name = downloads_name
    @downloads_feature = @downloads_name = nil
    @kc["projects/#{project}/features/#{orig_downloads_name}"].delete
  end
end

#downloads_featureObject

Returns the specified downloads feature if found, or a discovered one if the project only has one



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kenai_tools/downloads_client.rb', line 57

def downloads_feature
  @downloads_feature ||= begin
    project = @kc.project(@project)
    features = project && project['features']
    if downloads_features = features && features.select { |f| f['type'] == 'downloads' }
      if @specified_downloads_name
        downloads_features.detect { |f| f['name'] == @specified_downloads_name }
      elsif downloads_features.size == 1
        downloads_features.first
      else
        nil
      end
    else
      nil
    end
  end
end

#downloads_nameObject Also known as: ping



75
76
77
# File 'lib/kenai_tools/downloads_client.rb', line 75

def downloads_name
  @downloads_name ||= downloads_feature && downloads_feature['name']
end

#entry_type(path) ⇒ Object



89
90
91
92
93
# File 'lib/kenai_tools/downloads_client.rb', line 89

def entry_type(path)
  if h = entry(path)
    h['entry_type']
  end
end

#exist?(path = '/') ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/kenai_tools/downloads_client.rb', line 85

def exist?(path = '/')
  !!entry(path)
end

#get_or_createObject



32
33
34
35
36
37
38
39
40
# File 'lib/kenai_tools/downloads_client.rb', line 32

def get_or_create
  unless ping
    @specified_downloads_name = "downloads" unless @specified_downloads_name
    params = {:feature => {:name => @specified_downloads_name, :service => "downloads",
      :display_name => @specified_downloads_name.capitalize}}
    @kc["projects/#{project}/features"].post(params, :content_type => 'application/json')
  end
  downloads_name
end

#ls(path = '/') ⇒ Object



81
82
83
# File 'lib/kenai_tools/downloads_client.rb', line 81

def ls(path = '/')
  entry(path)
end

#mkdir(dir, opts = {}) ⇒ Object



171
172
173
174
175
176
# File 'lib/kenai_tools/downloads_client.rb', line 171

def mkdir(dir, opts = {})
  path = Pathname(dir)
  entry = {:display_name => path.basename}
  entry.merge(opts)
  @kc[entry_api_path(path)].put(:entry => entry)
end

#pull(remote_path, local_dest_dir = '.') ⇒ Object

Pull a remote file or directory hierarchy to the local host. Use remote_path to specify the remote file or directory hierarchy to download. If remote_path is ‘/’, download all content. Use local_dest_dir to specify the target location for the content which defaults to the current directory.

For example:

dlclient.pull('version-1.9')
dlclient.pull('version-1.9', '/tmp/project_downloads')


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kenai_tools/downloads_client.rb', line 106

def pull(remote_path, local_dest_dir = '.')
  dest = Pathname(local_dest_dir)
  fail "Destination must be a directory" unless dest.directory?
  remote_pn = clean_id_pn(remote_path)
  if ent = entry(remote_pn)
    display_name = ent['display_name']
    basename = display_name == '/' ? '.' : display_name
    local_path = dest + basename
    case ent['entry_type']
    when 'directory'
      local_path.mkdir unless local_path.exist?
      ent['children'].each do |ch|
        remote_child = remote_pn + ch['display_name']
        pull(remote_child, local_path)
      end
    when 'file'
      content = get_cloaked_url(ent['content_url'])
      local_path.open("w") { |f| f.write(content) }
    else
      puts "Warning: skipping unsupported entry type"
    end
  else
    puts "Unknown downloads entry: #{remote_pn}"
  end
end

#push(local_path, remote_dir = '/', opts = {}) ⇒ Object

Push a local file or directory hierarchy to the server. Use local_path path to specify the local file or directory to upload. If local_path is a directory that ends in ‘/’, upload the contents of that directory instead of the directory and its contents. Use remote_dir to specify the remote directory to upload to.

For example:

dlclient.push('version-1.9')
dlclient.push('dist', '/version-1.9')


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/kenai_tools/downloads_client.rb', line 143

def push(local_path, remote_dir = '/', opts = {})
  src = Pathname(local_path)
  if src.directory?
    if src.to_s.end_with?('/')
      target_dir = remote_dir
    else
      target_dir = Pathname(remote_dir) + src.basename
      mkdir(target_dir)
    end
    src.children.each do |ch|
      push(ch, target_dir)
    end
  else
    remote_id = Pathname(remote_dir) + src.basename
    entry = {:content_data => File.new(src)}.merge(opts)
    begin
    @kc[entry_api_path(remote_id)].put(:entry => entry)
    rescue => ex
      err_msg = "Error: unable to upload to target '#{remote_id}'"
      if server_msg = extract_error_message(ex)
        err_msg += ": #{server_msg}"
      end
      $stderr.puts err_msg
      raise ex
    end
  end
end

#rm(path) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/kenai_tools/downloads_client.rb', line 182

def rm(path)
  if entry_type(path) == 'directory'
    fail "Entry is a directory: #{path}"
  else
    @kc[entry_api_path(path)].delete
  end
end

#rm_r(path) ⇒ Object



178
179
180
# File 'lib/kenai_tools/downloads_client.rb', line 178

def rm_r(path)
  @kc[entry_api_path(path)].delete
end

#rmdir(path) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/kenai_tools/downloads_client.rb', line 190

def rmdir(path)
  entry = entry(path)
  if entry['entry_type'] == 'directory'
    if entry['children'].size == 0
      @kc[entry_api_path(path)].delete
    else
      fail "Directory not empty: #{path}"
    end
  else
    fail "Not a directory: #{path}"
  end
end