Class: Analects::Source

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/analects/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Source

Returns a new instance of Source.



6
7
8
# File 'lib/analects/source.rb', line 6

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/analects/source.rb', line 4

def options
  @options
end

Instance Method Details

#data_dirObject



19
20
21
# File 'lib/analects/source.rb', line 19

def data_dir
  Pathname(options[:data_dir])
end

#data_file_present?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/analects/source.rb', line 29

def data_file_present?
  location.exist?
end

#each(&block) ⇒ Object



80
81
82
83
# File 'lib/analects/source.rb', line 80

def each(&block)
  return to_enum unless block_given?
  loader.each(&block)
end

#libraryObject



10
# File 'lib/analects/source.rb', line 10

def library   ; options[:library]           ; end

#loaderObject



15
16
17
# File 'lib/analects/source.rb', line 15

def loader
  @loader ||= options[:loader].new(Pathname(location), library)
end

#locationObject



23
24
25
26
27
# 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

#nameObject



11
# File 'lib/analects/source.rb', line 11

def name      ; options[:name]              ; end

#retrievalObject



13
# File 'lib/analects/source.rb', line 13

def retrieval ; Array(options[:retrieval])  ; end

#retrieveObject



33
34
35
# File 'lib/analects/source.rb', line 33

def retrieve
  retrieve! unless data_file_present?
end

#retrieve!Object



37
38
39
40
41
# 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) ⇒ Object

url -> clones repo



76
77
78
# File 'lib/analects/source.rb', line 76

def retrieve_git(url)
  `git clone #{url} #{data_dir}/#{name}` # Admittedly crude
end

#retrieve_gunzip(stream) ⇒ Object

gzipped stream -> uncompressed stream



50
51
52
53
# File 'lib/analects/source.rb', line 50

def retrieve_gunzip(stream)
  require 'zlib'
  Zlib::GzipReader.new(stream)
end

#retrieve_http(url) ⇒ Object

url -> stream



44
45
46
47
# File 'lib/analects/source.rb', line 44

def retrieve_http(url)
  require 'open-uri'
  StringIO.new(open(url).read)
end

#retrieve_save(data) ⇒ Object

stream|string -> create data file



69
70
71
72
73
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# 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

#urlObject



12
# File 'lib/analects/source.rb', line 12

def url       ; options[:url]               ; end