Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/mir_utility.rb
Instance Method Summary collapse
-
#add_http_prefix ⇒ Object
Prefixes the given url with ‘http://’.
-
#capitalize_words ⇒ Object
General methods.
-
#expand_address_abbreviations ⇒ Object
Address methods.
- #formatted_phone ⇒ Object
- #formatted_zip ⇒ Object
-
#has_http? ⇒ Boolean
Returns true if a given string begins with http:// or https://.
-
#has_trailing_slash? ⇒ Boolean
Returns true if a given string has a trailing slash.
-
#is_page? ⇒ Boolean
Returns true if a given string refers to an HTML page.
-
#to_12_hour_time ⇒ Object
Time methods.
-
#to_host ⇒ Object
Returns the host from a given URL string; returns nil if the string is not a valid URL.
-
#to_uri ⇒ Object
Returns a URI for the given string; nil if the string is invalid.
-
#valid_http_url? ⇒ Boolean
Returns true if the given string is a valid URL.
Methods included from MirUtility::CoreExtensions::String::NumberHelper
Instance Method Details
#add_http_prefix ⇒ Object
Prefixes the given url with ‘http://’.
797 798 799 800 801 802 |
# File 'lib/mir_utility.rb', line 797 def add_http_prefix return if self.blank? _uri = self.to_uri return self if _uri.nil? || _uri.is_a?(URI::FTP) || _uri.is_a?(URI::HTTP) || _uri.is_a?(URI::HTTPS) || _uri.is_a?(URI::LDAP) || _uri.is_a?(URI::MailTo) "http://#{self}" end |
#capitalize_words ⇒ Object
General methods
724 725 726 |
# File 'lib/mir_utility.rb', line 724 def capitalize_words self.downcase.gsub(/\b([a-z])/) { $1.capitalize }.gsub( "'S", "'s" ) end |
#expand_address_abbreviations ⇒ Object
Address methods
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
# File 'lib/mir_utility.rb', line 730 def _address = self.strip.capitalize_words # NOTE: DO NOT rearrange the replace sequences; order matters! # streets _address.gsub!( /\b(ave|av)\.?\b/i, 'Avenue ' ) _address.gsub!( /\b(blvd|blv|bld|bl)\.?\b/i, 'Boulevard ' ) _address.gsub!( /\bcr\.?\b/i, 'Circle ' ) _address.gsub!( /\bctr\.?\b/i, 'Center ' ) _address.gsub!( /\b(crt|ct)\.?\b/i, 'Court ' ) _address.gsub!( /\bdr\.?\b/i, 'Drive ' ) _address.gsub!( /\b(expressw|expw|expy)\.?\b/i, 'Expressway ' ) _address.gsub!( /\bfrwy\.?\b/i, 'Freeway ' ) _address.gsub!( /\bhwy\.?\b/i, 'Highway ' ) _address.gsub!( /\bln\.?\b/i, 'Lane ' ) _address.gsub!( /\b(prkwy|pkwy|pkw|pky)\.?\b/i, 'Parkway ' ) _address.gsub!( /\bpk\.?\b/i, 'Pike ' ) _address.gsub!( /\bplz\.?\b/i, 'Plaza ' ) _address.gsub!( /\bpl\.?\b/i, 'Place ' ) _address.gsub!( /\brd\.?\b/i, 'Road ' ) _address.gsub!( /\b(rte|rt)\.?\b/i, 'Route ' ) _address.gsub!( /\bste\.?\b/i, 'Suite ' ) _address.gsub!( /\bst\.?\b/i, 'Street ' ) _address.gsub!( /\btrpk\.?\b/i, 'Turnpike ' ) _address.gsub!( /\btr\.?\b/i, 'Trail ' ) # directions _address.gsub!( /\bN\.?e\.?\b/i, 'Northeast ' ) _address.gsub!( /\bS\.?e\.?\b/i, 'Southeast ' ) _address.gsub!( /\bS\.?w\.?\b/i, 'Southwest ' ) _address.gsub!( /\bN\.?w\.?\b/i, 'Northwest ' ) _address.gsub!( /\bN\.?\b/, 'North ' ) _address.gsub!( /\bE\.?\b/, 'East ' ) _address.gsub!( /\bS\.?\b/, 'South ' ) _address.gsub!( /\bW\.?\b/, 'West ' ) _address.gsub!( '.', '' ) _address.gsub!( / +/, ' ' ) _address.strip end |
#formatted_phone ⇒ Object
771 772 773 774 775 776 777 778 779 780 |
# File 'lib/mir_utility.rb', line 771 def formatted_phone if self # remove non-digit characters _self = self.gsub(/[\(\) -]+/, '') # format as phone if 10 digits are left return number_to_phone(_self, :area_code => true ) if !! (_self =~ /[0-9]{10}/) end self end |
#formatted_zip ⇒ Object
782 783 784 785 786 |
# File 'lib/mir_utility.rb', line 782 def formatted_zip return if self.blank? self.gsub!( /[\(\) -]+/, '' ) self.size == 9 ? "#{self[0 .. 4]}-#{self[5 .. -1]}" : self end |
#has_http? ⇒ Boolean
Returns true if a given string begins with http:// or https://.
805 806 807 |
# File 'lib/mir_utility.rb', line 805 def has_http? !! (self =~ /^http[s]?:\/\/.+/) end |
#has_trailing_slash? ⇒ Boolean
Returns true if a given string has a trailing slash.
810 811 812 |
# File 'lib/mir_utility.rb', line 810 def has_trailing_slash? !! (self =~ /\/$/) end |
#is_page? ⇒ Boolean
Returns true if a given string refers to an HTML page.
815 816 817 |
# File 'lib/mir_utility.rb', line 815 def is_page? !! (self =~ /\.htm[l]?$/) end |
#to_12_hour_time ⇒ Object
Time methods
790 791 792 |
# File 'lib/mir_utility.rb', line 790 def to_12_hour_time (self == '0' || self.blank?) ? nil : Time.parse( "#{self[0..-3]}:#{self[-2..-1]}" ).to_s( :time ).gsub(/^0/, '') end |
#to_host ⇒ Object
Returns the host from a given URL string; returns nil if the string is not a valid URL.
820 821 822 823 |
# File 'lib/mir_utility.rb', line 820 def to_host _uri = self.to_uri _uri ? _uri.host : nil end |
#to_uri ⇒ Object
Returns a URI for the given string; nil if the string is invalid.
826 827 828 829 830 831 832 833 834 |
# File 'lib/mir_utility.rb', line 826 def to_uri begin _uri = URI.parse self rescue URI::InvalidURIError RAILS_DEFAULT_LOGGER.warn "#{self} is an invalid URI!" end _uri end |
#valid_http_url? ⇒ Boolean
Returns true if the given string is a valid URL.
837 838 839 |
# File 'lib/mir_utility.rb', line 837 def valid_http_url? self.scan(/:\/\//).size == 1 && self.to_uri.is_a?(URI::HTTP) end |