Class: String

Inherits:
Object show all
Defined in:
lib/string.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


14
# File 'lib/string.rb', line 14

def blank?; nil? || empty? end

#camelcaseObject



48
49
50
# File 'lib/string.rb', line 48

def camelcase
  self.gsub( /(\A|_)[[:lower:]]+/ ) {|m| m.gsub(/^_/,'').capitalize }
end

#from_jsonObject



15
16
17
18
# File 'lib/string.rb', line 15

def from_json
  t = JSON.parse( self ) rescue {}
  safe_parse_hash( t )
end

#from_msgpackObject



23
24
25
26
# File 'lib/string.rb', line 23

def from_msgpack
  t = MessagePack.unpack( self ) rescue {}
  safe_parse_hash( t )
end

#from_xmlObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/string.rb', line 30

def from_xml
  # Защита от кулхацкеров
  if self =~ /<!DOCTYPE[^>]+>|xsi:schemaLocation\s*=|<xs:schema|xmlns:xs=/
    raise RuntimeError.new('Invalid XML data provided')
  end
  # На всякий случай удаляем заголовок, могут быть глюки у Nori
  Nori.new(
    :convert_tags_to => lambda { |tag| tag.snakecase.to_sym }
    ).parse self.gsub(/^<\?xml[^>]+>\n*/i,'')
end

#from_yamlObject



19
20
21
22
# File 'lib/string.rb', line 19

def from_yaml
  t = YAML.load( self ) rescue {}
  safe_parse_hash( t )
end

#from_zlibObject



27
28
29
# File 'lib/string.rb', line 27

def from_zlib
  Zlib::Inflate.inflate self
end

#like_number?Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/string.rb', line 9

def like_number?
  x = self.gsub( /^([+-]?)\./, "#{ $1 }0." )
  !! ( x =~ /^[-+]?(\d+)(\.\d+(e[-+]?\d+)?)?$/ )
end

#percent_encodeObject



55
56
57
58
# File 'lib/string.rb', line 55

def percent_encode
  @@percent_encode_table ||= [ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45,0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B,0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65,0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B,0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71,0x72, 0x73, 0x74, 0x75, 0x76, 0x77,0x78, 0x79, 0x7A, 0x2D, 0x2E, 0x5F, 0x7E ].freeze
  self.each_byte.collect{|i| @@percent_encode_table.include?(i) ? ("%c" % i) : ("%%%02X" % i) }.join()
end

#present?Boolean

Returns:

  • (Boolean)


13
# File 'lib/string.rb', line 13

def present?; ! empty?; end

#safe_parse_hash(t) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/string.rb', line 60

def safe_parse_hash( t )
  case t
  when Array
    t.collect{|item| item.is_a?( Hash ) ? item.keys_to_symbols : item }
  when Hash
    t.keys_to_symbols
  else
    t
  end
end

#snakecaseObject



44
45
46
# File 'lib/string.rb', line 44

def snakecase
  self.gsub( /[[:upper:]]+/) {|m| "_#{ m.downcase }" }.gsub(/\A_/,'')
end

#to_aObject



40
41
42
# File 'lib/string.rb', line 40

def to_a
  [self]
end