Class: Worochi::Agent::Github::Helper::StreamIO

Inherits:
Object
  • Object
show all
Defined in:
lib/worochi/helper/github_helper.rb

Overview

This is a wrapper that produces a JSON stream that works with ‘Net::HTTP::Post#body_stream`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ StreamIO

Returns a new instance of StreamIO.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/worochi/helper/github_helper.rb', line 13

def initialize(item)
  item.content.rewind
  @parts = [
    StringIO.new('{"content":"'),
    Base64IO.new(item.content),
    StringIO.new('","encoding":"base64"}')
  ]
  @part_no = 0
  @pos = 0
  @size = @parts.inject(0) {|sum, p| sum + p.size}
end

Instance Attribute Details

#sizeInteger (readonly)

Returns size of the JSON.

Returns:

  • (Integer)

    size of the JSON



69
70
71
# File 'lib/worochi/helper/github_helper.rb', line 69

def size
  @size
end

Instance Method Details

#eof?Boolean

Returns file has been fully read.

Returns:

  • (Boolean)

    file has been fully read.



36
37
38
# File 'lib/worochi/helper/github_helper.rb', line 36

def eof?
  @pos >= size
end

#read(length = nil, outbuf = nil) ⇒ String

Parameters:

  • length (Integer) (defaults to: nil)
  • outbuf (IO) (defaults to: nil)

Returns:

  • (String)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/worochi/helper/github_helper.rb', line 43

def read(length=nil, outbuf=nil)
  if eof?
    outbuf.clear if outbuf
    return length.nil? ? '' : nil
  end
  
  length ||= size

  output = ''
  while output.length < length
    curr = @parts[@part_no]
    output += curr.read(length - output.length).to_s
    @part_no += 1 if curr.eof?
    break if @part_no == @parts.size
  end
  @pos += output.length

  unless outbuf.nil?
    outbuf.clear
    outbuf.insert(0, output)
  end

  output
end

#rewindnil

Rewind each component of the stream.

Returns:

  • (nil)


28
29
30
31
32
33
# File 'lib/worochi/helper/github_helper.rb', line 28

def rewind
  @parts.each { |part| part.rewind }
  @part_no = 0
  @pos = 0
  nil
end