Sha256: 32c2e6071accafe71b429c56c1759894cbc8496f192cd11c3d47cbae817b7ca3

Contents?: true

Size: 1.79 KB

Versions: 16

Compression:

Stored size: 1.79 KB

Contents

require 'uri'
require 'cgi'
require 'geminabox'

class GeminaboxClient
  attr_reader :url, :http_client

  def initialize(url)
    extract_username_and_password_from_url!(url)
    @http_client = Geminabox.http_adapter
    @http_client.set_auth(url_for(:upload), @username, @password) 
  end

  def extract_username_and_password_from_url!(url)
    uri = URI.parse(url.to_s)
    @username = CGI.unescape(uri.user) if uri.user
    @password = CGI.unescape(uri.password) if uri.password
    uri.user = uri.password = nil
    uri.path = uri.path + "/" unless uri.path.end_with?("/")
    @url = uri.to_s
  end

  def url_for(path)
    url + path.to_s
  end

  def push(gemfile, options = {})
    response = http_client.post(url_for(:upload), { 'file' => File.open(gemfile, "rb"), 'overwrite' => !!options[:overwrite] }, { 'Accept' => 'text/plain' })

    if response.status < 300
      response.body
    else
      raise GeminaboxClient::Error, "Error (#{response.status} received)\n\n#{response.body}"
    end
  end

end

class GeminaboxClient::Error < RuntimeError
end

module GeminaboxClient::GemLocator
  def find_gem(dir)
    gemspec_path = Dir.glob(File.join(dir, "*.gemspec")).first
    gemspec = Gem::Specification::load(gemspec_path)
    gemname = gemspec.name
    glob_matcher = "{pkg/,}#{gemname}-*.gem"
    latest_gem_for(gemname, Dir.glob(glob_matcher)) or raise Gem::CommandLineError, NO_GEM_PROVIDED_ERROR_MESSAGE
  end

  def latest_gem_for(gemname, files)
    regexp_matcher = %r{(?:pkg/)#{gemname}-(#{Gem::Version::VERSION_PATTERN})\.gem}
    sorter = lambda{|v| Gem::Version.new(regexp_matcher.match(v)[1]) }
    files.grep(regexp_matcher).max_by(&sorter)
  end

  extend self

  NO_GEM_PROVIDED_ERROR_MESSAGE = "Couldn't find a gem in pkg, please specify a gem name on the command line (e.g. gem inabox GEMNAME)"
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
geminabox-1.4.1 lib/geminabox_client.rb
geminabox-1.4.0 lib/geminabox_client.rb
geminabox-1.3.1 lib/geminabox_client.rb
geminabox-1.3.0 lib/geminabox_client.rb
geminabox-1.2.0 lib/geminabox_client.rb
geminabox-1.1.1 lib/geminabox_client.rb
geminabox-1.1.0 lib/geminabox_client.rb
geminabox-1.0.1 lib/geminabox_client.rb
geminabox-1.0.0 lib/geminabox_client.rb
geminabox-0.13.15 lib/geminabox_client.rb
geminabox-0.13.14 lib/geminabox_client.rb
geminabox-0.13.13 lib/geminabox_client.rb
geminabox-0.13.11 lib/geminabox_client.rb
geminabox-0.13.10 lib/geminabox_client.rb
geminabox-0.13.9 lib/geminabox_client.rb
geminabox-0.13.8 lib/geminabox_client.rb