# String Awesome [![Build Status](https://travis-ci.org/tiagopog/string_awesome.png?branch=master)](https://travis-ci.org/tiagopog/string_awesome) [![Code Climate](https://codeclimate.com/repos/52a5452b7e00a4670f00b139/badges/f9183e7116f7e040c184/gpa.png)](https://codeclimate.com/repos/52a5452b7e00a4670f00b139/feed) [![Dependency Status](https://gemnasium.com/tiagopog/string_awesome.png)](https://gemnasium.com/tiagopog/string_awesome) [![Gem Version](https://badge.fury.io/rb/string_awesome.png)](http://badge.fury.io/rb/string_awesome) This gem adds some awesome and easy-to-use extensions to Ruby String class. ## Installation Compatible with Ruby 1.9.3+ Add this line to your application's Gemfile: gem 'string_awesome' And then execute: $ bundle Or install it yourself as: $ gem install string_awesome ## Usage ### HTML #### String#linkify Finds URLs in the text and wrap in anchor tag. ``` ruby 'Awesome site: http://foobar.com'.linkify #=> 'Awesome site: http://foobar.com' 'www.foobar.com'.linkify #=> 'www.foobar.com' 'Awesome site: http://foobar.com'.linkify(class: 'link', truncate: 10) #=> 'Awesome site: http://foo...' 'Awesome site: http://foobar.com'.linkify(class: 'link', target: '_blank') #=> 'Awesome site: http://foobar.com' 'Awesome site: http://foobar.com'.linkify(truncate: { length: 10, html_encoded: true }) #=> 'Awesome site: http://foo…' ``` #### String#tweetify Finds URLs, Twitter handles, hashtags in the text and wrap in anchor tag. ``` ruby 'What about to follow @tiagopog?'.tweetify #=> 'What about to follow @tiagopog?' "Let's code! #rubyrocks".tweetify #=> "Let's code! #rubyrocks" 'Cool link from @tiagopog! http://foobar.com #rubyrocks'.tweetify(only: [:hashtag]) #=> 'Cool link from @tiagopog! http://foobar.com #rubyrocks' 'Cool link from @tiagopog! http://foobar.com #rubyrocks'.tweetify(only: [:hashtag, :tt_handle]) #=> 'Cool link from @tiagopog! http://foobar.com #rubyrocks' ``` #### String#nl2br Replaces \n to \
tags. ``` ruby "Hello world!".nl2br #=> "Hello
world!" ``` #### String#strip_tags Removes all HTML tags from text. ``` ruby '

Aloha!

'.strip_tags #=> 'Aloha!' ``` ### Other Cool Features #### String#ellipsis Appends ellipsis to the text. ``` ruby 'lorem ipsum!'.ellipsis #=> 'lorem...' "It's a very loooooong text!".ellipsis 11 #=> "It's a very..." "It's a very loooooong text!".ellipsis 8, after_a_word: true #=> "It's a..." 'lorem ipsum'.ellipsis(5, html_encoded: true) #=> 'lorem…' ``` #### String#no_accents Removes accents from words in the text. ``` ruby 'lórem ipsùm dólor sìt ãmet!'.no_accents #=> 'lorem ipsum dolor sit amet!' ``` #### String#slugfy Parses the text to a valid format for URLs. ``` ruby 'Lórem IPSUM Dolor?'.slugfy #=> 'lorem-ipsum-dolor' ``` #### String#to_title Converts the string into a title style and prevents other letters in the middle of the word from being uppercase. ``` ruby 'loREm IPsuM DOLOR'.to_title #=> 'Lorem Ipsum Dolor' ``` #### String#reverse_words Reverses a string by words, instead of reversing it by characters like the String#reverse. ``` ruby 'lorem ipsum dolor'.reverse_words #=> 'dolor ipsum lorem' ``` #### String#count_words Counts how many words there are in the text. ``` ruby 'lorem ipsum dolor'.count_words #=> 3 'lorem ipsum dolor'.count_words 7 # it will count words until this length: 7 #=> 1 ``` #### String#first_words Returns an Array with the N first words from the text. ``` ruby 'lorem ipsum dolor'.first_words 2 #=> 'lorem ipsum' ``` #### String#last_words Returns am Array with the N last words from the text. ``` ruby 'lorem ipsum dolor'.last_words 2 #=> 'ipsum dolor' ``` ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request