Class: Lumberg::FormatWhm

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/lumberg/format_whm.rb

Instance Method Summary collapse

Constructor Details

#initialize(env, *args, &block) ⇒ FormatWhm

Returns a new instance of FormatWhm



6
7
8
9
10
11
# File 'lib/lumberg/format_whm.rb', line 6

def initialize(env, *args, &block)
  @type = args[0]
  @key = args[1]
  @boolean_params = args[2]
  super(env)
end

Instance Method Details

#on_complete(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lumberg/format_whm.rb', line 13

def on_complete(env)
  encoding = env[:response_headers]['content-encoding']

  encoding = encoding.to_s.downcase if encoding

  uncompressed_body = case encoding
         when 'gzip'
           env[:response_headers].delete('content-encoding')
           Zlib::GzipReader.new(StringIO.new(env[:body])).read
         when 'deflate'
           env[:response_headers].delete('content-encoding')
           Zlib::Inflate.inflate(env[:body])
         else
           env[:body]
         end

  env[:body] = if @type == :whostmgr
    format_response(uncompressed_body)
  else
    begin
      parsed_body = JSON.parse(uncompressed_body)
      format_response(parsed_body)
    rescue JSON::ParserError
      # In some error cases, cpanel doesn't return JSON :smdh:
      raise Lumberg::WhmConnectionError, uncompressed_body
    end
  end
end

#response_values(env) ⇒ Object



42
43
44
# File 'lib/lumberg/format_whm.rb', line 42

def response_values(env)
  {status: env[:status], headers: env[:response_headers], body: env[:body]}
end