Class: DataCollector::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/data_collector/input.rb,
lib/data_collector/input/dir.rb,
lib/data_collector/input/rpc.rb,
lib/data_collector/input/queue.rb,
lib/data_collector/input/generic.rb

Defined Under Namespace

Classes: Dir, Generic, Queue, Rpc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInput

Returns a new instance of Input.



26
27
28
# File 'lib/data_collector/input.rb', line 26

def initialize
  @logger = Logger.new(STDOUT)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



24
25
26
# File 'lib/data_collector/input.rb', line 24

def raw
  @raw
end

Instance Method Details

#from_uri(source, options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/data_collector/input.rb', line 30

def from_uri(source, options = {}, &block)
  block_consumed = false
  data = nil
  if source.is_a?(StringIO)
    data = from_stringio(source, options)
  else
    source = CGI.unescapeHTML(source)
    @logger.info("Reading #{source}")
    raise DataCollector::Error, 'from_uri expects a scheme like file:// of https://' unless source =~ /:\/\//

    scheme, path = source.split('://')
    source = "#{scheme}://#{URI.encode_www_form_component(path)}"
    uri = URI(source)
    begin
      case uri.scheme
      when 'http'
        data = from_http(uri, options)
      when 'https'
        data = from_https(uri, options)
      when 'file'
        absolute_path = File.absolute_path("#{URI.decode_www_form_component("#{uri.host}#{uri.path}")}")
        if File.directory?(absolute_path)
          return from_dir(uri, options)
        else
          if block_given?
            data = from_file(uri, options, &block)
            block_consumed = true if data.is_a?(TrueClass)
          else
            data = from_file(uri, options)
          end
        end
      when /amqp/
        if uri.scheme =~ /^rpc/
          data = from_rpc(uri, options)
        else
          data = from_queue(uri, options)
        end
      else
        raise "Do not know how to process #{source}"
      end

    end
  end
  data = data.nil? ? 'no data found' : data

  if block_given? && !block_consumed
    yield data
  else
    data
  end
rescue DataCollector::InputError => e
  @logger.error(e.message)
  raise e
rescue DataCollector::Error => e
  @logger.error(e.message)
  nil
rescue StandardError => e
  @logger.error(e.message)
  puts e.backtrace.join("\n")
  nil
end