Module: Watobo::Mixin::Transcoders
- Defined in:
- lib/watobo/mixins/transcoders.rb
Instance Method Summary collapse
- #b64decode ⇒ Object
- #b64encode ⇒ Object
- #hex2int ⇒ Object
- #hexdecode ⇒ Object
- #hexencode ⇒ Object
- #url_decode ⇒ Object
- #url_encode ⇒ Object
Instance Method Details
#b64decode ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/watobo/mixins/transcoders.rb', line 13 def b64decode err_count = 0 begin b64string = self.force_encoding('ASCII-8BIT') #rs = Base64.strict_decode64(b64string) # using regular decode64 because of JWT (JSON Web Tokens) decoding rs = Base64.decode64(b64string) return rs rescue #b64string.gsub!(/.$/,'') #err_count += 1 #retry if err_count < 4 return self.to_s end end |
#b64encode ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/watobo/mixins/transcoders.rb', line 29 def b64encode begin plain = self.force_encoding('ASCII-8BIT') #rs = Base64.strict_encode64(plain) rs = Base64.strict_encode64(plain) # we only need a simple string without linebreaks #rs.gsub!(/\n/,'') #rs.strip! return rs rescue return self.to_s end end |
#hex2int ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/watobo/mixins/transcoders.rb', line 43 def hex2int begin plain = self.strip if plain =~ /^[0-9a-fA-F]{1,8}$/ then return plain.hex else return "" end rescue return "" end end |
#hexdecode ⇒ Object
66 67 68 69 |
# File 'lib/watobo/mixins/transcoders.rb', line 66 def hexdecode [ self ].pack("H*") end |
#hexencode ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/watobo/mixins/transcoders.rb', line 56 def hexencode begin self.unpack("H*")[0] rescue return "" end end |
#url_decode ⇒ Object
9 10 11 |
# File 'lib/watobo/mixins/transcoders.rb', line 9 def url_decode CGI::unescape(self) end |
#url_encode ⇒ Object
5 6 7 |
# File 'lib/watobo/mixins/transcoders.rb', line 5 def url_encode CGI::escape(self) end |