class Transcriptic::Client

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('me@example.com', 'mypass')
transcriptic.list_resources
transcriptic.run_info("run-625f827a")

Attributes

api_key[RW]
host[RW]
user[RW]

Public Class Methods

auth(user, password, host = Transcriptic::Auth.default_host) click to toggle source
# File lib/transcriptic/client.rb, line 29
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_string() click to toggle source
# File lib/transcriptic/client.rb, line 23
def self.gem_version_string
  "transcriptic/#{version}"
end
new(user, api_key, host = Transcriptic::Auth.default_host) click to toggle source
# File lib/transcriptic/client.rb, line 37
def initialize(user, api_key, host = Transcriptic::Auth.default_host)
  @user = user
  @api_key = api_key
  @host = host
end
version() click to toggle source
# File lib/transcriptic/client.rb, line 19
def self.version
  Transcriptic::VERSION
end

Public Instance Methods

create_or_get_protocol(labfile, signature) click to toggle source
# File lib/transcriptic/client.rb, line 122
def create_or_get_protocol(labfile, signature)
  if not get_protocol(labfile)
    create_protocol(labfile)
  end
  update_protocol(labfile, signature)
end
create_protocol(labfile) click to toggle source
# File lib/transcriptic/client.rb, line 96
def create_protocol(labfile)
  json_decode post("/api/protocols", {
    "protocol[name]" => labfile.options[:name],
    "protocol[author_email]" => labfile.options[:email],
    "protocol[author_name]" => labfile.options[:author],
    "protocol[description]" => labfile.options[:description]
  }).to_s
end
create_run(fd, project_id) click to toggle source
# File lib/transcriptic/client.rb, line 129
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
extract_warning(response) click to toggle source
# File lib/transcriptic/client.rb, line 217
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_protocol(labfile) click to toggle source
# File lib/transcriptic/client.rb, line 88
def get_protocol(labfile)
  begin
    json_decode get("/api/protocols/#{labfile.options[:name]}").to_s
  rescue RestClient::ResourceNotFound
    false
  end
end
list_resources() click to toggle source
# File lib/transcriptic/client.rb, line 47
def list_resources
  json_decode get("/api/resources.json").to_s
end
list_runs() click to toggle source
# File lib/transcriptic/client.rb, line 55
def list_runs
  json_decode get("/api/runs.json").to_s
end
on_warning(&blk) click to toggle source
# File lib/transcriptic/client.rb, line 169
def on_warning(&blk)
  @warning_callback = blk
end
process(method, uri, extra_headers={}, payload=nil, host=host) click to toggle source
# File lib/transcriptic/client.rb, line 197
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
    Transcriptic.ui.error " !   Unable to connect to #{host}"
  rescue RestClient::SSLCertificateNotVerified => ex
    host = URI.parse(realize_full_uri(uri, host)).host
    Transcriptic.ui.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
read_logs(run_id, options=[]) { |chunk| ... } click to toggle source
# File lib/transcriptic/client.rb, line 137
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) click to toggle source
# File lib/transcriptic/client.rb, line 175
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) click to toggle source
# File lib/transcriptic/client.rb, line 51
def resource_info(id)
  json_decode get("/api/resources/#{id}.json").to_s
end
run_info(id) click to toggle source
# File lib/transcriptic/client.rb, line 59
def run_info(id)
  begin
    json_decode get("/api/runs/#{id}.json").to_s
  rescue RestClient::ResourceNotFound
    false
  end
end
search_resources(term, limit = 10) click to toggle source
# File lib/transcriptic/client.rb, line 43
def search_resources(term, limit = 10)
  json_decode get("/api/resources/search.json?query=#{escape(term)}&limit=#{limit}").to_s
end
sign_upload(labfile, filename) click to toggle source
# File lib/transcriptic/client.rb, line 67
def sign_upload(labfile, filename)
  json_decode get("/api/upload/sign?type=protocol-pkg&group=#{labfile.options[:group]}&name=#{labfile.options[:name]}&filename=#{filename}&version=#{labfile.options[:version]}").to_s
end
update_protocol(labfile, signature) click to toggle source
# File lib/transcriptic/client.rb, line 105
def update_protocol(labfile, signature)
  payload = {
    "protocol[name]" => labfile.options[:name],
    "protocol[author_email]" => labfile.options[:email],
    "protocol[author_name]" => labfile.options[:author],
    "protocol[description]" => labfile.options[:description],
    "protocol[package][key]" => signature["key"],
    "protocol[package][version]" => labfile.options[:version],
    "protocol[package][dependencies]" => labfile.dependencies.to_json
  }
  begin
    json_decode put("/api/protocols/#{labfile.options[:name]}", payload).to_s
  rescue RestClient::UnprocessableEntity => ex
    json_decode ex.response.to_s
  end
end
upload_to_s3(path, signature) click to toggle source
# File lib/transcriptic/client.rb, line 71
def upload_to_s3(path, signature)
  conn = Faraday.new(:url => "https://#{signature["bucket"]}.s3.amazonaws.com") do |faraday|
    faraday.request  :multipart
    faraday.request  :url_encoded
    faraday.adapter  :net_http
  end
  res = conn.post "/", {
    "key" => signature["key"],
    "AWSAccessKeyId" => signature["access_key"],
    "acl" => "private",
    "success_action_status" => signature["success_action_status"],
    "policy" => signature["policy"],
    "signature" => signature["signature"],
    "file" => Faraday::UploadIO.new(path, 'application/jar')
  }
end

Private Instance Methods

default_resource_options_for_uri(uri) click to toggle source
# File lib/transcriptic/client.rb, line 268
def default_resource_options_for_uri(uri)
  if ENV["TRANSCRIPTIC_SSL_VERIFY"] == "disable"
    {}
  elsif realize_full_uri(uri) =~ %r|^https://secure.transcriptic.com|
    # OpenSSL::SSL::VERIFY_PEER
    { }
    #{ :verify_ssl => OpenSSL::SSL::VERIFY_NONE, :ssl_ca_file => local_ca_file }
  else
    {}
  end
end
hash_from_xml_doc(elements) click to toggle source
# File lib/transcriptic/client.rb, line 284
def hash_from_xml_doc(elements)
  elements.inject({}) do |hash, e|
    next(hash) unless e.respond_to?(:children)
    hash.update(e.name.gsub("-","_").to_sym => case e.children.length
      when 0 then nil
      when 1 then e.text
      else hash_from_xml_doc(e.children)
    end)
  end
end
local_ca_file() click to toggle source
# File lib/transcriptic/client.rb, line 280
def local_ca_file
  File.expand_path("../../data/cacert.pem", __FILE__)
end
realize_full_uri(given, host=host) click to toggle source
# File lib/transcriptic/client.rb, line 257
def realize_full_uri(given, host=host)
  full_host = (host =~ /^http/) ? host : "https://www.#{host}"
  host = URI.parse(full_host)
  uri = URI.parse(given)
  uri.host ||= host.host
  uri.scheme ||= host.scheme || "https"
  uri.path = (uri.path[0..0] == "/") ? uri.path : "/#{uri.path}"
  uri.port = host.port if full_host =~ /\:\d+/
  uri.to_s
end