Class: Transcriptic::Client::Protocol
- Inherits:
-
Object
- Object
- Transcriptic::Client::Protocol
- Defined in:
- lib/transcriptic/client.rb
Instance Attribute Summary collapse
-
#attached ⇒ Object
Returns the value of attribute attached.
-
#upid ⇒ Object
Returns the value of attribute upid.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate over all output chunks until EOF is reached.
-
#end_of_stream? ⇒ Boolean
Does the service have any remaining output?.
-
#initialize(client, protocol, upid = nil) ⇒ Protocol
constructor
A new instance of Protocol.
-
#launch(command, attached = false) ⇒ Object
launch the protocol.
-
#read ⇒ Object
Read the next chunk of output.
-
#to_s ⇒ Object
All output as a string.
Constructor Details
#initialize(client, protocol, upid = nil) ⇒ Protocol
Returns a new instance of Protocol.
69 70 71 72 73 |
# File 'lib/transcriptic/client.rb', line 69 def initialize(client, protocol, upid=nil) @client = client @protocol = protocol @upid = upid end |
Instance Attribute Details
#attached ⇒ Object
Returns the value of attribute attached.
67 68 69 |
# File 'lib/transcriptic/client.rb', line 67 def attached @attached end |
#upid ⇒ Object
Returns the value of attribute upid.
67 68 69 |
# File 'lib/transcriptic/client.rb', line 67 def upid @upid end |
Instance Method Details
#each ⇒ Object
Iterate over all output chunks until EOF is reached.
116 117 118 119 120 121 122 |
# File 'lib/transcriptic/client.rb', line 116 def each until end_of_stream? sleep(@interval) output = read yield output unless output.empty? end end |
#end_of_stream? ⇒ Boolean
Does the service have any remaining output?
92 93 94 |
# File 'lib/transcriptic/client.rb', line 92 def end_of_stream? @next_chunk.nil? end |
#launch(command, attached = false) ⇒ Object
launch the protocol
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/transcriptic/client.rb', line 76 def launch(command, attached=false) @attached = attached @response = @client.post( "/api/runs/#{@app}/confirm", command, :content_type => 'application/json' ) @next_chunk = @response.to_s @interval = 0 self rescue RestClient::RequestFailed => e raise ProtocolException, e.http_body if e.http_code == 502 raise end |
#read ⇒ Object
Read the next chunk of output.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/transcriptic/client.rb', line 97 def read chunk = @client.get(@next_chunk) if chunk.headers[:location].nil? && chunk.code != 204 # no more chunks @next_chunk = nil chunk.to_s elsif chunk.to_s == '' # assume no content and back off @interval = 2 '' elsif location = chunk.headers[:location] # some data read and next chunk available @next_chunk = location @interval = 0 chunk.to_s end end |
#to_s ⇒ Object
All output as a string
125 126 127 128 129 |
# File 'lib/transcriptic/client.rb', line 125 def to_s buf = [] each { |part| buf << part } buf.join end |