Module: URI
- Defined in:
- lib/botr/http/uri_ext.rb
Constant Summary collapse
- TBLENCWWWCOMP__ =
FIX: For some reason, the Bits on the Run API doesn't support the encoding of spaces to "+". As such, (ASCII space) will encode to "%20".
TBLENCWWWCOMP_.dup
Class Method Summary collapse
- .decode_www_form_component(str, enc = Encoding::UTF_8) ⇒ Object
- .encode_www_form_component(str, enc = nil) ⇒ Object
Class Method Details
.decode_www_form_component(str, enc = Encoding::UTF_8) ⇒ Object
23 24 25 26 |
# File 'lib/botr/http/uri_ext.rb', line 23 def self.decode_www_form_component(str, enc=Encoding::UTF_8) raise ArgumentError, "invalid %-encoding (#{str})" unless /\A[^%]*(?:%\h\h[^%]*)*\z/ =~ str str.b.gsub(/\+|%\h\h/, TBLENCWWWCOMP__).force_encoding(enc) end |
.encode_www_form_component(str, enc = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/botr/http/uri_ext.rb', line 9 def self.encode_www_form_component(str, enc=nil) str = str.to_s.dup if str.encoding != Encoding::ASCII_8BIT if enc && enc != Encoding::ASCII_8BIT str.encode!(Encoding::UTF_8, invalid: :replace, undef: :replace) str.encode!(enc, fallback: ->(x){"&#{x.ord};"}) end str.force_encoding(Encoding::ASCII_8BIT) end str.gsub!(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP__) str.force_encoding(Encoding::US_ASCII) end |