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

Returns a new instance of IncomingGem.



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

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



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

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

#extract_specObject



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

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



27
28
29
# File 'lib/geminabox/incoming_gem.rb', line 27

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

#get_nameObject



55
56
57
58
59
# File 'lib/geminabox/incoming_gem.rb', line 55

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

#hexdigestObject



65
66
67
# File 'lib/geminabox/incoming_gem.rb', line 65

def hexdigest
  @sha1
end

#nameObject



51
52
53
# File 'lib/geminabox/incoming_gem.rb', line 51

def name
  @name ||= get_name
end

#specObject



37
38
39
# File 'lib/geminabox/incoming_gem.rb', line 37

def spec
  @spec ||= extract_spec
end

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/geminabox/incoming_gem.rb', line 31

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