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.



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

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

Instance Method Details

#add(url) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/diggit_core.rb', line 219

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

#folder(url) ⇒ Object



276
277
278
# File 'lib/diggit_core.rb', line 276

def folder(url)
	File.expand_path(id(url), SOURCES_FOLDER)
end

#get(source_def) ⇒ Object



234
235
236
# File 'lib/diggit_core.rb', line 234

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

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



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/diggit_core.rb', line 238

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



268
269
270
# File 'lib/diggit_core.rb', line 268

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

#hashesObject



264
265
266
# File 'lib/diggit_core.rb', line 264

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

#id(url) ⇒ Object



272
273
274
# File 'lib/diggit_core.rb', line 272

def id(url)
	url.gsub(/[^[\w-]]+/, "_")
end

#rem(source_def) ⇒ Object



227
228
229
230
231
232
# File 'lib/diggit_core.rb', line 227

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

#saveObject



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

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

#sizeObject



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

def size
	return @sources.size
end

#update(source_hash) ⇒ Object



250
251
252
# File 'lib/diggit_core.rb', line 250

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

#url(source_def) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/diggit_core.rb', line 254

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