Module: UmbrellioUtils::Parsing

Extended by:
Parsing
Included in:
Parsing
Defined in:
lib/umbrellio_utils/parsing.rb

Constant Summary collapse

RFC_AUTH_HEADERS =
%w[
  HTTP_AUTHORIZATION
  HTTP_X_HTTP_AUTHORIZATION
  HTTP_REDIRECT_X_HTTP_AUTHORIZATION
].freeze
CARD_TRUNCATED_PAN_REGEX =
/\A(\d{6}).*(\d{4})\z/

Instance Method Summary collapse

Instance Method Details

#card_expiry_time(string, year_format: "%y") ⇒ Object



32
33
34
35
36
# 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) ⇒ Object



28
29
30
# 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) ⇒ Object



38
39
40
# File 'lib/umbrellio_utils/parsing.rb', line 38

def extract_host(string)
  URI(string).host
end

#parse_basic_auth(headers) ⇒ Object



42
43
44
45
46
47
48
# 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) ⇒ Object



62
63
64
65
66
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
# 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) { snakecase(tag).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) ⇒ Object



50
51
52
53
54
# 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) ⇒ Object



56
57
58
59
60
# 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) ⇒ Object



68
69
70
71
72
73
74
# 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) ⇒ Object



14
15
16
# File 'lib/umbrellio_utils/parsing.rb', line 14

def try_to_parse_as_json(data)
  JSON.parse(data) rescue data
end