Class: Transcriptic::Client::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/transcriptic/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, protocol) ⇒ Protocol

Returns a new instance of Protocol.



104
105
106
107
# File 'lib/transcriptic/client.rb', line 104

def initialize(client, protocol)
  @client = client
  @protocol = protocol
end

Instance Attribute Details

#attachedObject

Returns the value of attribute attached.



102
103
104
# File 'lib/transcriptic/client.rb', line 102

def attached
  @attached
end

Instance Method Details

#eachObject

Iterate over all output chunks until EOF is reached.



150
151
152
153
154
155
156
# File 'lib/transcriptic/client.rb', line 150

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?

Returns:

  • (Boolean)


126
127
128
# File 'lib/transcriptic/client.rb', line 126

def end_of_stream?
  @next_chunk.nil?
end

#launch(command, attached = false) ⇒ Object

launch the protocol



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/transcriptic/client.rb', line 110

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

#readObject

Read the next chunk of output.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/transcriptic/client.rb', line 131

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_sObject

All output as a string



159
160
161
162
163
# File 'lib/transcriptic/client.rb', line 159

def to_s
  buf = []
  each { |part| buf << part }
  buf.join
end