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, 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

#attachedObject

Returns the value of attribute attached.



67
68
69
# File 'lib/transcriptic/client.rb', line 67

def attached
  @attached
end

#upidObject

Returns the value of attribute upid.



67
68
69
# File 'lib/transcriptic/client.rb', line 67

def upid
  @upid
end

Instance Method Details

#eachObject

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?

Returns:

  • (Boolean)


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

#readObject

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_sObject

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