Class: CDR::Fetcher
- Inherits:
-
Object
- Object
- CDR::Fetcher
- Defined in:
- lib/cdr-fetcher.rb
Overview
Fetch CDR’s from a remote server.
:base_dir defaults to “/var/log/asterisk/cdr-csv”. :last_dir defaults to the first sub-directory in :base_dir. :last_file defaults to the first file in :last_dir.
Usage:
require 'cdr-fetcher'
require 'pp'
cdrs = CDR::Fetcher.new(
'hostname',
'username',
:password => 'password',
:base_dir => '/var/log/asterisk/cdr-csv',
:last_dir => 'old-cdrs',
:last_file=> 'old-cdr.csv')
cdrs.each do |cdr|
pp cdr
end
Instance Attribute Summary collapse
-
#base_dir ⇒ Object
readonly
Returns the value of attribute base_dir.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#last_dir ⇒ Object
Returns the value of attribute last_dir.
-
#last_file ⇒ Object
Returns the value of attribute last_file.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#each ⇒ Object
Iterator for CDRs.
-
#each_file ⇒ Object
Iterator for Files.
-
#initialize(hostname, username, opts = {}) ⇒ Fetcher
constructor
Create a new instance of Fetcher.
Constructor Details
#initialize(hostname, username, opts = {}) ⇒ Fetcher
Create a new instance of Fetcher
:base_dir defaults to "/var/log/asterisk/cdr-csv".
:last_dir defaults to the first sub-directory
in :base_dir.
:last_file defaults to the first file in :last_dir.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cdr-fetcher.rb', line 46 def initialize(hostname, username, opts={}) # Process options and arguments opts = { :password => nil, :base_dir => nil, :last_dir => nil, :last_file=> nil }.merge!(opts) @hostname = hostname @username = username @password = opts[:password] @base_dir = opts[:base_dir] @base_dir ||= '/var/log/asterisk/cdr-csv' # Setup SSH and SFTP Connection @ssh = Net::SSH.start(@hostname, @username, :password => @password) @sftp = Net::SFTP::Session.new(@ssh).connect! # Grab the list of directories, and the current directory attributes @directories = directories @cur_dir = directory(opts[:last_dir]) # If we weren't passed an argument, use the first file in the list @cur_dir ||= @directories[0] # Grab the list of files from the current directory @files = files @cur_file = file(opts[:last_file]) # If we weren't passed an argument, use the first file in the list @cur_file ||= @files[0] end |
Instance Attribute Details
#base_dir ⇒ Object (readonly)
Returns the value of attribute base_dir.
36 37 38 |
# File 'lib/cdr-fetcher.rb', line 36 def base_dir @base_dir end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
33 34 35 |
# File 'lib/cdr-fetcher.rb', line 33 def hostname @hostname end |
#last_dir ⇒ Object
Returns the value of attribute last_dir.
37 38 39 |
# File 'lib/cdr-fetcher.rb', line 37 def last_dir @last_dir end |
#last_file ⇒ Object
Returns the value of attribute last_file.
38 39 40 |
# File 'lib/cdr-fetcher.rb', line 38 def last_file @last_file end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
35 36 37 |
# File 'lib/cdr-fetcher.rb', line 35 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
34 35 36 |
# File 'lib/cdr-fetcher.rb', line 34 def username @username end |
Instance Method Details
#each ⇒ Object
Iterator for CDRs. Returns one CDR at a time, automatically moving to the next file or directory until all CDRs have been returned.
CDR files are read into memory one at a time.
Line numbers are NOT zero indexed (i.e. line 1 == line 1 NOT line 0)
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cdr-fetcher.rb', line 100 def each each_file do |file| @sftp.download!(file).split(/\n/).each_with_index do |line,i| cdr = { :hostname => @hostname, :file => file, :line => i+1, :cdr => CSV.parse_line(line) } yield cdr end end end |
#each_file ⇒ Object
Iterator for Files. Returns one file path at a time, automatically moving to the next file or directory until all CDRs have been returned.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cdr-fetcher.rb', line 79 def each_file @more_files = true while @more_files file = @cur_file if file yield [@base_dir,@cur_dir.name,@cur_file.name].join("/") next_file! else @more_files = false break end end end |