Module: EmmyHttp::Encoders

Extended by:
Encoders
Included in:
Encoders
Defined in:
lib/emmy_http/client/encoders.rb

Instance Method Summary collapse

Instance Method Details

#encode_auth(userinfo) ⇒ Object



29
30
31
# File 'lib/emmy_http/client/encoders.rb', line 29

def encode_auth(userinfo)
  "Basic #{Base64.strict_encode64(userinfo)}"
end

#encode_body(encoding, body) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/emmy_http/client/encoders.rb', line 33

def encode_body(encoding, body)
  require 'zlib'
  require 'stringio'

  case encoding
  when "gzip"
    wio = StringIO.new("w")
    begin
      w_gz = Zlib::GzipWriter.new(wio)
      w_gz.write(body)
    rescue
      raise EmmyHttp::EncoderError
    ensure
      w_gz.close
    end
    wio.string
  else
    body
  end
end

#escape(str) ⇒ Object



5
6
7
# File 'lib/emmy_http/client/encoders.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/encoders.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/encoders.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/encoders.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/encoders.rb', line 13

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