Class: Juxta
- Inherits:
-
Object
- Object
- Juxta
- Defined in:
- lib/juxta.rb
Overview
This class provides the Ruby interface to the JuxtaWS REST web service.
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
The connection object for this Juxta instance.
-
#logging ⇒ Object
Set to true to enable logging.
Instance Method Summary collapse
-
#async_collate_set(set_id) ⇒ String
Collate the specified comparison set.
-
#async_get_as_html(asset_id) ⇒ String
Retrieve the HTML for a given asset.
-
#async_get_as_json(asset_id) ⇒ String
Retrieve the JSON for a given asset.
-
#async_tokenize_set(set_id) ⇒ String
Tokenize the specified comparison set.
-
#collate_set(set_id) ⇒ Object
Collate the specified comparison set and wait for the response from server.
-
#create_alignments(set_id, json) ⇒ String
Create new alignments, which are associations of annotations in a comparison set.
-
#create_annotations(set_id, witness_id, json) ⇒ String
Create new annotations, on a given witness in a comparison set.
-
#create_sources(source_array) ⇒ Array
Command the server to create source files from the provided array of hashes.
-
#create_witness_set(file_list) ⇒ Array, String
Create an uncollated comparison set from an array of local files.
-
#create_workspace(workspace_id) ⇒ String
Create a new workspace.
-
#create_xslt(name, xslt) ⇒ String
Create a new XSLT on the server.
-
#delete_alignment(set_id, alignment_id) ⇒ Object
Delete the specified alignment from a given comparison set.
-
#delete_annotation(set_id, witness_id, annotation_id) ⇒ Object
Delete the specified annotation from a given comparison set witness.
-
#delete_set(set_id) ⇒ Object
Delete the specified comparison set.
-
#delete_source(source_id) ⇒ Object
Delete the specified source.
-
#delete_witness(witness_id) ⇒ Object
Delete the specified witness.
-
#delete_workspace(workspace_id) ⇒ Object
Delete a new workspace and everything in it.
-
#delete_xslt(xslt_id) ⇒ Object
Delete the specified XSLT.
-
#export(set_id, base_id) ⇒ Object
TEI Parallel segmentation export.
-
#get_alignment(set_id, alignment_id) ⇒ Hash
Get full information about a given alignment.
-
#get_annotation(set_id, witness_id, annotation_id) ⇒ Hash
Retrieve information about a specific annotation.
-
#get_as_html(asset_id) ⇒ Object
Retrieve the HTML for a given asset, waiting for server to prepare asset if necessary.
-
#get_as_json(asset_id) ⇒ Object
Retrieve the JSON for a given asset, waiting for server to prepare asset if necessary.
-
#get_heatmap_asset_id(set_id, base_id) ⇒ String
Creates an asset identifier for the heatmap of a given base text in a comparison set.
-
#get_heatmap_url(set_id, base_id) ⇒ String
Creates a URL for the heatmap of a given base text in a comparison set.
-
#get_set(set_id) ⇒ Hash
Get full information about a given comparison set.
-
#get_side_by_side_asset_id(set_id, witness_a, witness_b) ⇒ String
Creates an asset identifier for a side-by-side comparison of two witnesses in a comparison set.
-
#get_side_by_side_url(set_id, witness_a, witness_b) ⇒ String
Creates a URL for a side-by-side comparison of two witnesses in a comparison set.
-
#get_source(source_id) ⇒ Hash
Get full information about a given source.
-
#get_status(task_id) ⇒ String
Retrieve the status of a server side task.
-
#get_witness(witness_id) ⇒ Hash
Get full information about a given witness.
-
#get_witness_fragment(witness_id, start_point, end_point) ⇒ String
Retrieve a text fragment from a given witness.
-
#get_xslt(asset_id) ⇒ String
Get the specified XSLT resource from server.
-
#initialize(url, username = nil, password = nil) ⇒ Connection
constructor
Initialize a Juxta interface with optional authentication.
-
#list_alignments(set_id) ⇒ Array
List the alignments for a given comparison set.
-
#list_annotations(set_id, witness_id) ⇒ Array
List the annotations applied to a given witness in a comparison set.
-
#list_sets ⇒ Array
List the comparison sets in this workspace.
-
#list_sources ⇒ Array
List the sources in this workspace.
-
#list_witnesses ⇒ Array
List the witnesses in this workspace.
-
#list_workspaces ⇒ Array
Lists the workspaces available on this server.
-
#list_xslt ⇒ Array
List the XSL stylesheets in this workspace.
-
#make_set(witness_ids) ⇒ String
Group the specified witnesses into a new comparison set.
-
#obtain_source_from_url(url) ⇒ String
Command the server to obtain an XML source file from the specified URL.
-
#rename_witness(witness_id, new_name) ⇒ Hash
Change the name of the witness.
-
#search(query) ⇒ Array
Search the current workspace for the specified phrase.
-
#select_workspace(workspace_name) ⇒ Object
Select a workspace to operate in.
-
#tokenize_set(set_id) ⇒ Object
Tokenize the specified comparison set.
-
#transform_source(source_id) ⇒ String
Transform the specified source into a witness using the associated XSLT.
-
#upload_source(file_name) ⇒ String
Upload a file from local disk to the server.
-
#workspace ⇒ String
The name of the currently selected workspace.
Constructor Details
#initialize(url, username = nil, password = nil) ⇒ Connection
Initialize a Juxta interface with optional authentication.
23 24 25 26 |
# File 'lib/juxta.rb', line 23 def initialize( url, username=nil, password=nil ) @logging = false @connection = Connection.new( url, username, password ) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
The connection object for this Juxta instance.
15 16 17 |
# File 'lib/juxta.rb', line 15 def connection @connection end |
#logging ⇒ Object
Set to true to enable logging.
12 13 14 |
# File 'lib/juxta.rb', line 12 def logging @logging end |
Instance Method Details
#async_collate_set(set_id) ⇒ String
Collate the specified comparison set. Returns immediately, use get_status(task_id) to check the status of collation.
408 409 410 411 |
# File 'lib/juxta.rb', line 408 def async_collate_set( set_id ) ( "Collating witness set #{set_id}..." ) unless @logging == false @connection.post( "set/#{set_id}/collate", nil) end |
#async_get_as_html(asset_id) ⇒ String
Retrieve the HTML for a given asset. Returns immediately, use get_status(task_id) to check status.
458 459 460 461 462 463 464 465 |
# File 'lib/juxta.rb', line 458 def async_get_as_html( asset_id ) ( "Getting html #{asset_id}..." ) unless @logging == false resp = @connection.get_html( asset_id ) if resp.include?('RENDERING') == true return resp.split( ' ' )[ 1 ] end return nil end |
#async_get_as_json(asset_id) ⇒ String
Retrieve the JSON for a given asset. Returns immediately, use get_status(task_id) to check status.
535 536 537 538 539 540 541 542 |
# File 'lib/juxta.rb', line 535 def async_get_as_json( asset_id ) ( "Getting json #{asset_id}..." ) unless @logging == false resp = @connection.get( asset_id ) if resp['status'] == 'RENDERING' return resp['taskId'] end return nil end |
#async_tokenize_set(set_id) ⇒ String
Tokenize the specified comparison set. Returns immediately, use get_status(task_id) to check the status of tokenization. Tokens are accessible as annotations of type ‘token’.
378 379 380 381 |
# File 'lib/juxta.rb', line 378 def async_tokenize_set( set_id ) ( "Tokenizing witness set #{set_id}..." ) unless @logging == false @connection.post( "set/#{set_id}/tokenize", nil) end |
#collate_set(set_id) ⇒ Object
Collate the specified comparison set and wait for the response from server.
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/juxta.rb', line 417 def collate_set( set_id ) task_id = async_collate_set( set_id ) while true do sleep(2.0) resp = get_status( task_id ) case resp when 'COMPLETE' return true when 'FAILED' ( "failed to tokenize set: #{set_id}") return false end end end |
#create_alignments(set_id, json) ⇒ String
Create new alignments, which are associations of annotations in a comparison set.
212 213 214 215 216 217 |
# File 'lib/juxta.rb', line 212 def create_alignments( set_id, json ) asset_id = "set/#{set_id}/alignment" ( "Posting alignments to #{asset_id}..." ) unless @logging == false resp = @connection.post(asset_id, json) return resp end |
#create_annotations(set_id, witness_id, json) ⇒ String
Create new annotations, on a given witness in a comparison set.
177 178 179 180 181 182 |
# File 'lib/juxta.rb', line 177 def create_annotations( set_id, witness_id, json ) asset_id = "set/#{set_id}/witness/#{witness_id}/annotation" ( "Posting annotations to #{asset_id}..." ) unless @logging == false resp = @connection.post(asset_id, json) return resp end |
#create_sources(source_array) ⇒ Array
Command the server to create source files from the provided array of hashes.
318 319 320 321 322 |
# File 'lib/juxta.rb', line 318 def create_sources( source_array ) ( "Creating sources from JSON data..." ) unless @logging == false resp = @connection.post( "source", source_array ) JSON.parse(resp) end |
#create_witness_set(file_list) ⇒ Array, String
Create an uncollated comparison set from an array of local files.
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/juxta.rb', line 589 def create_witness_set( file_list ) src_ids = [] wit_ids = [] file_list.each do |file| # create source src_id = upload_source( file ) src_ids.push( src_id ) # xform to witness wit_id = transform_source( src_id ) wit_ids.push( wit_id ) end # create set from witnesses set_id = make_set( wit_ids ) return src_ids, wit_ids, set_id end |
#create_workspace(workspace_id) ⇒ String
Create a new workspace.
65 66 67 68 69 70 |
# File 'lib/juxta.rb', line 65 def create_workspace( workspace_id ) ( "Creating workspace #{workspace_id} ..." ) unless @logging == false json = { "name" => workspace_id, "description" => "the description for #{workspace_id}" } workspace_id = @connection.post( "workspace", json, false ) return workspace_id end |
#create_xslt(name, xslt) ⇒ String
Create a new XSLT on the server.
149 150 151 |
# File 'lib/juxta.rb', line 149 def create_xslt( name, xslt ) @connection.post("xslt", {:name=>name, :xslt=>xslt } ) end |
#delete_alignment(set_id, alignment_id) ⇒ Object
Delete the specified alignment from a given comparison set.
287 288 289 |
# File 'lib/juxta.rb', line 287 def delete_alignment( set_id, alignment_id ) return delete_asset( "set/#{set_id}/alignment/#{alignment_id}" ) end |
#delete_annotation(set_id, witness_id, annotation_id) ⇒ Object
Delete the specified annotation from a given comparison set witness.
278 279 280 |
# File 'lib/juxta.rb', line 278 def delete_annotation( set_id, witness_id, annotation_id ) return delete_asset( "set/#{set_id}/witness/#{witness_id}/annotation/#{annotation_id}" ) end |
#delete_set(set_id) ⇒ Object
Delete the specified comparison set.
268 269 270 |
# File 'lib/juxta.rb', line 268 def delete_set( set_id ) return delete_asset( "set/#{set_id}" ) end |
#delete_source(source_id) ⇒ Object
Delete the specified source.
252 253 254 |
# File 'lib/juxta.rb', line 252 def delete_source( source_id ) return delete_asset( "source/#{source_id}" ) end |
#delete_witness(witness_id) ⇒ Object
Delete the specified witness.
244 245 246 |
# File 'lib/juxta.rb', line 244 def delete_witness( witness_id ) return delete_asset( "witness/#{witness_id}" ) end |
#delete_workspace(workspace_id) ⇒ Object
Delete a new workspace and everything in it.
76 77 78 79 80 81 82 83 84 |
# File 'lib/juxta.rb', line 76 def delete_workspace( workspace_id ) ( "Deleting workspace #{workspace_id} ..." ) unless @logging == false resp = @connection.delete( "workspace/#{workspace_id}", false ) if resp['status'] == 'FAILED' ( "failed to delete asset: #{asset_id}") return false end return true end |
#delete_xslt(xslt_id) ⇒ Object
Delete the specified XSLT.
260 261 262 |
# File 'lib/juxta.rb', line 260 def delete_xslt( xslt_id ) return delete_asset( "xslt/#{xslt_id}" ) end |
#export(set_id, base_id) ⇒ Object
TEI Parallel segmentation export
577 578 579 580 581 |
# File 'lib/juxta.rb', line 577 def export( set_id, base_id ) ( "Exporting set #{set_id}..." ) unless @logging == false resp = @connection.get_xml( "set/#{set_id}/export?mode=teips&base=#{base_id}&sync" ) return resp end |
#get_alignment(set_id, alignment_id) ⇒ Hash
Get full information about a given alignment.
234 235 236 237 238 |
# File 'lib/juxta.rb', line 234 def get_alignment( set_id, alignment_id ) asset_id = "set/#{set_id}/alignment/#{alignment_id}" ( "Getting alignment #{asser_id}..." ) unless @logging == false @connection.get( "#{asset_id}" ) end |
#get_annotation(set_id, witness_id, annotation_id) ⇒ Hash
Retrieve information about a specific annotation.
202 203 204 205 |
# File 'lib/juxta.rb', line 202 def get_annotation( set_id, witness_id, annotation_id ) ( "Getting annotation #{annotation_id}..." ) unless @logging == false @connection.get( "set/#{set_id}/witness/#{witness_id}/annotation/#{annotation_id}?content=YES" ) end |
#get_as_html(asset_id) ⇒ Object
Retrieve the HTML for a given asset, waiting for server to prepare asset if necessary.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/juxta.rb', line 511 def get_as_html( asset_id ) task_id = async_get_as_html( asset_id ) if task_id.nil? == false while true do sleep(2.0) resp = get_status( task_id ) case resp when 'COMPLETE' return @connection.get_html( asset_id ) when 'FAILED' ( "failed to get html asset: #{asset_id}") return false end end end return resp end |
#get_as_json(asset_id) ⇒ Object
Retrieve the JSON for a given asset, waiting for server to prepare asset if necessary.
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/juxta.rb', line 548 def get_as_json( asset_id ) task_id = async_get_as_json( asset_id ) if task_id.nil? == false while true do sleep(2.0) resp = get_status( task_id ) case resp when 'COMPLETE' return @connection.get( asset_id ) when 'FAILED' ( "failed to get json asset: #{asset_id}") return false end end end return false end |
#get_heatmap_asset_id(set_id, base_id) ⇒ String
Creates an asset identifier for the heatmap of a given base text in a comparison set. Use get_as_html or get_as_json to retrieve the asset.
473 474 475 |
# File 'lib/juxta.rb', line 473 def get_heatmap_asset_id( set_id, base_id ) "set/#{set_id}/view?mode=heatmap&base=#{base_id}" end |
#get_heatmap_url(set_id, base_id) ⇒ String
Creates a URL for the heatmap of a given base text in a comparison set.
493 494 495 |
# File 'lib/juxta.rb', line 493 def get_heatmap_url( set_id, base_id ) @connection.make_full_url( get_heatmap_asset_id(set_id,base_id) ) end |
#get_set(set_id) ⇒ Hash
Get full information about a given comparison set.
166 167 168 169 |
# File 'lib/juxta.rb', line 166 def get_set( set_id ) ( "Getting set #{set_id}..." ) unless @logging == false @connection.get( "set/#{set_id}" ) end |
#get_side_by_side_asset_id(set_id, witness_a, witness_b) ⇒ String
Creates an asset identifier for a side-by-side comparison of two witnesses in a comparison set. Use get_as_html or get_as_json to retrieve the asset.
484 485 486 |
# File 'lib/juxta.rb', line 484 def get_side_by_side_asset_id( set_id, witness_a, witness_b ) "set/#{set_id}/view?mode=sidebyside&docs=#{witness_a},#{witness_b}" end |
#get_side_by_side_url(set_id, witness_a, witness_b) ⇒ String
Creates a URL for a side-by-side comparison of two witnesses in a comparison set.
503 504 505 |
# File 'lib/juxta.rb', line 503 def get_side_by_side_url( set_id, witness_a, witness_b ) @connection.make_full_url( get_side_by_side_asset_id(set_id, witness_a, witness_b) ) end |
#get_source(source_id) ⇒ Hash
Get full information about a given source.
107 108 109 110 |
# File 'lib/juxta.rb', line 107 def get_source( source_id ) ( "Getting source #{source_id}..." ) unless @logging == false @connection.get( "source/#{source_id}" ) end |
#get_status(task_id) ⇒ String
Retrieve the status of a server side task.
447 448 449 450 451 |
# File 'lib/juxta.rb', line 447 def get_status( task_id ) ( "Getting status for #{task_id}..." ) unless @logging == false resp = @connection.get("task/#{task_id}/status") return resp['status'] end |
#get_witness(witness_id) ⇒ Hash
Get full information about a given witness.
98 99 100 101 |
# File 'lib/juxta.rb', line 98 def get_witness( witness_id ) ( "Getting witness #{witness_id}..." ) unless @logging == false @connection.get( "witness/#{witness_id}" ) end |
#get_witness_fragment(witness_id, start_point, end_point) ⇒ String
Retrieve a text fragment from a given witness.
118 119 120 121 |
# File 'lib/juxta.rb', line 118 def get_witness_fragment( witness_id, start_point, end_point ) ( "Getting witness #{witness_id}..." ) unless @logging == false @connection.get_html( "witness/#{witness_id}.txt?range=#{start_point},#{end_point}" ) end |
#get_xslt(asset_id) ⇒ String
Get the specified XSLT resource from server.
571 572 573 574 |
# File 'lib/juxta.rb', line 571 def get_xslt( asset_id ) ( "Getting xslt #{asset_id}..." ) unless @logging == false @connection.get_html( "xslt/#{asset_id}" ) end |
#list_alignments(set_id) ⇒ Array
List the alignments for a given comparison set.
223 224 225 226 227 |
# File 'lib/juxta.rb', line 223 def list_alignments( set_id ) asset_id = "set/#{set_id}" ( "Listing alignments for #{asset_id}..." ) unless @logging == false @connection.get( "#{asset_id}/alignment" ) end |
#list_annotations(set_id, witness_id) ⇒ Array
List the annotations applied to a given witness in a comparison set.
189 190 191 192 193 194 |
# File 'lib/juxta.rb', line 189 def list_annotations( set_id, witness_id ) asset_id = "set/#{set_id}/witness/#{witness_id}" ( "Listing annotations for #{asset_id}..." ) unless @logging == false annotation_list = @connection.get( "#{asset_id}/annotation" ) return annotation_list end |
#list_sets ⇒ Array
List the comparison sets in this workspace.
355 356 357 358 359 |
# File 'lib/juxta.rb', line 355 def list_sets ( "Listing sets..." ) unless @logging == false set_list = @connection.get( "set" ) return set_list end |
#list_sources ⇒ Array
List the sources in this workspace.
138 139 140 141 142 |
# File 'lib/juxta.rb', line 138 def list_sources ( "Listing sources..." ) unless @logging == false source_list = @connection.get( "source" ) return source_list end |
#list_witnesses ⇒ Array
List the witnesses in this workspace. Witnesses are the transformed version of the sources.
89 90 91 92 |
# File 'lib/juxta.rb', line 89 def list_witnesses ( "Listing witnesses..." ) unless @logging == false @connection.get( "witness" ) end |
#list_workspaces ⇒ Array
Lists the workspaces available on this server.
55 56 57 58 59 |
# File 'lib/juxta.rb', line 55 def list_workspaces ( "Listing workspaces..." ) unless @logging == false ws_list = @connection.get( "workspace", false ) return ws_list end |
#list_xslt ⇒ Array
List the XSL stylesheets in this workspace.
156 157 158 159 160 |
# File 'lib/juxta.rb', line 156 def list_xslt ( "Listing xslt..." ) unless @logging == false xslt_list = @connection.get( "xslt" ) return xslt_list end |
#make_set(witness_ids) ⇒ String
Group the specified witnesses into a new comparison set.
365 366 367 368 369 370 |
# File 'lib/juxta.rb', line 365 def make_set( witness_ids ) ( "Creating witness set ..." ) unless @logging == false set = { 'name' => make_guid(), 'witnesses' => witness_ids } set_id = @connection.post( "set", set ) return set_id end |
#obtain_source_from_url(url) ⇒ String
Command the server to obtain an XML source file from the specified URL.
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/juxta.rb', line 328 def obtain_source_from_url( url ) id = make_guid() ( "Downloading #{url} as #{id} ..." ) unless @logging == false resp = @connection.post( "source", [{name: id, type: 'url', contentType: 'xml', data: url}] ) parsed = JSON.parse(resp) if parsed.length > 0 parsed[0] else nil end end |
#rename_witness(witness_id, new_name) ⇒ Hash
Change the name of the witness.
128 129 130 131 132 133 |
# File 'lib/juxta.rb', line 128 def rename_witness( witness_id, new_name ) ( "Renaming witness #{witness_id} to #{new_name}..." ) unless @logging == false json = { 'name' => new_name } resp = @connection.put( "witness/#{witness_id}", json ) return resp end |
#search(query) ⇒ Array
Search the current workspace for the specified phrase. Server must have search indexing enabled.
437 438 439 440 441 |
# File 'lib/juxta.rb', line 437 def search( query ) ( "Searching for #{query}..." ) unless @logging == false resp = @connection.get("search?q=#{query}") return resp end |
#select_workspace(workspace_name) ⇒ Object
Select a workspace to operate in.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/juxta.rb', line 38 def select_workspace(workspace_name) workspaces = list_workspaces() workspace_names = workspaces.map { |workspace| workspace["name"] } if workspace_names.include? workspace_name @connection.workspace = workspace_name return true else return false end end |
#tokenize_set(set_id) ⇒ Object
Tokenize the specified comparison set. Tokens are accessible as annotations of type ‘token’.
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/juxta.rb', line 387 def tokenize_set( set_id ) task_id = async_tokenize_set( set_id ) while true do sleep(2.0) resp = get_status( task_id ) case resp when 'COMPLETE' return true when 'FAILED' ( "failed to tokenize set: #{set_id}") return false end end end |
#transform_source(source_id) ⇒ String
Transform the specified source into a witness using the associated XSLT.
344 345 346 347 348 349 350 |
# File 'lib/juxta.rb', line 344 def transform_source( source_id ) ( "Transforming #{source_id} ..." ) unless @logging == false json = { 'source' => source_id, 'finalName' => make_guid() } wit_id = @connection.post( "transform", json ) return wit_id end |
#upload_source(file_name) ⇒ String
Upload a file from local disk to the server.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/juxta.rb', line 295 def upload_source( file_name ) id = make_guid() ( "Uploading #{file_name} as #{id} ..." ) unless @logging == false src_id = @connection.upload_file( id, "text/xml", open( file_name )) srcs = @connection.get("source") srcs.each do |src| if src['id'] == src_id ( "successfully uploaded file: #{file_name} (#{src['name']})" ) unless @logging == false return src_id end end ( "failed to upload file: #{file_name}") return nil end |
#workspace ⇒ String
The name of the currently selected workspace. Default is “public”.
30 31 32 |
# File 'lib/juxta.rb', line 30 def workspace() @connection.workspace end |