Class: HTTPX::Response::Body

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

Instance Method Summary collapse

Constructor Details

#initialize(response, options) ⇒ Body

Returns a new instance of Body.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/httpx/response.rb', line 117

def initialize(response, options)
  @response = response
  @headers = response.headers
  @options = options
  @threshold_size = options.body_threshold_size
  @window_size = options.window_size
  @encoding = response.content_type.charset || Encoding::BINARY
  @length = 0
  @buffer = nil
  @state = :idle
end

Instance Method Details

#==(other) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'lib/httpx/response.rb', line 219

def ==(other)
  object_id == other.object_id || begin
    if other.respond_to?(:read)
      _with_same_buffer_pos { FileUtils.compare_stream(@buffer, other) }
    else
      to_s == other.to_s
    end
  end
end

#bytesizeObject



149
150
151
# File 'lib/httpx/response.rb', line 149

def bytesize
  @length
end

#closeObject

closes/cleans the buffer, resets everything



209
210
211
212
213
214
215
216
217
# File 'lib/httpx/response.rb', line 209

def close
  if @buffer
    @buffer.close
    @buffer.unlink if @buffer.respond_to?(:unlink)
    @buffer = nil
  end
  @length = 0
  @state = :closed
end

#closed?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/httpx/response.rb', line 129

def closed?
  @state == :closed
end

#copy_to(dest) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/httpx/response.rb', line 196

def copy_to(dest)
  return unless @buffer

  rewind

  if dest.respond_to?(:path) && @buffer.respond_to?(:path)
    FileUtils.mv(@buffer.path, dest.path)
  else
    ::IO.copy_stream(@buffer, dest)
  end
end

#eachObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/httpx/response.rb', line 153

def each
  return enum_for(__method__) unless block_given?

  begin
    if @buffer
      rewind
      while (chunk = @buffer.read(@window_size))
        yield(chunk.force_encoding(@encoding))
      end
    end
  ensure
    close
  end
end

#empty?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/httpx/response.rb', line 192

def empty?
  @length.zero?
end

#inspectObject

:nocov:



230
231
232
233
234
# File 'lib/httpx/response.rb', line 230

def inspect
  "#<HTTPX::Response::Body:#{object_id} " \
    "@state=#{@state} " \
    "@length=#{@length}>"
end

#read(*args) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/httpx/response.rb', line 141

def read(*args)
  return unless @buffer

  rewind

  @buffer.read(*args)
end

#to_sObject Also known as: to_str



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/httpx/response.rb', line 168

def to_s
  case @buffer
  when StringIO
    begin
      @buffer.string.force_encoding(@encoding)
    rescue ArgumentError
      @buffer.string
    end
  when Tempfile, File
    rewind
    content = _with_same_buffer_pos { @buffer.read }
    begin
      content.force_encoding(@encoding)
    rescue ArgumentError # ex: unknown encoding name - utf
      content
    end
  when nil
    "".b
  else
    @buffer
  end
end

#write(chunk) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/httpx/response.rb', line 133

def write(chunk)
  return if @state == :closed

  @length += chunk.bytesize
  transition
  @buffer.write(chunk)
end