Class: PasteHub::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/pastehub/util.rb

Instance Method Summary collapse

Constructor Details

#initializeUtil

Returns a new instance of Util.



15
16
# File 'lib/pastehub/util.rb', line 15

def initialize()
end

Instance Method Details

#currentSecondsObject



30
31
32
# File 'lib/pastehub/util.rb', line 30

def currentSeconds( )
  self.key_seconds( self.currentTime() )
end

#currentTimeObject

return the currentTime in Unixtime



24
25
26
27
28
# File 'lib/pastehub/util.rb', line 24

def currentTime( )
  dt = Time.new.gmtime.to_datetime()
  currentDate = dt.strftime( "%s" ) + "=" + dt.strftime( "%F.%H:%M:%S" )
  currentDate
end

#diffList(list1, list2) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/pastehub/util.rb', line 34

def diffList( list1, list2 )
  set1 = Set.new
  set2 = Set.new
  list1.each { |e| set1.add(e) }
  list2.each { |e| set2.add(e) }
  set1.difference( set2 ).to_a
end

#digest(str) ⇒ Object

return message digest for str.



19
20
21
# File 'lib/pastehub/util.rb', line 19

def digest( str )
  Digest::SHA1.hexdigest( str )
end

#dropList(list1, num) ⇒ Object

Same as Gauche’s drop* function



54
55
56
57
58
59
60
61
62
# File 'lib/pastehub/util.rb', line 54

def dropList( list1, num )
  if num < 0
    list1
  elsif num <= list1.size
    list1[ num .. list1.size ]
  else
    []
  end
end

#pulloutURL(str) ⇒ Object

pullout URL string from the string



74
75
76
77
78
79
80
81
# File 'lib/pastehub/util.rb', line 74

def pulloutURL( str )
  m = str.match( /(^|[\t\s])(http[s]?:\/\/[^\t\s]+)/ )
  if m
    return m[2]
  else
    return nil
  end
end

#stringLimit(str, limit) ⇒ Object

Cut string with limit characters



65
66
67
68
69
70
71
# File 'lib/pastehub/util.rb', line 65

def stringLimit( str, limit )
  if limit < str.size()
    str[0...(limit)] + "..."
  else
    str
  end
end

#takeList(list1, num) ⇒ Object

Same as Gauche’s take* function



43
44
45
46
47
48
49
50
51
# File 'lib/pastehub/util.rb', line 43

def takeList( list1, num )
  if ( num < 0 )
    list1
  elsif num <= list1.size
    list1[ 0 ... num ]
  else
    list1
  end
end