Class: Mihari::Feed::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/mihari/feed/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, http_request_headers: {}, http_request_method: "GET", http_request_payload_type: nil, http_request_payload: {}) ⇒ Reader

Returns a new instance of Reader.



11
12
13
14
15
16
17
18
# File 'lib/mihari/feed/reader.rb', line 11

def initialize(uri, http_request_headers: {}, http_request_method: "GET", http_request_payload_type: nil, http_request_payload: {})
  @uri = Addressable::URI.parse(uri)
  @http_request_headers = http_request_headers.insensitive
  @http_request_method = http_request_method
  @http_request_payload = http_request_payload

  http_request_headers["content-type"] = http_request_payload_type if http_request_payload_type
end

Instance Attribute Details

#http_request_headersObject (readonly)

Returns the value of attribute http_request_headers.



9
10
11
# File 'lib/mihari/feed/reader.rb', line 9

def http_request_headers
  @http_request_headers
end

#http_request_methodObject (readonly)

Returns the value of attribute http_request_method.



9
10
11
# File 'lib/mihari/feed/reader.rb', line 9

def http_request_method
  @http_request_method
end

#http_request_payloadObject (readonly)

Returns the value of attribute http_request_payload.



9
10
11
# File 'lib/mihari/feed/reader.rb', line 9

def http_request_payload
  @http_request_payload
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/mihari/feed/reader.rb', line 9

def uri
  @uri
end

Instance Method Details

#convert_as_csv(text) ⇒ Array<Hash>

Convert text as CSV

Parameters:

  • text (String)

Returns:

  • (Array<Hash>)


61
62
63
64
65
# File 'lib/mihari/feed/reader.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) ⇒ Array<Hash>

Convert text as JSON

Parameters:

  • text (String)

Returns:

  • (Array<Hash>)


47
48
49
50
51
52
# File 'lib/mihari/feed/reader.rb', line 47

def convert_as_json(text)
  data = JSON.parse(text, symbolize_names: true)
  return data if data.is_a?(Array)

  [data]
end

#readObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mihari/feed/reader.rb', line 20

def read
  return read_file(uri.path) if uri.scheme == "file"

  res = nil
  client = HTTP.new(uri, headers: http_request_headers, payload: http_request_payload)

  res = client.get if http_request_method == "GET"
  res = client.post if http_request_method == "POST"

  return [] if res.nil?

  body = res.body
  content_type = res["Content-Type"].to_s
  if content_type.include?("application/json")
    convert_as_json(body)
  else
    convert_as_csv(body)
  end
end

#read_file(path) ⇒ Array<Hash>

Read & convert a file

Parameters:

  • path (String)

Returns:

  • (Array<Hash>)


74
75
76
77
78
79
80
81
82
# File 'lib/mihari/feed/reader.rb', line 74

def read_file(path)
  text = File.read(path)

  if path.end_with?(".json")
    convert_as_json text
  else
    convert_as_csv text
  end
end