Class: Mihari::Feed::Reader
- Inherits:
-
Object
- Object
- Mihari::Feed::Reader
- Defined in:
- lib/mihari/feed/reader.rb
Instance Attribute Summary collapse
-
#http_request_headers ⇒ Object
readonly
Returns the value of attribute http_request_headers.
-
#http_request_method ⇒ Object
readonly
Returns the value of attribute http_request_method.
-
#http_request_payload ⇒ Object
readonly
Returns the value of attribute http_request_payload.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#convert_as_csv(text) ⇒ Array<Hash>
Convert text as CSV.
-
#convert_as_json(text) ⇒ Array<Hash>
Convert text as JSON.
-
#initialize(uri, http_request_headers: {}, http_request_method: "GET", http_request_payload_type: nil, http_request_payload: {}) ⇒ Reader
constructor
A new instance of Reader.
- #read ⇒ Object
-
#read_file(path) ⇒ Array<Hash>
Read & convert a file.
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_headers ⇒ Object (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_method ⇒ Object (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_payload ⇒ Object (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 |
#uri ⇒ Object (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
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
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 |
#read ⇒ Object
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
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 |