utilities.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # encoding: utf-8
  2. module Mail
  3. module Utilities
  4. include Patterns
  5. # Returns true if the string supplied is free from characters not allowed as an ATOM
  6. def atom_safe?( str )
  7. not ATOM_UNSAFE === str
  8. end
  9. # If the string supplied has ATOM unsafe characters in it, will return the string quoted
  10. # in double quotes, otherwise returns the string unmodified
  11. def quote_atom( str )
  12. atom_safe?( str ) ? str : dquote(str)
  13. end
  14. # If the string supplied has PHRASE unsafe characters in it, will return the string quoted
  15. # in double quotes, otherwise returns the string unmodified
  16. def quote_phrase( str )
  17. if RUBY_VERSION >= '1.9'
  18. original_encoding = str.encoding
  19. str.force_encoding('ASCII-8BIT')
  20. if (PHRASE_UNSAFE === str)
  21. dquote(str).force_encoding(original_encoding)
  22. else
  23. str.force_encoding(original_encoding)
  24. end
  25. else
  26. (PHRASE_UNSAFE === str) ? dquote(str) : str
  27. end
  28. end
  29. # Returns true if the string supplied is free from characters not allowed as a TOKEN
  30. def token_safe?( str )
  31. not TOKEN_UNSAFE === str
  32. end
  33. # If the string supplied has TOKEN unsafe characters in it, will return the string quoted
  34. # in double quotes, otherwise returns the string unmodified
  35. def quote_token( str )
  36. token_safe?( str ) ? str : dquote(str)
  37. end
  38. # Wraps supplied string in double quotes unless it is already wrapped.
  39. #
  40. # Additionally will escape any double quotation marks in the string with a single
  41. # backslash in front of the '"' character.
  42. def dquote( str )
  43. match = str.match(/^"(.*)?"$/)
  44. str = match[1] if match
  45. # First remove all escaped double quotes:
  46. str = str.gsub(/\\"/, '"')
  47. # Then wrap and re-escape all double quotes
  48. '"' + str.gsub(/["]/n) {|s| '\\' + s } + '"'
  49. end
  50. # Unwraps supplied string from inside double quotes.
  51. #
  52. # Example:
  53. #
  54. # string = '"This is a string"'
  55. # unquote(string) #=> 'This is a string'
  56. def unquote( str )
  57. match = str.match(/^"(.*?)"$/)
  58. match ? match[1] : str
  59. end
  60. # Wraps a string in parenthesis and escapes any that are in the string itself.
  61. #
  62. # Example:
  63. #
  64. # paren( 'This is a string' ) #=> '(This is a string)'
  65. def paren( str )
  66. RubyVer.paren( str )
  67. end
  68. # Unwraps a string from being wrapped in parenthesis
  69. #
  70. # Example:
  71. #
  72. # str = '(This is a string)'
  73. # unparen( str ) #=> 'This is a string'
  74. def unparen( str )
  75. match = str.match(/^\((.*?)\)$/)
  76. match ? match[1] : str
  77. end
  78. # Wraps a string in angle brackets and escapes any that are in the string itself
  79. #
  80. # Example:
  81. #
  82. # bracket( 'This is a string' ) #=> '<This is a string>'
  83. def bracket( str )
  84. RubyVer.bracket( str )
  85. end
  86. # Unwraps a string from being wrapped in parenthesis
  87. #
  88. # Example:
  89. #
  90. # str = '<This is a string>'
  91. # unbracket( str ) #=> 'This is a string'
  92. def unbracket( str )
  93. match = str.match(/^\<(.*?)\>$/)
  94. match ? match[1] : str
  95. end
  96. # Escape parenthesies in a string
  97. #
  98. # Example:
  99. #
  100. # str = 'This is (a) string'
  101. # escape_paren( str ) #=> 'This is \(a\) string'
  102. def escape_paren( str )
  103. RubyVer.escape_paren( str )
  104. end
  105. def uri_escape( str )
  106. uri_parser.escape(str)
  107. end
  108. def uri_unescape( str )
  109. uri_parser.unescape(str)
  110. end
  111. def uri_parser
  112. @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
  113. end
  114. # Matches two objects with their to_s values case insensitively
  115. #
  116. # Example:
  117. #
  118. # obj2 = "This_is_An_object"
  119. # obj1 = :this_IS_an_object
  120. # match_to_s( obj1, obj2 ) #=> true
  121. def match_to_s( obj1, obj2 )
  122. obj1.to_s.downcase == obj2.to_s.downcase
  123. end
  124. # Capitalizes a string that is joined by hyphens correctly.
  125. #
  126. # Example:
  127. #
  128. # string = 'resent-from-field'
  129. # capitalize_field( string ) #=> 'Resent-From-Field'
  130. def capitalize_field( str )
  131. str.to_s.split("-").map { |v| v.capitalize }.join("-")
  132. end
  133. # Takes an underscored word and turns it into a class name
  134. #
  135. # Example:
  136. #
  137. # constantize("hello") #=> "Hello"
  138. # constantize("hello-there") #=> "HelloThere"
  139. # constantize("hello-there-mate") #=> "HelloThereMate"
  140. def constantize( str )
  141. str.to_s.split(/[-_]/).map { |v| v.capitalize }.to_s
  142. end
  143. # Swaps out all underscores (_) for hyphens (-) good for stringing from symbols
  144. # a field name.
  145. #
  146. # Example:
  147. #
  148. # string = :resent_from_field
  149. # dasherize ( string ) #=> 'resent_from_field'
  150. def dasherize( str )
  151. str.to_s.gsub('_', '-')
  152. end
  153. # Swaps out all hyphens (-) for underscores (_) good for stringing to symbols
  154. # a field name.
  155. #
  156. # Example:
  157. #
  158. # string = :resent_from_field
  159. # underscoreize ( string ) #=> 'resent_from_field'
  160. def underscoreize( str )
  161. str.to_s.downcase.gsub('-', '_')
  162. end
  163. if RUBY_VERSION <= '1.8.6'
  164. def map_lines( str, &block )
  165. results = []
  166. str.each_line do |line|
  167. results << yield(line)
  168. end
  169. results
  170. end
  171. def map_with_index( enum, &block )
  172. results = []
  173. enum.each_with_index do |token, i|
  174. results[i] = yield(token, i)
  175. end
  176. results
  177. end
  178. else
  179. def map_lines( str, &block )
  180. str.each_line.map(&block)
  181. end
  182. def map_with_index( enum, &block )
  183. enum.each_with_index.map(&block)
  184. end
  185. end
  186. end
  187. end