Class: GData::HTTP::MimeBodyString

Inherits:
Object
  • Object
show all
Defined in:
lib/gdata/http/mime_body.rb

Overview

Class makes a string into a stream-like object

Instance Method Summary collapse

Constructor Details

#initialize(source_string) ⇒ MimeBodyString

Returns a new instance of MimeBodyString.



76
77
78
79
# File 'lib/gdata/http/mime_body.rb', line 76

def initialize(source_string)
  @string = source_string
  @bytes_read = 0
end

Instance Method Details

#read(bytes_requested) ⇒ Object

Implement read so that this class can be treated as a stream.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gdata/http/mime_body.rb', line 82

def read(bytes_requested)
  if RUBY_VERSION < '1.9'
    max_bytes = @string.length
  else
    max_bytes = @string.bytesize
  end
  if @bytes_read == max_bytes
    return false
  elsif bytes_requested > max_bytes - @bytes_read
    bytes_requested = max_bytes - @bytes_read
  end
  
  buffer = @string[@bytes_read, bytes_requested]
  @bytes_read += bytes_requested
  
  return buffer
end