module UmbrellioUtils::Parsing

Constants

CARD_TRUNCATED_PAN_REGEX
RFC_AUTH_HEADERS

Public Instance Methods

card_expiry_time(string, year_format: "%y") click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 32
def card_expiry_time(string, year_format: "%y")
  format_string = "%m/#{year_format}"
  time = suppress(ArgumentError) { Time.zone.strptime(string, format_string) }
  time + 1.month - 1.second if time
end
card_truncated_pan(string) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 28
def card_truncated_pan(string)
  string.gsub(CARD_TRUNCATED_PAN_REGEX, "\\1...\\2")
end
extract_host(string) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 38
def extract_host(string)
  URI(string).host
end
parse_basic_auth(headers) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 42
def parse_basic_auth(headers)
  auth_header = headers.values_at(*RFC_AUTH_HEADERS).compact.first or return
  credentials_b64 = auth_header[/\ABasic (.*)/, 1] or return
  joined_credentials = Base64.strict_decode64(credentials_b64) rescue return

  joined_credentials.split(":")
end
parse_datetime(timestamp, timezone: "UTC", format: nil) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 62
def parse_datetime(timestamp, timezone: "UTC", format: nil)
  return if timestamp.blank?
  tz = ActiveSupport::TimeZone[timezone]
  format ? tz.strptime(timestamp, format) : tz.parse(timestamp)
end
parse_xml(xml, remove_attributes: true, snakecase: true) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 18
def parse_xml(xml, remove_attributes: true, snakecase: true)
  xml = Nokogiri::XML(xml)
  xml.remove_namespaces!
  xml.xpath("//@*").remove if remove_attributes

  tags_converter = snakecase ? -> (tag) { tag.snakecase.to_sym } : -> (tag) { tag.to_sym }
  nori = Nori.new(convert_tags_to: tags_converter, convert_dashes_to_underscores: false)
  nori.parse(xml.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION))
end
safely_parse_base64(string) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 50
def safely_parse_base64(string)
  Base64.strict_decode64(string)
rescue ArgumentError
  nil
end
safely_parse_json(string) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 56
def safely_parse_json(string)
  JSON.parse(string)
rescue JSON::ParserError
  {}
end
sanitize_phone(string, e164_format: false) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 68
def sanitize_phone(string, e164_format: false)
  phone = Phonelib.parse(string)
  return if phone.invalid?
  return phone.e164 if e164_format

  phone.sanitized
end
try_to_parse_as_json(data) click to toggle source
# File lib/umbrellio_utils/parsing.rb, line 14
def try_to_parse_as_json(data)
  JSON.parse(data) rescue data
end