Class: Transcriptic::Client
- Inherits:
-
Object
- Object
- Transcriptic::Client
- 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.create('myapp')
Defined Under Namespace
Modules: JSON Classes: Protocol, ProtocolException
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#host ⇒ Object
Returns the value of attribute host.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .auth(user, password, host = Transcriptic::Auth.default_host) ⇒ Object
- .gem_version_string ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
- #create_run(fd, project_id) ⇒ Object
-
#delete(uri, extra_headers = {}) ⇒ Object
:nodoc:.
- #delete_dataset(dataset_id) ⇒ Object
-
#escape(value) ⇒ Object
:nodoc:.
- #extract_warning(response) ⇒ Object
-
#get(uri, extra_headers = {}) ⇒ Object
:nodoc:.
- #get_dataset_info(dataset_id) ⇒ Object
-
#info(name_or_domain) ⇒ Object
Show info such as mode, custom domain, and collaborators on an app.
-
#initialize(user, api_key, host = Transcriptic::Auth.default_host) ⇒ Client
constructor
A new instance of Client.
- #launch_run(run_id) ⇒ Object
-
#list ⇒ Object
Show a list of projects which you are a collaborator on.
- #list_datasets(run_id = nil, project_id = nil) ⇒ Object
- #list_projects ⇒ Object
- #on_warning(&blk) ⇒ Object
-
#post(uri, payload = "", extra_headers = {}, host = host) ⇒ Object
:nodoc:.
- #process(method, uri, extra_headers = {}, payload = nil, host = host) ⇒ Object
- #project_info(name) ⇒ Object
- #protocol(run_id, upid) ⇒ Object
-
#put(uri, payload, extra_headers = {}) ⇒ Object
:nodoc:.
- #read_logs(run_id, options = []) ⇒ Object
- #resource(uri, options = {}, host = host) ⇒ Object
- #status(run_id) ⇒ Object
-
#transcriptic_headers ⇒ Object
:nodoc:.
-
#xml(raw) ⇒ Object
:nodoc:.
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, #json_decode, #json_encode, #longest, #mute!, #output, #output_with_arrow, #output_with_bang, #output_with_indent, #quantify, #quiet?, #redisplay, #say, #say_status, #set_buffer, #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.
28 29 30 31 32 |
# File 'lib/transcriptic/client.rb', line 28 def initialize(user, api_key, host = Transcriptic::Auth.default_host) @user = user @api_key = api_key @host = host end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
21 22 23 |
# File 'lib/transcriptic/client.rb', line 21 def api_key @api_key end |
#host ⇒ Object
Returns the value of attribute host.
21 22 23 |
# File 'lib/transcriptic/client.rb', line 21 def host @host end |
#user ⇒ Object
Returns the value of attribute user.
21 22 23 |
# File 'lib/transcriptic/client.rb', line 21 def user @user end |
Class Method Details
.auth(user, password, host = Transcriptic::Auth.default_host) ⇒ Object
23 24 25 26 |
# File 'lib/transcriptic/client.rb', line 23 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 ⇒ Object
17 18 19 |
# File 'lib/transcriptic/client.rb', line 17 def self.gem_version_string "transcriptic-gem/#{version}" end |
.version ⇒ Object
13 14 15 |
# File 'lib/transcriptic/client.rb', line 13 def self.version Transcriptic::VERSION end |
Instance Method Details
#create_run(fd, project_id) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/transcriptic/client.rb', line 136 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:
208 209 210 |
# File 'lib/transcriptic/client.rb', line 208 def delete(uri, extra_headers={}) # :nodoc: process(:delete, uri, extra_headers) end |
#delete_dataset(dataset_id) ⇒ Object
57 58 59 |
# File 'lib/transcriptic/client.rb', line 57 def delete_dataset(dataset_id) json_decode delete("/api/datasets/#{dataset_id}").to_s end |
#escape(value) ⇒ Object
:nodoc:
259 260 261 262 |
# File 'lib/transcriptic/client.rb', line 259 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
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/transcriptic/client.rb', line 232 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:
196 197 198 |
# File 'lib/transcriptic/client.rb', line 196 def get(uri, extra_headers={}) # :nodoc: process(:get, uri, extra_headers) end |
#get_dataset_info(dataset_id) ⇒ Object
53 54 55 |
# File 'lib/transcriptic/client.rb', line 53 def get_dataset_info(dataset_id) json_decode get("/api/datasets/#{dataset_id}").to_s end |
#info(name_or_domain) ⇒ Object
Show info such as mode, custom domain, and collaborators on an app.
62 63 64 |
# File 'lib/transcriptic/client.rb', line 62 def info(name_or_domain) name_or_domain = name_or_domain.gsub(/^(http:\/\/)?(www\.)?/, '') end |
#launch_run(run_id) ⇒ Object
144 145 146 |
# File 'lib/transcriptic/client.rb', line 144 def launch_run(run_id) json_decode post("/api/runs/#{run_id}/confirm").to_s end |
#list ⇒ Object
Show a list of projects which you are a collaborator on.
45 46 47 |
# File 'lib/transcriptic/client.rb', line 45 def list json_decode get('/api/runs.json').to_s end |
#list_datasets(run_id = nil, project_id = nil) ⇒ Object
49 50 51 |
# File 'lib/transcriptic/client.rb', line 49 def list_datasets(run_id = nil, project_id = nil) json_decode get("/api/datasets.json?run_id=#{run_id}&project_id=#{project_id}").to_s end |
#list_projects ⇒ Object
34 35 36 |
# File 'lib/transcriptic/client.rb', line 34 def list_projects json_decode get("/api/projects.json").to_s end |
#on_warning(&blk) ⇒ Object
184 185 186 |
# File 'lib/transcriptic/client.rb', line 184 def on_warning(&blk) @warning_callback = blk end |
#post(uri, payload = "", extra_headers = {}, host = host) ⇒ Object
:nodoc:
200 201 202 |
# File 'lib/transcriptic/client.rb', line 200 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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/transcriptic/client.rb', line 212 def process(method, uri, extra_headers={}, payload=nil, host=host) headers = transcriptic_headers.merge(extra_headers) args = [method, payload, headers].compact = (uri) begin response = resource(uri, , 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 |
#project_info(name) ⇒ Object
38 39 40 |
# File 'lib/transcriptic/client.rb', line 38 def project_info(name) json_decode get("/api/projects/#{name}.json").to_s end |
#protocol(run_id, upid) ⇒ Object
148 149 150 |
# File 'lib/transcriptic/client.rb', line 148 def protocol(run_id, upid) Protocol.new(self, run_id, upid) end |
#put(uri, payload, extra_headers = {}) ⇒ Object
:nodoc:
204 205 206 |
# File 'lib/transcriptic/client.rb', line 204 def put(uri, payload, extra_headers={}) # :nodoc: process(:put, uri, extra_headers, payload) end |
#read_logs(run_id, options = []) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/transcriptic/client.rb', line 152 def read_logs(run_id, =[]) query = "&" + .join("&") unless .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
190 191 192 193 194 |
# File 'lib/transcriptic/client.rb', line 190 def resource(uri, ={}, host=host) RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] resource = RestClient::Resource.new(realize_full_uri(uri, host), ) resource end |
#status(run_id) ⇒ Object
132 133 134 |
# File 'lib/transcriptic/client.rb', line 132 def status(run_id) json_decode get("/api/runs/#{run_id}").to_s end |
#transcriptic_headers ⇒ Object
:nodoc:
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/transcriptic/client.rb', line 244 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:
255 256 257 |
# File 'lib/transcriptic/client.rb', line 255 def xml(raw) # :nodoc: REXML::Document.new(raw) end |