Class: Cran::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/cran/package.rb

Defined Under Namespace

Classes: InvalidContentType, InvalidFileSize, InvalidTempFile, NoDescriptionError

Constant Summary collapse

DESCRIPTION_FILE_NAME =
/DESCRIPTION/
DESCRIPTION_FILE_MAPPING =
{
    name: "Package",
    version: "Version",
    date: "Date/Publication",
    title: "Title",
    description: "Description",
    author: "Author",
    maintainer: "Maintainer"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Package

Returns a new instance of Package.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cran/package.rb', line 23

def initialize(url, options = {})
  @options = Cran::Config.get(:package).merge(options)
  begin
    @url = url.to_s
    logger.debug "Initialize package #{@url}..."
    tmp_file = open(url)
    raise InvalidTempFile unless tmp_file.is_a?(Tempfile)
    raise InvalidFileSize if tmp_file.size.zero?
    raise InvalidContentType unless tmp_file.content_type == @options.fetch(:content_type)
    tar = Gem::Package::TarReader.new(Zlib::GzipReader.open(tmp_file))
    description_file = tar.find{|s| DESCRIPTION_FILE_NAME =~ s.full_name} or raise NoDescriptionError
    dcf_parse = Dcf.parse(description_file.read).first
    @_obj = OpenStruct.new(Hash.new.tap do |h|
      DESCRIPTION_FILE_MAPPING.each{|key,value| h.store(key, dcf_parse.fetch(value))}
    end)
  rescue InvalidTempFile, InvalidFileSize, InvalidContentType, NoDescriptionError => e
    raise e if @options.fetch(:raise_on_error)
    logger.warn "#{e.class} : skip file #{@url}"
  ensure
    FileUtils.rm_f(tmp_file) if defined?(tmp_file) && tmp_file.is_a?(Tempfile)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/cran/package.rb', line 50

def method_missing(id,*args,&block)
  if _obj.respond_to?(id)
    self._obj.send(id, *args,&block)
  else
    super
  end
end

Instance Attribute Details

#_objObject (readonly)

Returns the value of attribute _obj.



20
21
22
# File 'lib/cran/package.rb', line 20

def _obj
  @_obj
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/cran/package.rb', line 21

def options
  @options
end

Instance Method Details

#saveObject



46
47
48
# File 'lib/cran/package.rb', line 46

def save
  Cran::Db.push(@url, _obj)
end