lib/bukin/providers/bukget.rb in bukin-0.2.0 vs lib/bukin/providers/bukget.rb in bukin-0.3.0

- old
+ new

@@ -1,7 +1,8 @@ require 'bukin/utils' require 'json' +require 'cgi' # BukGet api # Docs: http://bukget.org/pages/docs/API3.html class Bukin::Bukget attr_reader :url @@ -12,17 +13,38 @@ def api_url "#{url}/3" end - def download(name, version, server) + def resolve_info(data) + return data if data[:download] + + name = data[:name] + version = data[:version] || 'latest' + server = data[:server] server = 'bukkit' if server == 'craftbukkit' - url = "#{api_url}/plugins/#{server}/#{name}/#{version}/download" - download_file(url, true) + + url = "#{api_url}/plugins/#{CGI.escape(server)}/#{CGI.escape(name)}/#{CGI.escape(version)}" + info = JSON.parse(open(url).read) + + versions = info['versions'] + if versions.empty? + raise Bukin::InstallError, "The plugin #{name} (#{version}) has no available downloads from BukGet." + end + + version_data = versions.find {|version_data| jar_extension?(version_data)} + if version_data + data[:version] = version_data['version'] + data[:download] = version_data['download'] + else + puts info + raise Bukin::InstallError, "The plugin #{name} (#{version}) has no jar files available for download" + end + + data end - def info(name, version, server) - server = 'bukkit' if server == 'craftbukkit' - url = "#{api_url}/plugins/#{server}/#{name}/#{version}" - JSON.parse(open(url).read) + def jar_extension?(version_data) + File.extname(version_data['filename']) == '.jar' end + end