Sha256: 5d7dac4fec25931b5a38ad90ec688348f5dc602345754d89a3968890ecdbe066

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require 'json'
require 'cgi'

module Bukin
  # Bukkit download api
  # Docs: http://dl.bukkit.org/about/
  class BukkitDl
    attr_reader :url

    VERSION = 'latest-rb'
    URL = 'http://dl.bukkit.org'
    GOOD_VERSIONS = "'latest', 'latest-rb', 'latest-beta', 'latest-dev', "\
                    "'git-0fd25c4' or 'build-2912'"

    def initialize(url = URL)
      @url = url
    end

    def find(data)
      name = data[:name]
      version = data[:version] || VERSION

      unless correct_version_format?(version)
        raise VersionError.new(name, version, GOOD_VERSIONS)
      end

      info = Bukin.try_get_json(
        "#{@url}/api/1.0/downloads/projects/"\
        "#{CGI.escape(name)}/view/#{CGI.escape(version)}/")

      raise NoDownloadError.new(name, version) unless info

      download = @url + info['file']['url']

      return "build-#{info['build_number']}", download
    end

    def correct_version_format?(version)
      'latest' == version ||
      /^latest-(rb|beta|dev)$/ =~ version ||
      /^git-[0-9a-f]{7,40}$/ =~ version ||
      /^build-[0-9]+$/ =~ version
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bukin-0.9.0 lib/bukin/bukkit_dl.rb
bukin-0.8.0 lib/bukin/bukkit_dl.rb
bukin-0.7.0 lib/bukin/bukkit_dl.rb