Class: Source

Inherits:
SourceBase show all
Defined in:
lib/abelard/load.rb

Instance Method Summary collapse

Methods inherited from SourceBase

#process

Constructor Details

#initialize(conf_in) ⇒ Source

Returns a new instance of Source.



229
230
231
232
233
234
235
# File 'lib/abelard/load.rb', line 229

def initialize(conf_in)
  if conf_in.respond_to?(:keys)
    @conf = conf_in
  else
    @conf = get_config(conf_in) || die("No config for #{conf_in}")
  end
end

Instance Method Details

#all_configsObject



258
259
260
# File 'lib/abelard/load.rb', line 258

def all_configs
  YAML.load_file(CONFIG_FILE) || {}
end

#get_config(key) ⇒ Object



262
263
264
265
# File 'lib/abelard/load.rb', line 262

def get_config(key)
  configuration_file = all_configs
  configuration_file[key]
end

#loadObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/abelard/load.rb', line 237

def load
  conf = @conf
  dest = conf["dest"] || die("No 'dest' directory defined")
  urls = conf["urls"] || die("No urls defined")

  ensure_dest(dest)

  fetcher = Fetcher.new
  fetcher.user = conf["user"]
  fetcher.password = conf["password"]

  urls.each do |urlpath|
    url = URI(urlpath)
    fetcher.get(url) do |response|
      write_raw(response.body, "#{dest}/raw.xml") if Debug
      parser = LibXML::XML::Parser.string(response.body)
      process(parser, dest)
    end
  end
end

#save_config(key, conf) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
# File 'lib/abelard/load.rb', line 267

def save_config(key, conf)
  configuration_data = all_configs
  if configuration_data.has_key? key
    die("Already have config for #{key}")
  else
    configuration_data[key] = conf
    open(CONFIG_FILE, 'w+') do |conf_file|
      conf_file.puts(configuration_data.to_yaml)
    end
  end
end

#write_raw(data, filename) ⇒ Object



279
280
281
# File 'lib/abelard/load.rb', line 279

def write_raw(data, filename)
  File.open(filename, "w") { |f| f.write(data) }
end