Sha256: d937050568c839d0c42ccbfee67e370fcf024a6748be0b8ff00d7622ac0f5c77

Contents?: true

Size: 1.44 KB

Versions: 25

Compression:

Stored size: 1.44 KB

Contents

module Geminabox

  class IncomingGem
    def initialize(gem_data, root_path = Geminabox.data)
      unless gem_data.respond_to? :read
        raise ArgumentError, "Expected an instance of IO"
      end

      digest = Digest::SHA1.new
      if RbConfig::CONFIG["MAJOR"].to_i <= 1 and RbConfig::CONFIG["MINOR"].to_i <= 8
        @tempfile = Tempfile.new("gem")
      else
        @tempfile = Tempfile.new("gem", :encoding => 'binary', :binmode => true)
      end

      while data = gem_data.read(1024**2)
        @tempfile.write data
        digest << data
      end

      @tempfile.close
      @sha1 = digest.hexdigest

      @root_path = root_path
    end

    def gem_data
      File.open(@tempfile.path, "rb")
    end

    def valid?
      spec && spec.name && spec.version
    rescue Gem::Package::Error
      false
    end

    def spec
      @spec ||= extract_spec
    end

    def extract_spec
      if Gem::Package.respond_to? :open
        Gem::Package.open(gem_data, "r", nil) do |pkg|
          return pkg.metadata
        end
      else
        Gem::Package.new(@tempfile.path).spec
      end
    end

    def name
      @name ||= get_name
    end

    def get_name
      filename = %W[#{spec.name} #{spec.version}]
      filename.push(spec.platform) if spec.platform && spec.platform != "ruby"
      filename.join("-") + ".gem"
    end

    def dest_filename
      File.join(@root_path, "gems", name)
    end

    def hexdigest
      @sha1
    end
  end

end

Version data entries

25 entries across 25 versions & 2 rubygems

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