Module: EmmyHttp::Encoding

Extended by:
Encoding
Included in:
Encoding
Defined in:
lib/emmy_http/client/encoding.rb

Instance Method Summary collapse

Instance Method Details

#escape(str) ⇒ Object



5
6
7
# File 'lib/emmy_http/client/encoding.rb', line 5

def escape(str)
  str.gsub(/([^a-zA-Z0-9_.-]+)/) { '%'+$1.unpack('H2'*$1.bytesize).join('%').upcase }
end

#param(k, v) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/emmy_http/client/encoding.rb', line 21

def param(k, v)
  if v.is_a?(Array)
    v.map { |e| escape(k) + "[]=" + escape(e) }.join("&")
  else
    escape(k) + "=" + escape(v)
  end
end

#query(query) ⇒ Object



17
18
19
# File 'lib/emmy_http/client/encoding.rb', line 17

def query(query)
  query.map { |k, v| param(k, v) }.join('&')
end

#rfc3986(str) ⇒ Object



9
10
11
# File 'lib/emmy_http/client/encoding.rb', line 9

def rfc3986(str)
  str.gsub(/([!'()*]+)/m) { '%'+$1.unpack('H2'*$1.bytesize).join('%').upcase }
end

#www_form(enum) ⇒ Object



13
14
15
# File 'lib/emmy_http/client/encoding.rb', line 13

def www_form(enum)
  URI.encode_www_form(enum)
end