Class: Geminabox::IncomingGem

Inherits:
Object
  • Object
show all
Defined in:
lib/geminabox/incoming_gem.rb

Instance Method Summary collapse

Constructor Details

#initialize(gem_data, root_path = Geminabox.settings.data) ⇒ IncomingGem



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/geminabox/incoming_gem.rb', line 2

def initialize(gem_data, root_path = Geminabox.settings.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')
  end

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

  @tempfile.close
  @sha1 = digest.hexdigest

  @root_path = root_path
end

Instance Method Details

#dest_filenameObject



58
59
60
# File 'lib/geminabox/incoming_gem.rb', line 58

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

#extract_specObject



39
40
41
42
43
44
45
46
47
# File 'lib/geminabox/incoming_gem.rb', line 39

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

#gem_dataObject



25
26
27
# File 'lib/geminabox/incoming_gem.rb', line 25

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

#hexdigestObject



62
63
64
# File 'lib/geminabox/incoming_gem.rb', line 62

def hexdigest
  @sha1
end

#nameObject



49
50
51
52
53
54
55
56
# File 'lib/geminabox/incoming_gem.rb', line 49

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

#specObject



35
36
37
# File 'lib/geminabox/incoming_gem.rb', line 35

def spec
  @spec ||= extract_spec
end

#valid?Boolean



29
30
31
32
33
# File 'lib/geminabox/incoming_gem.rb', line 29

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