/lib/wwmd/class_extensions/extensions_base.rb

http://github.com/miketracy/wwmd · Ruby · 252 lines · 170 code · 41 blank · 41 comment · 11 complexity · b276662cf31d9da7c852b0437c1cab0a MD5 · raw file

  1. require 'htmlentities'
  2. =begin rdoc
  3. let's re-open everything!
  4. =end
  5. require 'uri'
  6. class Numeric
  7. # return binary representation of <tt>length</tt> size padded with \x00
  8. # length: length in bytes to return (padded with least signficant \x00
  9. # reverse: reverse the byte order
  10. def to_bin (len,rev = false)
  11. str = ""
  12. bignum = self
  13. 1.upto(len) do |i|
  14. str << (bignum & 0xFF).to_n8
  15. bignum = bignum >> 8
  16. end
  17. return str.reverse if rev
  18. return str
  19. end
  20. # integer to ip address
  21. def int_to_ip
  22. [24, 16, 8, 0].map { |b| (self >> b) & 255 }.join('.')
  23. end
  24. # integer to mac address [uses ':' as delimiter]
  25. def int_to_mac
  26. [40,32,24,16,8,0].map { |b| ((self >> b) & 255).to_s(16).rjust(2,"0") }.join(":")
  27. end
  28. end
  29. class String
  30. def hexify
  31. self.unpack("H*").first.upcase
  32. end
  33. def unhexify
  34. [self].pack("H*")
  35. end
  36. alias_method :dehexify,:unhexify
  37. def strip_up
  38. self.gsub(/[^\x20-\x7e,\n]/,"").gsub(/^\n/,"")
  39. end
  40. # ip address to int
  41. def ip_to_int
  42. self.split('.').inject(0) { |a,e| (a << 8) + e.to_i }
  43. end
  44. # mac address to int [uses ':' as delimiter]
  45. def mac_to_int
  46. self.split(':').inject(0) { |a,e| (a << 8) + e.to_i(16) }
  47. end
  48. # return true or false for <tt>string.match</tt>
  49. def contains?(rexp)
  50. return !self.match(rexp).nil?
  51. end
  52. # strip the string and return true if empty
  53. def empty?
  54. return self.strip == ''
  55. end
  56. # return everything in the string (url) before the first get param
  57. ## "http://foo.bar.com/page.asp?somearg=foo&otherarg=bar".clip
  58. ## => "http://foo.bar.com/page.asp"
  59. def clip(pref="?")
  60. if (v = self.index(pref))
  61. return self[0..(v-1)]
  62. end
  63. return self
  64. end
  65. # return everything in the string (url) after the first get parameter
  66. # without the leading '?'
  67. #
  68. # pass true as the second param to also get back the ?
  69. ## "http://foo.bar.com/page.asp?somearg=foo&otherarg=bar".clop
  70. ## => "somearg=foo&otherarg=bar"
  71. def clop(pref="?",preftoo=false)
  72. (preftoo == false) ? add = "" : add = pref
  73. if (v = self.index(pref))
  74. return add + self[(v+1)..-1]
  75. end
  76. return nil
  77. end
  78. def clopp; self.clop("?",true); end #:nodoc:
  79. def clopa
  80. return [self.clip,self.clop]
  81. end
  82. alias_method :clipa, :clopa
  83. # File.dirname with a trailing slash
  84. def dirname
  85. return self if self.match(/\/$/)
  86. File.dirname(self) + "/"
  87. end
  88. # File.basename
  89. def basename(ext=nil)
  90. if ext
  91. File.basename(self,ext)
  92. else
  93. File.basename(self)
  94. end
  95. end
  96. def extname
  97. self.split('.').last
  98. end
  99. # write string to passed filename
  100. # if filename is nil? will raise an error
  101. def write(fname=nil)
  102. raise "filename required" unless fname
  103. File.write(fname,self)
  104. self
  105. end
  106. # parse passed GET param string into a form and return the FormArray object
  107. def to_form(action=nil)
  108. if self.split("\n").size > 1
  109. return self.to_form_from_show
  110. end
  111. ret = FormArray.new
  112. self.split("&").each do |x|
  113. y = x.split("=",2)
  114. ret.add(y[0].to_s,y[1].to_s)
  115. # ret[y[0].to_s] = y[1].to_s
  116. # ret.extend!(y[0].to_s,y[1].to_s)
  117. end
  118. ret.action = action if action
  119. return ret
  120. end
  121. def to_form_from_show
  122. self.split("\n").map { |a|
  123. key,val = a.split("=",2)
  124. key = key.split(" ")[-1]
  125. val = val.strip if val
  126. ["#{key}=#{val}"]
  127. }.join("&").to_form.squeeze_keys!
  128. end
  129. def mform
  130. return self.gsub("\n","").to_form
  131. end
  132. def to_form_from_req
  133. # self.split("\x0d\x0a\x0d\x0a")[1].to_form
  134. self.split("\n\n")[1].to_form
  135. end
  136. alias_method :to_ffr, :to_form_from_req
  137. # create filename from url changing "/" to "_"
  138. def to_fn(ext=nil)
  139. ret = self.clip.split("/")[3..-1].join("_")
  140. ret += ".#{ext}" if not ext.nil?
  141. return ret
  142. end
  143. # strip html tags from string
  144. def strip_html
  145. self.gsub(/<\/?[^>]*>/, "")
  146. end
  147. # range or int
  148. def head(c=5)
  149. if c.kind_of?(Range) then
  150. range = c
  151. else
  152. range = (0..(c - 1))
  153. end
  154. self.split("\n")[range].join("\n")
  155. end
  156. # return a literal regexp object for this string
  157. #
  158. # escape regexp operators
  159. def to_regexp
  160. return Regexp.new(self.gsub(/([\[\]\{\}\(\)\*\$\?])/) { |x| '\\' + x })
  161. end
  162. # check if this string is a guid
  163. def is_guid?
  164. begin
  165. Guid.from_s(self)
  166. rescue => e
  167. return false
  168. end
  169. return true
  170. end
  171. def md5
  172. Digest::MD5.digest(self).hexify
  173. end
  174. def sha1
  175. Digest::SHA1.digest(self).hexify
  176. end
  177. def sha256
  178. Digest::SHA256.digest(self).hexify
  179. end
  180. def sha512
  181. Digest::SHA512.digest(self).hexify
  182. end
  183. def pbcopy
  184. IO.popen('pbcopy', 'r+') { |c| c.print self }
  185. end
  186. end
  187. class Array
  188. # grep each element of an array for the passed regular expression
  189. # and return an Array of matches
  190. # (only works one deep)
  191. def each_grep(regex)
  192. ret = []
  193. self.each { |e| ret << e.grep(regex) }
  194. return ret
  195. end
  196. # join the array with "\n" and write to a file
  197. def to_file(filename)
  198. File.write(filename,self.join("\n"))
  199. end
  200. end
  201. class File
  202. # write string to file
  203. def self.write(filename,contents)
  204. fout = File.open(filename,"w")
  205. fout.print contents
  206. fout.close
  207. end
  208. end
  209. def pbpaste
  210. %x[pbpaste]
  211. end