Class: MintHttp::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net_response, net_request, mint_request, net_http) ⇒ Response

Returns a new instance of Response.

Parameters:



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mint_http/response.rb', line 65

def initialize(net_response, net_request, mint_request, net_http)
  @net_response = net_response
  @net_request = net_request
  @mint_request = mint_request
  @version = net_response.http_version
  @status_code = net_response.code.to_i
  @status_text = net_response.message
  @headers = Headers.new.merge(net_response.each_header.to_h)

  tcp_socket = net_http.underlying_tcp_socket
  @local_address = tcp_socket.local_address rescue nil
  @remote_address = tcp_socket.remote_address rescue nil
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



31
32
33
# File 'lib/mint_http/response.rb', line 31

def headers
  @headers
end

#local_addressObject (readonly)

Returns the value of attribute local_address.



55
56
57
# File 'lib/mint_http/response.rb', line 55

def local_address
  @local_address
end

#mint_requestObject (readonly)

Returns the value of attribute mint_request.



15
16
17
# File 'lib/mint_http/response.rb', line 15

def mint_request
  @mint_request
end

#net_requestObject (readonly)

Returns the value of attribute net_request.



7
8
9
# File 'lib/mint_http/response.rb', line 7

def net_request
  @net_request
end

#net_responseObject (readonly)

Returns the value of attribute net_response.



11
12
13
# File 'lib/mint_http/response.rb', line 11

def net_response
  @net_response
end

#remote_addressObject (readonly)

Returns the value of attribute remote_address.



59
60
61
# File 'lib/mint_http/response.rb', line 59

def remote_address
  @remote_address
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



23
24
25
# File 'lib/mint_http/response.rb', line 23

def status_code
  @status_code
end

#status_textObject (readonly)

Returns the value of attribute status_text.



27
28
29
# File 'lib/mint_http/response.rb', line 27

def status_text
  @status_text
end

#time_connectedNumeric

Returns:

  • (Numeric)


43
44
45
# File 'lib/mint_http/response.rb', line 43

def time_connected
  @time_connected
end

#time_connectingNumeric

Returns:

  • (Numeric)


51
52
53
# File 'lib/mint_http/response.rb', line 51

def time_connecting
  @time_connecting
end

#time_endedNumeric

Returns:

  • (Numeric)


39
40
41
# File 'lib/mint_http/response.rb', line 39

def time_ended
  @time_ended
end

#time_startedNumeric

Returns:

  • (Numeric)


35
36
37
# File 'lib/mint_http/response.rb', line 35

def time_started
  @time_started
end

#time_totalNumeric

Returns:

  • (Numeric)


47
48
49
# File 'lib/mint_http/response.rb', line 47

def time_total
  @time_total
end

#versionObject (readonly)

Returns the value of attribute version.



19
20
21
# File 'lib/mint_http/response.rb', line 19

def version
  @version
end

Instance Method Details

#bodyObject



130
131
132
# File 'lib/mint_http/response.rb', line 130

def body
  net_response.body
end

#client_error?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/mint_http/response.rb', line 87

def client_error?
  (400..499).include?(@status_code)
end

#https?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/mint_http/response.rb', line 146

def https?
  @net_request
end

#inspectObject



150
151
152
# File 'lib/mint_http/response.rb', line 150

def inspect
  "#<#{self.class}/#{@version} #{@status_code} #{@status_text} #{@headers.inspect}>"
end

#jsonObject



134
135
136
# File 'lib/mint_http/response.rb', line 134

def json
  @json ||= JSON.parse(body)
end

#json?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/mint_http/response.rb', line 138

def json?
  headers['content-type']&.include?('application/json')
end

#not_found?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/mint_http/response.rb', line 99

def not_found?
  @status_code == 404
end

#raise!Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mint_http/response.rb', line 107

def raise!
  case @status_code
    when 401
      raise AuthenticationError.new('Unauthenticated', self)
    when 403
      raise AuthorizationError.new('Forbidden', self)
    when 404
      raise NotFoundError.new('Not Found', self)
    when 502
      raise BadGatewayError.new('Bad Gateway', self)
    when 503
      raise ServiceUnavailableError.new('Service Unavailable', self)
    when 504
      raise GatewayTimeoutError.new('Gateway Timeout', self)
    when 400..499
      raise ClientError.new('Client Error', self)
    when 500..599
      raise ServerError.new('Server Error', self)
    else
      self
  end
end

#redirect?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/mint_http/response.rb', line 83

def redirect?
  (300..399).include?(@status_code)
end

#server_error?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/mint_http/response.rb', line 103

def server_error?
  (500..599).include?(@status_code)
end

#success?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/mint_http/response.rb', line 79

def success?
  (200..299).include?(@status_code)
end

#unauthenticated?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/mint_http/response.rb', line 91

def unauthenticated?
  @status_code == 401
end

#unauthorized?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/mint_http/response.rb', line 95

def unauthorized?
  @status_code == 403
end

#xml?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/mint_http/response.rb', line 142

def xml?
  headers['content-type']&.match?(/\/xml/)
end