-
Notifications
You must be signed in to change notification settings - Fork 1
/
Formatter.rb
42 lines (35 loc) · 1.23 KB
/
Formatter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require 'cgi'
module Formatter
# validate date formats: YYYY-MM-D
def self.validate_dates(dates)
date_format = /(2009|201[0-5])-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])/
dates.each do |d|
date_format.match(d).nil? \
? false : true
end
end
# encode a string for use in rest call
def self.url_encode(string)
string = CGI.escape string
end
def self.remove_non_ascii(string)
encoding_options = {
:invalid => :replace, # Replace invalid byte sequences
:undef => :replace, # Replace anything not defined in ASCII
:replace => '', # Use a blank for those replacements
:universal_newline => true # Always break lines with \n
}
string.encode(Encoding.find('ASCII'), encoding_options)
end
def self.remove_escape_sequence(string)
new_string = string.gsub("\n","")
end
# prevent non-standard characters from being URL-encoded improperly by adding escape-slashes
# when refactoring -- make sure not to use gsub! as it may alter the original API data
def self.escape_characters(string)
['&','?','|','!',"'",'+'].each do |syn_char|
string = string.gsub(syn_char,'\\\\' + "#{syn_char}")
end
return string
end
end