module FrilansFinansAPI::ParseLog

Public Class Methods

call(filename) click to toggle source
# File lib/frilans_finans_api/parse_log.rb, line 30
def self.call(filename)
  lines = []
  File.foreach(filename) do |log_line|
    next unless log_line.index('[FrilansFinansAPI::Request]')

    # MATCH BODY
    body_match = 'BODY: '
    body_match_index = log_line.index(body_match)
    body_content_index = body_match_index + body_match.length
    body_json = log_line[body_content_index..-1].strip
    body = JSON.parse(body_json)

    # MATCH STATUS
    status = string_between_markers(log_line, ' STATUS: ', ' BODY: ')

    # MATCH PARAM
    json_string = string_between_markers(log_line, ' PARAMS: ', ' STATUS: ')
    params = JSON.parse(json_string)

    # MATCH URI
    uri = string_between_markers(log_line, ' URI: ', ' PARAMS: ')

    lines << LogRequest.new(uri: uri, params: params, body: body, status: status)
  end
  lines
end
string_between_markers(string, start_marker, end_marker) click to toggle source
# File lib/frilans_finans_api/parse_log.rb, line 57
def self.string_between_markers(string, start_marker, end_marker)
  string[/#{Regexp.escape(start_marker)}(.*?)#{Regexp.escape(end_marker)}/m, 1]
end