Class: EmtApi::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/emt_api/parser.rb

Class Method Summary collapse

Class Method Details

.add_spaces_between_separators(sentence) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/emt_api/parser.rb', line 19

def self.add_spaces_between_separators(sentence)
  spaced_sentence = sentence

  # Add space before the '-' character
  spaced_sentence.gsub!(/(?!\s)\-/, ' -')

  # Add space after the '-' character
  spaced_sentence.gsub!(/\-(?!\s)/, '- ')

  # Add space after the '.' used for abbreviations
  spaced_sentence.gsub!(/\.(?!\s)/, '. ')

  spaced_sentence
end

.capitalize_sentence(sentence) ⇒ Object



34
35
36
# File 'lib/emt_api/parser.rb', line 34

def self.capitalize_sentence(sentence)
  sentence.split.map { |word| word.capitalize }.join(' ')
end

.parse_date(date_string) ⇒ Object



4
5
6
7
# File 'lib/emt_api/parser.rb', line 4

def self.parse_date(date_string)
  date_string = remove_trailing_whitespaces(date_string)
  Date.parse(date_string)
end

.parse_sentence(sentence) ⇒ Object



9
10
11
12
13
# File 'lib/emt_api/parser.rb', line 9

def self.parse_sentence(sentence)
  sentence = remove_trailing_whitespaces(sentence)
  sentence = add_spaces_between_separators(sentence)
  capitalize_sentence(sentence)
end

.remove_trailing_whitespaces(sentence) ⇒ Object



15
16
17
# File 'lib/emt_api/parser.rb', line 15

def self.remove_trailing_whitespaces(sentence)
  sentence.rstrip
end