Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/zmb/utils.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get(q = {}) ⇒ Object
- #http(type, q = nil) ⇒ Object
- #plural(amount = 2) ⇒ Object
- #post(q = {}) ⇒ Object
- #split_seperators ⇒ Object
- #truncate_words(num) ⇒ Object
Class Method Details
.random(len = 8) ⇒ Object
4 5 6 7 |
# File 'lib/zmb/utils.rb', line 4 def self.random(len=8) characters = ('a'..'z').to_a + ('1'..'9').to_a (1..len).map{ characters[rand(characters.size)] }.join end |
Instance Method Details
#get(q = {}) ⇒ Object
70 71 72 |
# File 'lib/zmb/utils.rb', line 70 def get(q={}) http('get', q) end |
#http(type, q = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/zmb/utils.rb', line 54 def http(type, q=nil) u = URI.parse(self) http = Net::HTTP.new(u.host, u.port) q = q.to_query_string if q.class == Hash q = u.query unless q http.start do |h| case type when 'get' then h.get(u.path + '?' + q) when 'post' then h.post(u.path, q) when 'head' then h.head(u.path + '?' + q) end end end |
#plural(amount = 2) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/zmb/utils.rb', line 46 def plural(amount=2) if amount == 1 then self else self + 's' end end |
#post(q = {}) ⇒ Object
74 75 76 |
# File 'lib/zmb/utils.rb', line 74 def post(q={}) http('post', q) end |
#split_seperators ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/zmb/utils.rb', line 9 def split_seperators if include?("\n") then split("\n").map{ |arg| arg.strip } elsif include?(',') then split(',').map{ |arg| arg.strip } elsif include?(' ') then split(' ') else [self] end end |
#truncate_words(num) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zmb/utils.rb', line 21 def truncate_words(num) return [self] if size <= num lines = Array.new line = Array.new len = 0 split(' ').each do |word| len += 1 unless len == 0 len += word.size if not len <= num then lines << line.join(' ') line = Array.new len = word.size end line << word end lines << line.join(' ') if lines.size != 0 lines end |