class Analects::Source

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/analects/source.rb, line 6
def initialize(options = {})
  @options = options
end

Public Instance Methods

data_dir() click to toggle source
# File lib/analects/source.rb, line 19
def data_dir
  Pathname(options[:data_dir])
end
data_file_present?() click to toggle source
# File lib/analects/source.rb, line 29
def data_file_present?
  location.exist?
end
each(&block) click to toggle source
# File lib/analects/source.rb, line 80
def each(&block)
  return to_enum unless block_given?
  loader.each(&block)
end
library() click to toggle source
# File lib/analects/source.rb, line 10
def library   ; options[:library]           ; end
loader() click to toggle source
# File lib/analects/source.rb, line 15
def loader
  @loader ||= options[:loader].new(Pathname(location), library)
end
location() click to toggle source
# File lib/analects/source.rb, line 23
def location
  options[:data_file] ?
    data_dir.join(options[:data_file]) :
    data_dir.join(options[:name].to_s)
end
name() click to toggle source
# File lib/analects/source.rb, line 11
def name      ; options[:name]              ; end
retrieval() click to toggle source
# File lib/analects/source.rb, line 13
def retrieval ; Array(options[:retrieval])  ; end
retrieve() click to toggle source
# File lib/analects/source.rb, line 33
def retrieve
  retrieve! unless data_file_present?
end
retrieve!() click to toggle source
# File lib/analects/source.rb, line 37
def retrieve!
  retrieval.inject(url) do | result, method |
    self.send( "retrieve_#{method}", result )
  end
end
retrieve_git(url) click to toggle source

url -> clones repo

# File lib/analects/source.rb, line 76
def retrieve_git(url)
  `git clone #{url} #{data_dir}/#{name}` # Admittedly crude
end
retrieve_gunzip(stream) click to toggle source

gzipped stream -> uncompressed stream

# File lib/analects/source.rb, line 50
def retrieve_gunzip(stream)
  require 'zlib'
  Zlib::GzipReader.new(stream)
end
retrieve_http(url) click to toggle source

url -> stream

# File lib/analects/source.rb, line 44
def retrieve_http(url)
  require 'open-uri'
  StringIO.new(open(url).read)
end
retrieve_save(data) click to toggle source

stream|string -> create data file

# File lib/analects/source.rb, line 69
def retrieve_save(data)
  File.open( location, 'w' ) do |f|
    f << ( data.respond_to?(:read) ? data.read : data )
  end
end
retrieve_unzip(stream) click to toggle source
# File lib/analects/source.rb, line 55
def retrieve_unzip(stream)
  require 'zip'
  location.mkdir unless location.exist?
  Zip::InputStream.open(stream) do |io|
    while (entry = io.get_next_entry)
      next if entry.ftype == :symlink
      loc = location.join(entry.name)
      loc.delete if loc.exist?
      entry.extract(loc)
    end
  end
end
url() click to toggle source
# File lib/analects/source.rb, line 12
def url       ; options[:url]               ; end