Class: Gdt::Gdt2Http
- Inherits:
-
Object
- Object
- Gdt::Gdt2Http
- Defined in:
- lib/gdt2http.rb
Instance Method Summary collapse
- #files ⇒ Object
- #handle_file(filename) ⇒ Object
-
#initialize ⇒ Gdt2Http
constructor
parse command line options.
-
#load_configuration ⇒ Object
load configuration from configfile, merge with default options.
-
#run!(args = ARGV) ⇒ Object
run the application.
Constructor Details
#initialize ⇒ Gdt2Http
parse command line options
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gdt2http.rb', line 38 def initialize load_configuration @opts = OptionParser.new do |opts| opts.on "-V","--version","Display version and exit" do puts "#{self.class} #{::Gdt::VERSION}" exit end opts.on "-f", "--files PATTERN", Array, "a list of files or shell globs to look for", "default is '**/*.GDT'" do |arg| @options[:files] = arg end opts.on "-u", "--uri URI", "URI of the HTTP-Endpoint to which the parsed data is sent" do |arg| @options[:endpoint] = arg end opts.on_tail "-p", "--print-config", "Print the current configuration", "in a format that can be used as a configuration file" do puts @options_from_file.merge(@options).to_yaml exit end end end |
Instance Method Details
#files ⇒ Object
62 63 64 |
# File 'lib/gdt2http.rb', line 62 def files @options[:files].map { |p| Dir.glob(p) }.flatten.compact.uniq end |
#handle_file(filename) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gdt2http.rb', line 66 def handle_file(filename) # open and parse the given file str = File.read(filename) begin data = Gdt.new(str) begin res = ::Net::HTTP.post_form(URI.parse(@options[:endpoint]), data.to_hash ) puts res.header.to_hash.map { |k,v| "#{k}: #{v}" } puts case res when ::Net::HTTPSuccess File.delete(filename) if @options[:delete_files] puts res.body when ::Net::HTTPClientError warn "Client Error" when ::Net::HTTPServerError warn "Server Error" end rescue ::Errno::ECONNREFUSED puts "Unable to connect to server '#{@options[:endpoint]}' (connection refused)" end rescue ParseError => e warn "Parse error in '#{filename}'" end end |
#load_configuration ⇒ Object
load configuration from configfile, merge with default options
27 28 29 30 31 32 33 34 |
# File 'lib/gdt2http.rb', line 27 def load_configuration # try to read configuration from file @options_from_file = YAML.load_file(ConfigFile) || {} rescue {} # if an option is not given on the command line # it is taken from the config file, or the default is used @options = Hash.new() { |h,k| @options_from_file[k] || DefaultConfig[k] } end |
#run!(args = ARGV) ⇒ Object
run the application
97 98 99 100 101 102 |
# File 'lib/gdt2http.rb', line 97 def run!(args = ARGV) @opts.parse!(args) files.each { |f| handle_file(f) } end |