Class: FocusNfe::HTTP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/focus_nfe/http/response.rb

Overview

Wrapper imutável de uma resposta HTTP da API Focus NFe.

Expõe status, headers (leitura case-insensitive), body (JSON parseado quando o Content-Type indica JSON, senão a string crua) e raw_body (sempre a string original). O parsing é eager no construtor — a instância é congelada, então não há memoização preguiçosa.

Defined Under Namespace

Classes: Headers

Constant Summary collapse

JSON_TYPE =

Returns reconhece Content-Type JSON (incluindo sufixos +.../...+json+).

Returns:

  • (Regexp)

    reconhece Content-Type JSON (incluindo sufixos +.../...+json+)

%r{\bapplication/(?:[\w.+-]+\+)?json\b}i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, headers: {}, body: nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • status (Integer)

    código de status HTTP

  • headers (Hash) (defaults to: {})

    cabeçalhos da resposta

  • body (String, nil) (defaults to: nil)

    corpo cru recebido



45
46
47
48
49
50
51
# File 'lib/focus_nfe/http/response.rb', line 45

def initialize(status:, headers: {}, body: nil)
  @status = status
  @headers = Headers.new(headers)
  @raw_body = body
  @body = parse_body
  freeze
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



40
41
42
# File 'lib/focus_nfe/http/response.rb', line 40

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



40
41
42
# File 'lib/focus_nfe/http/response.rb', line 40

def headers
  @headers
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



40
41
42
# File 'lib/focus_nfe/http/response.rb', line 40

def raw_body
  @raw_body
end

#statusObject (readonly)

Returns the value of attribute status.



40
41
42
# File 'lib/focus_nfe/http/response.rb', line 40

def status
  @status
end

Instance Method Details

#success?Boolean

Returns true quando o status está na faixa 2xx.

Returns:

  • (Boolean)

    true quando o status está na faixa 2xx



54
55
56
# File 'lib/focus_nfe/http/response.rb', line 54

def success?
  status.between?(200, 299)
end