Class: Mihari::Services::FeedReader

Inherits:
Mihari::Service show all
Defined in:
lib/mihari/services/feed.rb

Overview

Feed reader

Instance Method Summary collapse

Methods inherited from Mihari::Service

call, #result, result

Instance Method Details

#call(url, headers: {}, method: "GET", params: nil, json: nil, form: nil, timeout: nil) ⇒ Array<Hash>

Parameters:

  • url (String)
  • options (Hash, nil)
  • method (String) (defaults to: "GET")
  • headers (Hash, nil) (defaults to: {})
  • params (Hash, nil) (defaults to: nil)
  • json (Hash, nil) (defaults to: nil)
  • form (form, nil) (defaults to: nil)
  • selector (String)
  • timeout (Integer, nil) (defaults to: nil)

Returns:

  • (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

Parameters:

  • text (String)

Returns:

  • (Array<Object>)


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

Parameters:

  • text (String)

Returns:

  • (Hash, Array<Object>)


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

Parameters:

  • path (String)

Returns:

  • (Array<Object>)


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