Class: Diggit::Sources

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

Instance Method Summary collapse

Constructor Details

#initializeSources

Returns a new instance of Sources.



144
145
146
147
148
# File 'lib/diggit_core.rb', line 144

def initialize
  @log = Log.new
  @sources = []
  IO.readlines(DIGGIT_SOURCES).each{ |line| @sources << line.strip }
end

Instance Method Details

#add(url) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/diggit_core.rb', line 160

def add(url)
  unless @sources.include?(url)
    @sources << url
    @log.init(url)
    save
  end
end

#folder(url) ⇒ Object



213
214
215
# File 'lib/diggit_core.rb', line 213

def folder(url)
    File.expand_path(url.gsub(/[^[\w-]]+/, "_"), SOURCES_FOLDER)
end

#get(source_def) ⇒ Object



175
176
177
# File 'lib/diggit_core.rb', line 175

def get(source_def)
  hash(url(source_def))
end

#get_all(source_defs, filter = {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/diggit_core.rb', line 179

def get_all(source_defs, filter={})
  sources = []
  if source_defs.nil? || source_defs.empty?
    sources = hashes
  else
    sources = source_defs.map{ |d| hash(d) }
  end
  sources = sources.select{ |s| s[:log][:state] == filter[:state] } if (filter.has_key?(:state))
  sources = sources.select{ |s| s[:log][:error].empty? != filter[:error] } if (filter.has_key?(:error))
  return sources
end

#hash(url) ⇒ Object



209
210
211
# File 'lib/diggit_core.rb', line 209

def hash(url)
  {url: url, folder: folder(url), log: @log.log(url)}
end

#hashesObject



205
206
207
# File 'lib/diggit_core.rb', line 205

def hashes
  @sources.map{ |s| hash(s) }
end

#rem(source_def) ⇒ Object



168
169
170
171
172
173
# File 'lib/diggit_core.rb', line 168

def rem(source_def)
  url = url(source_def)
  @sources.delete(url)
  @log.rem(url)
  save
end

#saveObject



154
155
156
157
158
# File 'lib/diggit_core.rb', line 154

def save
  File.open(DIGGIT_SOURCES, "w") do |f|
    @sources.each{ |s| f.puts(s) }
  end
end

#sizeObject



150
151
152
# File 'lib/diggit_core.rb', line 150

def size
  return @sources.size
end

#update(source_hash) ⇒ Object



191
192
193
# File 'lib/diggit_core.rb', line 191

def update(source_hash)
  @log.update(source_hash)
end

#url(source_def) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/diggit_core.rb', line 195

def url(source_def)
  url = source_def
  if /\d+/.match(source_def)
    idx = source_def.to_i - 1
    raise "Wrong source identifier" if idx < 0 || idx >= @sources.size
    url = @sources[source_def.to_i - 1]
  end
  url
end