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.



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

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

Instance Method Details

#add(url) ⇒ Object



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

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

#folder(url) ⇒ Object



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

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

#get(source_def) ⇒ Object



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

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

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



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

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



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

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

#hashesObject



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

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

#rem(source_def) ⇒ Object



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

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

#saveObject



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

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

#sizeObject



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

def size
  return @sources.size
end

#update(source_hash) ⇒ Object



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

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

#url(source_def) ⇒ Object



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

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