Class: Faraday::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/better-faraday.rb

Instance Method Summary collapse

Instance Method Details

#assert_200!Object



87
88
89
# File 'lib/better-faraday.rb', line 87

def assert_200!
  assert_status! 200
end

#assert_2xx!Object



83
84
85
# File 'lib/better-faraday.rb', line 83

def assert_2xx!
  assert_status!(200..299)
end

#assert_status!(code_or_range) ⇒ Object

Raises:

  • (klass)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/better-faraday.rb', line 63

def assert_status!(code_or_range)
  within_range = if Range === code_or_range
    status.in?(code_or_range)
  else
    status == code_or_range
  end

  return self if within_range

  klass = if status_4xx?
    "BetterFaraday::HTTP#{status}".safe_constantize || BetterFaraday::HTTP4xx
  elsif status_5xx?
    "BetterFaraday::HTTP#{status}".safe_constantize || BetterFaraday::HTTP5xx
  else
    BetterFaraday::HTTPError
  end

  raise klass.new(self)
end

#inspectObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/better-faraday.rb', line 107

def inspect
  @inspection_text ||= begin
    request_headers = bf_protect_data(env.bf_request_headers.dup)
    request_body = env.bf_request_body.yield_self { |body| String === body ? body : body.to_s }
    request_body_bytes_count = env.bf_request_body.bytesize
    response_body = env.body.yield_self { |body| String === body ? body : body.to_s }
    response_body_bytes_count = response_body.bytesize

    if env.bf_request_headers["content-type"].to_s.match?(/\bapplication\/json\b/i)
      request_json = bf_json_parse(request_body).yield_self do |data|
        bf_json_dump(bf_protect_data(data)) if data
      end
    end

    if env.response_headers
      response_headers = bf_protect_data(env.response_headers.to_hash)
    end

    if env.response_headers && env.response_headers["content-type"].to_s.match?(/\bapplication\/json\b/i)
      response_json = bf_json_parse(response_body).yield_self do |data|
        bf_json_dump(bf_protect_data(data)) if data
      end
    end

    lines = [
      "-- #{status} #{reason_phrase} --".upcase,
      "",
      "-- Request URL --",
      env.url.to_s,
      "",
      "-- Request Method --",
      env.method.to_s.upcase,
      "",
      "-- Request Headers --",
      bf_json_dump(request_headers).truncate(2048, omission: "... (truncated)"),
      "",

      %[-- Request Body (#{request_body_bytes_count} #{"byte".pluralize(request_body_bytes_count)}) --],
      if request_json
        request_json
      else
        # String#inspect returns \x{XXXX} for the encoding other than Unicode.
        # [1..-2] removed leading and trailing " added by String#inspect.
        # gsub(/\\"/, "\"") unescapes ".
        request_body.inspect.gsub(/\\"/, "\"")[1..-2]
      end.truncate(2048, omission: "... (truncated)"),
      "",

      "-- Request Sent At --",
      env.bf_request_sent_at.strftime("%Y-%m-%d %H:%M:%S.%3N") + " UTC",
      "",

      "-- Response Headers --",
      if response_headers
        bf_json_dump(response_headers)
      else
        env.response_headers.to_s.inspect.gsub(/\\"/, "\"")[1..-2]
      end.truncate(2048, omission: "... (truncated)"),
      "",

      %[-- Response Body (#{response_body_bytes_count} #{"byte".pluralize(response_body_bytes_count)}) --],
      if response_json
        response_json
      else
        response_body.inspect.gsub(/\\"/, "\"")[1..-2]
      end.truncate(2048, omission: "... (truncated)"),
      ""
    ]

    if env.bf_response_received_at
      lines.concat [
        "-- Response Received At --",
        env.bf_response_received_at.strftime("%Y-%m-%d %H:%M:%S.%3N") + " UTC",
        "",
        "-- Response Received In --",
        "#{((env.bf_response_received_at.to_f - env.bf_request_sent_at.to_f) * 1000.0).ceil(3)}ms",
        ""
      ]
    end

    lines.join("\n").freeze
  end

  @inspection_text.dup
end

#status_2xx?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/better-faraday.rb', line 91

def status_2xx?
  status >= 200 && status <= 299
end

#status_3xx?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/better-faraday.rb', line 95

def status_3xx?
  status >= 300 && status <= 399
end

#status_4xx?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/better-faraday.rb', line 99

def status_4xx?
  status >= 400 && status <= 499
end

#status_5xx?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/better-faraday.rb', line 103

def status_5xx?
  status >= 500 && status <= 599
end