Class: RubyForge
- Inherits:
-
Object
- Object
- RubyForge
- Defined in:
- lib/rubyforge.rb,
lib/rubyforge/client.rb
Defined Under Namespace
Classes: Client
Constant Summary collapse
- VERSION =
:stopdoc:
'2.0.2'
- HOME =
ENV["HOME"] || ENV["HOMEPATH"] || File::("~")
- RUBYFORGE_D =
File::join HOME, ".rubyforge"
- CONFIG_F =
File::join RUBYFORGE_D, "user-config.yml"
- CONFIG =
YAML.load(config)
Instance Attribute Summary collapse
-
#autoconfig ⇒ Object
readonly
TODO: add an autoconfig method that is self-repairing, removing key checks.
-
#userconfig ⇒ Object
readonly
TODO: add an autoconfig method that is self-repairing, removing key checks.
Instance Method Summary collapse
-
#add_file(group_name, package_name, release_name, userfile) ⇒ Object
add a file to an existing release under the specified group_id, package_id, and release_id.
- #add_release(group_id, package_id, release_name, *files) ⇒ Object
- #client ⇒ Object
- #configure(opts = {}) ⇒ Object
- #create_package(group_id, package_name) ⇒ Object
- #delete_package(group_id, package_id) ⇒ Object
- #force ⇒ Object
- #get_via_rest_api(path) ⇒ Object
-
#initialize(userconfig = nil, autoconfig = nil, opts = nil) ⇒ RubyForge
constructor
A new instance of RubyForge.
-
#login ⇒ Object
These are no-ops now, but we’ll keep them here for backwards compatibility.
- #logout ⇒ Object
-
#lookup(type, val) ⇒ Object
:nodoc:.
-
#post_news(group_id, subject, body) ⇒ Object
Posts news item to
group_id
(can be name) withsubject
andbody
. -
#run(page, form, extheader = {}) ⇒ Object
:nodoc:.
- #save_autoconfig ⇒ Object
- #scrape_config ⇒ Object
- #scrape_project(project) ⇒ Object
- #setup ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(userconfig = nil, autoconfig = nil, opts = nil) ⇒ RubyForge
Returns a new instance of RubyForge.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rubyforge.rb', line 31 def initialize(userconfig=nil, autoconfig=nil, opts=nil) # def initialize(userconfig=CONFIG_F, opts={}) @userconfig, @autoconfig = userconfig, autoconfig @autoconfig ||= CONFIG["rubyforge"].dup @userconfig.merge! opts if opts @client = nil @uri = nil end |
Instance Attribute Details
#autoconfig ⇒ Object (readonly)
TODO: add an autoconfig method that is self-repairing, removing key checks
29 30 31 |
# File 'lib/rubyforge.rb', line 29 def autoconfig @autoconfig end |
#userconfig ⇒ Object (readonly)
TODO: add an autoconfig method that is self-repairing, removing key checks
29 30 31 |
# File 'lib/rubyforge.rb', line 29 def userconfig @userconfig end |
Instance Method Details
#add_file(group_name, package_name, release_name, userfile) ⇒ Object
add a file to an existing release under the specified group_id, package_id, and release_id
example :
add_file("codeforpeople", "traits", "0.8.0", "traits-0.8.0.gem")
add_file("codeforpeople", "traits", "0.8.0", "traits-0.8.0.tgz")
add_file(1024, 1242, "0.8.0", "traits-0.8.0.gem")
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/rubyforge.rb', line 260 def add_file(group_name, package_name, release_name, userfile) type_id = @userconfig["type_id"] group_id = lookup "group", group_name package_id = lookup "package", package_name release_id = (Integer === release_name) ? release_name : lookup("release", package_name)[release_name] url = "/releases/#{release_id}/files.js" userfile = open userfile, 'rb' type_id ||= userfile.path[%r|\.[^\./]+$|] type_id = (lookup "type", type_id rescue lookup "type", ".oth") processor_id = @userconfig["processor_id"] processor_id ||= "Any" processor_id = lookup "processor", processor_id form = { "file[filename]" => File.basename(userfile.path), "file[processor_id]" => processor_id, "file[type_id]" => type_id, "contents" => userfile.read } run url, form end |
#add_release(group_id, package_id, release_name, *files) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/rubyforge.rb', line 203 def add_release(group_id, package_id, release_name, *files) group_id = lookup "group", group_id package_id = lookup "package", package_id release_date = @userconfig["release_date"] release_notes = @userconfig["release_notes"] release_changes = @userconfig["release_changes"] preformatted = @userconfig["preformatted"] release_date ||= Time.now.strftime("%Y-%m-%d %H:%M") release_notes = IO::read(release_notes) if test(?e, release_notes) if release_notes release_changes = IO::read(release_changes) if test(?e, release_changes) if release_changes preformatted = preformatted ? 1 : 0 form = { "release[name]" => release_name, "release[release_date]" => release_date, "release[notes]" => release_notes, "release[changes]" => release_changes, "release[preformatted]" => preformatted, } url = "/packages/#{package_id}/releases" json = run url, form release_id = JSON.parse(json)["release_id"].to_i rescue nil unless release_id then puts json if $DEBUG raise "Couldn't get release_id, upload failed?" end # FIXME #raise "Invalid package_id #{package_id}" if html[/Invalid package_id/] #raise "You have already released this version." if html[/That filename already exists in this project/] files.each do |file| add_file(group_id, package_id, release_id, file) end package_name = @autoconfig["package_ids"].invert[package_id] raise "unknown package name for #{package_id}" if package_name.nil? @autoconfig["release_ids"][package_name] ||= {} @autoconfig["release_ids"][package_name][release_name] = release_id save_autoconfig release_id end |
#client ⇒ Object
286 287 288 289 290 291 292 293 |
# File 'lib/rubyforge.rb', line 286 def client return @client if @client @client = RubyForge::Client::new ENV["HTTP_PROXY"] @client.debug_dev = STDERR if ENV["RUBYFORGE_DEBUG"] || ENV["DEBUG"] || $DEBUG @client end |
#configure(opts = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubyforge.rb', line 46 def configure opts = {} user_path = CONFIG_F dir, file = File.split(user_path) @userconfig = if test(?e, user_path) then YAML.load_file(user_path) else CONFIG end.merge(opts) @autoconfig_path = File.join(dir, file.sub(/^user/, 'auto')) @autoconfig = if test(?e, @autoconfig_path) then YAML.load_file(@autoconfig_path) else CONFIG["rubyforge"].dup end @autoconfig["type_ids"] = CONFIG['rubyforge']['type_ids'].dup raise "no <username>" unless @userconfig["username"] raise "no <password>" unless @userconfig["password"] self end |
#create_package(group_id, package_name) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rubyforge.rb', line 160 def create_package(group_id, package_name) page = "/groups/#{group_id}/packages" group_id = lookup "group", group_id is_private = @userconfig["is_private"] is_public = is_private ? 0 : 1 form = { "package[name]" => package_name, "package[is_public]" => is_public } run page, form group_name = @autoconfig["group_ids"].invert[group_id] scrape_project(group_name) end |
#delete_package(group_id, package_id) ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/rubyforge.rb', line 192 def delete_package(group_id, package_id) group_id = lookup "group", group_id package_id = lookup "package", package_id package_name = @autoconfig["package_ids"].invert[package_id] @autoconfig["package_ids"].delete package_name @autoconfig["release_ids"].delete package_name save_autoconfig url = "/packages/#{package_id}" run url, {"_method" => "delete"} end |
#force ⇒ Object
69 70 71 |
# File 'lib/rubyforge.rb', line 69 def force @userconfig['force'] end |
#get_via_rest_api(path) ⇒ Object
112 113 114 115 116 |
# File 'lib/rubyforge.rb', line 112 def get_via_rest_api(path) url = "#{@userconfig['uri']}#{path}" puts "Hitting REST API: #{url}" if $DEBUG JSON.parse(client.get_content(url, {}, {}, @userconfig)) end |
#login ⇒ Object
These are no-ops now, but we’ll keep them here for backwards compatibility
43 |
# File 'lib/rubyforge.rb', line 43 def login ; end |
#logout ⇒ Object
44 |
# File 'lib/rubyforge.rb', line 44 def logout ; end |
#lookup(type, val) ⇒ Object
:nodoc:
303 304 305 306 307 308 309 310 |
# File 'lib/rubyforge.rb', line 303 def lookup(type, val) # :nodoc: unless Fixnum === val then key = val.to_s val = @autoconfig["#{type}_ids"][key] raise "no <#{type}_id> configured for <#{ key }>" unless val end val end |
#post_news(group_id, subject, body) ⇒ Object
Posts news item to group_id
(can be name) with subject
and body
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/rubyforge.rb', line 181 def post_news(group_id, subject, body) # TODO - what was the post_changes parameter for? form = { "news_byte[summary]" => subject, "news_byte[details]" => body } group_id = lookup "group", group_id url = "/groups/#{group_id}/news_bytes" run url, form end |
#run(page, form, extheader = {}) ⇒ Object
:nodoc:
295 296 297 298 299 300 301 |
# File 'lib/rubyforge.rb', line 295 def run(page, form, extheader={}) # :nodoc: uri = self.uri + page puts "client.post_content #{uri.inspect}, #{form.inspect}, #{extheader.inspect}" if $DEBUG response = client.post_content uri, form, extheader, @userconfig puts response if $DEBUG response end |
#save_autoconfig ⇒ Object
90 91 92 93 94 |
# File 'lib/rubyforge.rb', line 90 def save_autoconfig File.open(@autoconfig_path, "w") do |file| YAML.dump @autoconfig, file end end |
#scrape_config ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rubyforge.rb', line 96 def scrape_config username = @userconfig['username'] %w(group package processor release).each do |type| @autoconfig["#{type}_ids"].clear if @autoconfig["#{type}_ids"] end json = get_via_rest_api "/users/#{username}/groups.js" projects = json.collect {|group| group['group']['unix_group_name'] } puts "Fetching #{projects.size} projects" projects.each do |project| scrape_project(project) end end |
#scrape_project(project) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rubyforge.rb', line 118 def scrape_project(project) data = { "group_ids" => {}, "package_ids" => {}, "processor_ids" => Hash.new { |h,k| h[k] = {} }, "release_ids" => Hash.new { |h,k| h[k] = {} }, } unless data["group_ids"].has_key? project then json = get_via_rest_api "/groups/#{project}.js" group_id = json["group"]["group_id"].to_i data["group_ids"][project] = group_id end # Get project's packages json = get_via_rest_api "/groups/#{project}/packages.js" json.each do |package| data["package_ids"][package["package"]["name"]] = package["package"]["package_id"] # Get releases for this package json = get_via_rest_api "/packages/#{package["package"]["package_id"]}/releases.js" json.each do |release| data["release_ids"][package["package"]["name"]][release["name"]] = release["release_id"] end end # Get processor ids if @autoconfig['processor_ids'].nil? || @autoconfig['processor_ids'].empty? puts "Fetching processor ids" if $DEBUG json = get_via_rest_api "/processors.js" json.each do |processor| data["processor_ids"][processor["processor"]["name"]] = processor["processor"]["processor_id"] end end data.each do |key, val| @autoconfig[key] ||= {} @autoconfig[key].merge! val end save_autoconfig end |
#setup ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rubyforge.rb', line 77 def setup FileUtils::mkdir_p RUBYFORGE_D, :mode => 0700 unless test ?d, RUBYFORGE_D test ?e, CONFIG_F and FileUtils::mv CONFIG_F, "#{CONFIG_F}.bak" config = CONFIG.dup config.delete "rubyforge" open(CONFIG_F, "w") { |f| f.write YAML.dump(config) } edit = (ENV["EDITOR"] || ENV["EDIT"] || "vi") + " '#{CONFIG_F}'" system edit or puts "edit '#{CONFIG_F}'" end |
#uri ⇒ Object
73 74 75 |
# File 'lib/rubyforge.rb', line 73 def uri @uri ||= URI.parse @userconfig['uri'] end |