Class: Mihari::Services::FeedReader
- Inherits:
-
Mihari::Service
- Object
- Mihari::Service
- Mihari::Services::FeedReader
- Defined in:
- lib/mihari/services/feed.rb
Overview
Feed reader
Instance Method Summary collapse
- #call(url, headers: {}, method: "GET", params: nil, json: nil, form: nil, timeout: nil) ⇒ Array<Hash>
-
#convert_as_csv(text) ⇒ Array<Object>
Convert text as CSV.
-
#convert_as_json(text) ⇒ Hash, Array<Object>
Convert text as JSON.
-
#read_file(path) ⇒ Array<Object>
Read & convert a file.
Methods inherited from Mihari::Service
Instance Method Details
#call(url, headers: {}, method: "GET", params: nil, json: nil, form: nil, timeout: nil) ⇒ Array<Hash>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mihari/services/feed.rb', line 26 def call(url, headers: {}, method: "GET", params: nil, json: nil, form: nil, timeout: nil) url = Addressable::URI.parse(url) return read_file(url.path) if url.scheme == "file" http = HTTP::Factory.build(headers:, timeout:) res = http.get(url, params:) if method == "GET" res = http.post(url, params:, json:, form:) if method == "POST" body = res.body.to_s content_type = res["Content-Type"].to_s return convert_as_json(body) if content_type.include?("application/json") convert_as_csv(body) end |
#convert_as_csv(text) ⇒ Array<Object>
Convert text as CSV
61 62 63 64 65 |
# File 'lib/mihari/services/feed.rb', line 61 def convert_as_csv(text) text_without_comments = text.lines.reject { |line| line.start_with? "#" }.join("\n") CSV.new(text_without_comments).to_a.reject(&:empty?) end |
#convert_as_json(text) ⇒ Hash, Array<Object>
Convert text as JSON
50 51 52 |
# File 'lib/mihari/services/feed.rb', line 50 def convert_as_json(text) JSON.parse(text).deep_symbolize_keys end |
#read_file(path) ⇒ Array<Object>
Read & convert a file
74 75 76 77 78 79 80 |
# File 'lib/mihari/services/feed.rb', line 74 def read_file(path) text = File.read(path) return convert_as_json(text) if path.end_with?(".json") convert_as_csv text end |