Class: Transcriptic::Client

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

Overview

A Ruby class to call the Transcriptic REST API. You might use this if you want to manage your Transcriptic apps from within a Ruby program, such as Capistrano.

Example:

require 'transcriptic'
transcriptic = Transcriptic::Client.new('[email protected]', 'mypass')
transcriptic.list_resources
transcriptic.run_info("run-625f827a")

Defined Under Namespace

Modules: JSON Classes: Protocol, ProtocolException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UI

#arrow, #confirm, #confirm_billing, #confirm_command, #confirm_quote, #deprecate, disable_error_capture, #display, #display_row, #display_table, enable_error_capture, #error, #error_with_failure, extended, extended_into, #fail, #format_bytes, #format_date, #format_with_bang, #get_terminal_environment, included, included_into, #indent, #info, #json_decode, #json_encode, #longest, #mute!, #output, #output_with_arrow, #output_with_bang, #output_with_indent, #quantify, #quiet?, #redisplay, #say, #say_status, #set_buffer, #status, #string_distance, #suggestion, #time_ago, #truncate, #unmute!, #wait_spinner, #warn

Constructor Details

#initialize(user, api_key, host = Transcriptic::Auth.default_host) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
# File 'lib/transcriptic/client.rb', line 29

def initialize(user, api_key, host = Transcriptic::Auth.default_host)
  @user = user
  @api_key = api_key
  @host = host
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



22
23
24
# File 'lib/transcriptic/client.rb', line 22

def api_key
  @api_key
end

#hostObject

Returns the value of attribute host.



22
23
24
# File 'lib/transcriptic/client.rb', line 22

def host
  @host
end

#userObject

Returns the value of attribute user.



22
23
24
# File 'lib/transcriptic/client.rb', line 22

def user
  @user
end

Class Method Details

.auth(user, password, host = Transcriptic::Auth.default_host) ⇒ Object



24
25
26
27
# File 'lib/transcriptic/client.rb', line 24

def self.auth(user, password, host = Transcriptic::Auth.default_host)
  client = new(user, nil, host)
  json_decode client.post('/users/sign_in', { 'user[email]' => user, 'user[password]' => password }, :accept => 'json').to_s
end

.gem_version_stringObject



18
19
20
# File 'lib/transcriptic/client.rb', line 18

def self.gem_version_string
  "transcriptic/#{version}"
end

.versionObject



14
15
16
# File 'lib/transcriptic/client.rb', line 14

def self.version
  Transcriptic::VERSION
end

Instance Method Details

#create_run(fd, project_id) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/transcriptic/client.rb', line 55

def create_run(fd, project_id)
  payload = {
    'zipdata' => Base64.encode64(File.open(fd).read),
    'project_id' => project_id
  }
  json_decode post("/api/runs", payload, { 'Content-Type' => 'application/zip; charset=UTF-8' }).to_s
end

#delete(uri, extra_headers = {}) ⇒ Object

:nodoc:



190
191
192
# File 'lib/transcriptic/client.rb', line 190

def delete(uri, extra_headers={})    # :nodoc:
  process(:delete, uri, extra_headers)
end

#escape(value) ⇒ Object

:nodoc:



241
242
243
244
# File 'lib/transcriptic/client.rb', line 241

def escape(value)  # :nodoc:
  escaped = URI.escape(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  escaped.gsub('.', '%2E') # not covered by the previous URI.escape
end

#extract_warning(response) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/transcriptic/client.rb', line 214

def extract_warning(response)
  return unless response
  if response.headers[:x_transcriptic_warning] && @warning_callback
    warning = response.headers[:x_transcriptic_warning]
    @displayed_warnings ||= {}
    unless @displayed_warnings[warning]
      @warning_callback.call(warning)
      @displayed_warnings[warning] = true
    end
  end
end

#get(uri, extra_headers = {}) ⇒ Object

:nodoc:



178
179
180
# File 'lib/transcriptic/client.rb', line 178

def get(uri, extra_headers={})    # :nodoc:
  process(:get, uri, extra_headers)
end

#list_resourcesObject



39
40
41
# File 'lib/transcriptic/client.rb', line 39

def list_resources
  json_decode get("/api/resources.json").to_s
end

#list_runsObject



47
48
49
# File 'lib/transcriptic/client.rb', line 47

def list_runs
  json_decode get("/api/runs.json").to_s
end

#on_warning(&blk) ⇒ Object



166
167
168
# File 'lib/transcriptic/client.rb', line 166

def on_warning(&blk)
  @warning_callback = blk
end

#post(uri, payload = "", extra_headers = {}, host = host) ⇒ Object

:nodoc:



182
183
184
# File 'lib/transcriptic/client.rb', line 182

def post(uri, payload="", extra_headers={}, host=host)    # :nodoc:
  process(:post, uri, extra_headers, payload, host)
end

#process(method, uri, extra_headers = {}, payload = nil, host = host) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/transcriptic/client.rb', line 194

def process(method, uri, extra_headers={}, payload=nil, host=host)
  headers  = transcriptic_headers.merge(extra_headers)
  args     = [method, payload, headers].compact

  resource_options = default_resource_options_for_uri(uri)

  begin
    response = resource(uri, resource_options, host).send(*args)
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError
    host = URI.parse(realize_full_uri(uri, host)).host
    error " !   Unable to connect to #{host}"
  rescue RestClient::SSLCertificateNotVerified => ex
    host = URI.parse(realize_full_uri(uri, host)).host
    error "WARNING: Unable to verify SSL certificate for #{host}\nTo disable SSL verification, run with TRANSCRIPTIC_SSL_VERIFY=disable"
  end

  extract_warning(response)
  response
end

#protocol(run_id) ⇒ Object



63
64
65
# File 'lib/transcriptic/client.rb', line 63

def protocol(run_id)
  Protocol.new(self, run_id)
end

#put(uri, payload, extra_headers = {}) ⇒ Object

:nodoc:



186
187
188
# File 'lib/transcriptic/client.rb', line 186

def put(uri, payload, extra_headers={})    # :nodoc:
  process(:put, uri, extra_headers, payload)
end

#read_logs(run_id, options = []) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/transcriptic/client.rb', line 67

def read_logs(run_id, options=[])
  query = "&" + options.join("&") unless options.empty?
  url = get("/api/runs/#{run_id}/logs.json?#{query}").to_s
  if 'not_found' == url
    error "Run #{run_id} not found!"
  end

  uri  = URI.parse(url);
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  http.read_timeout = 60 * 60 * 24

  begin
    http.start do
      http.request_get(uri.path + (uri.query ? "?" + uri.query : "")) do |request|
        request.read_body do |chunk|
          yield chunk
        end
      end
    end
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError
    abort(" !    Could not connect to logging service")
  rescue Timeout::Error, EOFError
    abort("\n !    Request timed out")
  end
end

#resource(uri, options = {}, host = host) ⇒ Object



172
173
174
175
176
# File 'lib/transcriptic/client.rb', line 172

def resource(uri, options={}, host=host)
  RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy']
  resource = RestClient::Resource.new(realize_full_uri(uri, host), options)
  resource
end

#resource_info(id) ⇒ Object



43
44
45
# File 'lib/transcriptic/client.rb', line 43

def resource_info(id)
  json_decode get("/api/resources/#{id}.json").to_s
end

#run_info(id) ⇒ Object



51
52
53
# File 'lib/transcriptic/client.rb', line 51

def run_info(id)
  json_decode get("/api/runs/#{id}.json").to_s
end

#search_resources(term, limit = 10) ⇒ Object



35
36
37
# File 'lib/transcriptic/client.rb', line 35

def search_resources(term, limit = 10)
  json_decode get("/api/resources/search.json?query=#{escape(term)}&limit=#{limit}").to_s
end

#transcriptic_headersObject

:nodoc:



226
227
228
229
230
231
232
233
234
235
# File 'lib/transcriptic/client.rb', line 226

def transcriptic_headers   # :nodoc:
  headers = {
    'X-Transcriptic-API-Version' => '1',
    'User-Agent'                 => self.class.gem_version_string,
    'X-Ruby-Version'             => RUBY_VERSION,
    'X-Ruby-Platform'            => RUBY_PLATFORM
  }
  headers = headers.merge({'Authorization' => "Token token=\"#{@api_key}\""}) if @api_key
  headers
end

#xml(raw) ⇒ Object

:nodoc:



237
238
239
# File 'lib/transcriptic/client.rb', line 237

def xml(raw)   # :nodoc:
  REXML::Document.new(raw)
end