data_collector 0.52.0 → 0.53.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a823f454e158aad98d32448c3644aab73c6a9005a717bb76b7df606ebd46af8
4
- data.tar.gz: 1d54374bbe71277fe40b425b35c23615f35359d3b38ae984dde66ca036d7284b
3
+ metadata.gz: 749914ea43bad2734c79a131eb5db2f1ef2b953fa386a9cb3eb50698e7d2f1f4
4
+ data.tar.gz: 0402100bbfe402ad1c96cc0b767c608a917402e748ea4f57b9bf298ca9510dba
5
5
  SHA512:
6
- metadata.gz: 3345b2e790f0649408e537ab606eb09d27085ff6bfb3094baabb65354b59015cbfd154ec9a7d4e002018f3236c93a80d50d1439175c37ba591afd57c366b38f0
7
- data.tar.gz: ed41b772815c3cec498c71e08cbb933e485c7e31cf82d2fbff777fef2026f6a78e54fbc503c67bd1f89c0758b044080143877e426a8a5c3fd2f81983cbe66826
6
+ metadata.gz: b00c092e0670a26a85bbe1299c687a132c65af7950c5b0f74ff537a8ba948af27617164bd8beebc3b072d424fafdd58865e8cafbaa03d5e6aafb8e682806ba43
7
+ data.tar.gz: 4072e26cfd9a2ef57b094b3ea7cc340f0d735e8ca4356d6e346f725b2486e7284b624936724c0545677571d7ff2a0c447785374778cb55080e69cf54bc7df1a3
@@ -29,60 +29,64 @@ module DataCollector
29
29
 
30
30
  def from_uri(source, options = {}, &block)
31
31
  block_consumed = false
32
- source = CGI.unescapeHTML(source)
33
- @logger.info("Reading #{source}")
34
- raise DataCollector::Error, 'from_uri expects a scheme like file:// of https://' unless source =~ /:\/\//
35
-
36
- scheme, path = source.split('://')
37
- source = "#{scheme}://#{URI.encode_www_form_component(path)}"
38
- uri = URI(source)
39
- begin
40
- data = nil
41
- case uri.scheme
42
- when 'http'
43
- data = from_http(uri, options)
44
- when 'https'
45
- data = from_https(uri, options)
46
- when 'file'
47
- absolute_path = File.absolute_path("#{URI.decode_www_form_component("#{uri.host}#{uri.path}")}")
48
- if File.directory?(absolute_path)
49
- return from_dir(uri, options)
50
- else
51
- if block_given?
52
- data = from_file(uri, options, &block)
53
- block_consumed = true if data.is_a?(TrueClass)
32
+ data = nil
33
+ if source.is_a?(StringIO)
34
+ data = from_stringio(source, options)
35
+ else
36
+ source = CGI.unescapeHTML(source)
37
+ @logger.info("Reading #{source}")
38
+ raise DataCollector::Error, 'from_uri expects a scheme like file:// of https://' unless source =~ /:\/\//
39
+
40
+ scheme, path = source.split('://')
41
+ source = "#{scheme}://#{URI.encode_www_form_component(path)}"
42
+ uri = URI(source)
43
+ begin
44
+ case uri.scheme
45
+ when 'http'
46
+ data = from_http(uri, options)
47
+ when 'https'
48
+ data = from_https(uri, options)
49
+ when 'file'
50
+ absolute_path = File.absolute_path("#{URI.decode_www_form_component("#{uri.host}#{uri.path}")}")
51
+ if File.directory?(absolute_path)
52
+ return from_dir(uri, options)
53
+ else
54
+ if block_given?
55
+ data = from_file(uri, options, &block)
56
+ block_consumed = true if data.is_a?(TrueClass)
57
+ else
58
+ data = from_file(uri, options)
59
+ end
60
+ end
61
+ when /amqp/
62
+ if uri.scheme =~ /^rpc/
63
+ data = from_rpc(uri, options)
54
64
  else
55
- data = from_file(uri, options)
65
+ data = from_queue(uri, options)
56
66
  end
57
- end
58
- when /amqp/
59
- if uri.scheme =~ /^rpc/
60
- data = from_rpc(uri, options)
61
67
  else
62
- data = from_queue(uri, options)
68
+ raise "Do not know how to process #{source}"
63
69
  end
64
- else
65
- raise "Do not know how to process #{source}"
66
- end
67
70
 
68
- data = data.nil? ? 'no data found' : data
69
-
70
- if block_given? && !block_consumed
71
- yield data
72
- else
73
- data
74
71
  end
75
- rescue DataCollector::InputError => e
76
- @logger.error(e.message)
77
- raise e
78
- rescue DataCollector::Error => e
79
- @logger.error(e.message)
80
- nil
81
- rescue StandardError => e
82
- @logger.error(e.message)
83
- puts e.backtrace.join("\n")
84
- nil
85
72
  end
73
+ data = data.nil? ? 'no data found' : data
74
+
75
+ if block_given? && !block_consumed
76
+ yield data
77
+ else
78
+ data
79
+ end
80
+ rescue DataCollector::InputError => e
81
+ @logger.error(e.message)
82
+ raise e
83
+ rescue DataCollector::Error => e
84
+ @logger.error(e.message)
85
+ nil
86
+ rescue StandardError => e
87
+ @logger.error(e.message)
88
+ puts e.backtrace.join("\n")
89
+ nil
86
90
  end
87
91
 
88
92
  private
@@ -204,6 +208,23 @@ module DataCollector
204
208
  data
205
209
  end
206
210
 
211
+ def from_stringio(sio, options = {}, &block)
212
+ raise DataCollector::InputError, "No IO input" unless sio.is_a?(StringIO)
213
+ raise DataCollector::InputError, "content_type option not supplied" unless options.key?(:content_type)
214
+
215
+ preferred_extension = MIME::Types[options[:content_type]].first.extensions.first || 'txt'
216
+
217
+ file = Tempfile.new(["dc_", ".#{preferred_extension}"])
218
+ begin
219
+ sio.rewind
220
+ file.write(sio.read)
221
+ file.close
222
+ from_file(URI("file://#{file.path}"))
223
+ ensure
224
+ file.unlink
225
+ end
226
+ end
227
+
207
228
  def from_file(uri, options = {}, &block)
208
229
  data = nil
209
230
  uri = normalize_uri(uri)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module DataCollector
3
- VERSION = "0.52.0"
3
+ VERSION = "0.53.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_collector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.52.0
4
+ version: 0.53.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Celik
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-20 00:00:00.000000000 Z
11
+ date: 2024-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport