class Semantria::Session

Attributes

api_version[RW]
host[RW]

Public Class Methods

new(consumer_key = nil, consumer_secret = nil, application_name = nil, use_compression = false, serializer = nil, username = nil, password = nil, session_file = '/tmp/session.dat') click to toggle source

Create a new instance

# File lib/semantria/session.rb, line 14
def initialize(consumer_key = nil, consumer_secret = nil, application_name = nil, use_compression = false, serializer = nil, username = nil, password = nil, session_file = '/tmp/session.dat')
  @host = 'https://api.semantria.com'
  @key_url = 'https://semantria.com/auth/session'
  @app_key = 'cd954253-acaf-4dfa-a417-0a8cfb701f12'
  @wrapper_name = "Ruby/#{Semantria::VERSION}"
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
  @use_compression = use_compression
  @api_version = '4.2'

  if serializer.nil?
    #set default json serializer
    @serializer = JsonSerializer.new()
    @format = @serializer.gettype()
  else
    @serializer = serializer
    @format = @serializer.gettype()
  end

  if consumer_key.nil? && consumer_secret.nil?
    @consumer_key, @consumer_secret = obtainKeys(username, password, session_file)
    if @consumer_key.nil? || @consumer_secret.nil?
      fail "Cannot obtain Semantria keys. Wrong username or password"
    end
  end

  if application_name.nil?
    @application_name = @wrapper_name
  else
    @application_name = '%s/%s' % [application_name, @wrapper_name]
  end

end

Public Instance Methods

addBlacklist(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 174
def addBlacklist(items, config_id = nil)
  updateBlacklist(items, config_id, true)
end
addCategories(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 212
def addCategories(items, config_id = nil)
  updateCategories(items, config_id, true)
end
addConfigurations(items) click to toggle source
# File lib/semantria/session.rb, line 137
def addConfigurations(items)
  updateConfigurations(items, true)
end
addEntities(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 326
def addEntities(items, config_id = nil)
  updateEntities(items, config_id, true)
end
addPhrases(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 288
def addPhrases(items, config_id = nil)
  updatePhrases(items, config_id, true)
end
addQueries(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 250
def addQueries(items, config_id = nil)
  updateQueries(items, config_id, true)
end
addTaxonomy(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 364
def addTaxonomy(items, config_id = nil)
  updateTaxonomy(items, config_id, true)
end
cancelCollection(id, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 506
def cancelCollection(id, config_id = nil)
  fail 'Collection ID is nil or empty' if id.nil?

  id = URI.encode id

  if config_id.nil?
    url = "#{@host}/collection/#{id}.#{@format}"
  else
    url = "#{@host}/collection/#{id}.#{@format}?config_id=#{config_id}"
  end

  runRequest('DELETE', url)
end
cancelDocument(doc_id, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 442
def cancelDocument(doc_id, config_id = nil)
  fail 'Document ID is nil or empty' if doc_id.nil?

  doc_id = URI.encode doc_id

  if config_id.nil?
    url = "#{@host}/document/#{doc_id}.#{@format}"
  else
    url = "#{@host}/document/#{doc_id}.#{@format}?config_id=#{config_id}"
  end

  runRequest('DELETE', url)
end
cloneConfigurations(name, template) click to toggle source
# File lib/semantria/session.rb, line 148
def cloneConfigurations(name, template)
  items = {
    'name' => name,
    'template' => template
  }
  updateConfigurations([items])
end
deleteConfigurations(items) click to toggle source
# File lib/semantria/session.rb, line 156
def deleteConfigurations(items)
  url = "#{@host}/configurations.#{@format}"

  wrapper = get_type_wrapper('delete_configurations')
  data = @serializer.serialize(items, wrapper)
  runRequest('DELETE', url, nil, data)
end
getBlacklist(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 164
def getBlacklist(config_id = nil)
  if config_id.nil?
    url = "#{@host}/blacklist.#{@format}"
  else
    url = "#{@host}/blacklist.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_blacklist') || []
end
getCategories(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 202
def getCategories(config_id = nil)
  if config_id.nil?
    url = "#{@host}/categories.#{@format}"
  else
    url = "#{@host}/categories.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_categories') || []
end
getCollection(id, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 492
def getCollection(id, config_id = nil)
  fail 'Collection ID is nil or empty' if id.nil?

  id = URI.encode id

  if config_id.nil?
    url = "#{@host}/collection/#{id}.#{@format}"
  else
    url = "#{@host}/collection/#{id}.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_collection')
end
getConfigurations() click to toggle source
# File lib/semantria/session.rb, line 132
def getConfigurations
  url = "#{@host}/configurations.#{@format}"
  runRequest('GET', url, 'get_configurations') || []
end
getDocument(doc_id, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 428
def getDocument(doc_id, config_id = nil)
  fail 'Document ID is nil or empty' if doc_id.nil?

  doc_id = URI.encode doc_id

  if config_id.nil?
    url = "#{@host}/document/#{doc_id}.#{@format}"
  else
    url = "#{@host}/document/#{doc_id}.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_document')
end
getEntities(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 316
def getEntities(config_id = nil)
  if config_id.nil?
    url = "#{@host}/entities.#{@format}"
  else
    url = "#{@host}/entities.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_entities') || []
end
getPhrases(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 278
def getPhrases(config_id = nil)
  if config_id.nil?
    url = "#{@host}/phrases.#{@format}"
  else
    url = "#{@host}/phrases.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_sentiment_phrases') || []
end
getProcessedCollections(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 520
def getProcessedCollections(config_id = nil)
  if config_id.nil?
    url = "#{@host}/collection/processed.#{@format}"
  else
    url = "#{@host}/collection/processed.#{@format}?config_id=#{config_id}"
  end

  result = runRequest('GET', url, 'get_processed_collections')
  result ||= []
end
getProcessedCollectionsByJobId(job_id) click to toggle source
# File lib/semantria/session.rb, line 531
def getProcessedCollectionsByJobId(job_id)
  url = "#{@host}/collection/processed.#{@format}?job_id=#{job_id}"

  result = runRequest('GET', url, 'get_processed_collections_by_job_id')
  result ||= []
end
getProcessedDocuments(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 456
def getProcessedDocuments(config_id = nil)
  if config_id.nil?
    url = "#{@host}/document/processed.#{@format}"
  else
    url = "#{@host}/document/processed.#{@format}?config_id=#{config_id}"
  end

  result = runRequest('GET', url, 'get_processed_documents')
  result ||= []
end
getProcessedDocumentsByJobId(job_id) click to toggle source
# File lib/semantria/session.rb, line 467
def getProcessedDocumentsByJobId(job_id)
  url = "#{@host}/document/processed.#{@format}?job_id=#{job_id}"

  result = runRequest('GET', url, 'get_processed_documents_by_job_id')
  result ||= []
end
getQueries(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 240
def getQueries(config_id = nil)
  if config_id.nil?
    url = "#{@host}/queries.#{@format}"
  else
    url = "#{@host}/queries.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_queries') || []
end
getStatistics() click to toggle source
# File lib/semantria/session.rb, line 127
def getStatistics
  url = "#{@host}/statistics.#{@format}"
  runRequest('GET', url, 'get_statistics')
end
getStatus() click to toggle source
# File lib/semantria/session.rb, line 107
def getStatus
  url = "#{@host}/status.#{@format}"
  runRequest('GET', url, 'get_status')
end
getSubscription() click to toggle source
# File lib/semantria/session.rb, line 122
def getSubscription
  url = "#{@host}/subscription.#{@format}"
  runRequest('GET', url, 'get_subscription')
end
getSupportedFeatures(language) click to toggle source
# File lib/semantria/session.rb, line 112
def getSupportedFeatures(language)
  if language.nil?
    url = "#{@host}/features.#{@format}"
  else
    url = "#{@host}/features.#{@format}?language=#{language}"
  end
      
  runRequest('GET', url, 'get_features')
end
getTaxonomy(config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 354
def getTaxonomy(config_id = nil)
  if config_id.nil?
    url = "#{@host}/taxonomy.#{@format}"
  else
    url = "#{@host}/taxonomy.#{@format}?config_id=#{config_id}"
  end

  runRequest('GET', url, 'get_taxonomy') || []
end
obtainKeys(username, password, session_file = '/tmp/sematria-session.dat') click to toggle source
# File lib/semantria/session.rb, line 48
def obtainKeys(username, password, session_file = '/tmp/sematria-session.dat')
  if File.exist?(session_file)
    session_id = File.read(session_file)
    url = "#{@key_url}/#{session_id}.json?appkey=#{@app_key}"
    uri = URI.parse url
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Get.new(uri.request_uri)
    response = http.request(request)
    if response.code.to_i == 200
      body = @serializer.deserialize(response.body)
      if body[:custom_params][:key].present && body[:custom_params][:secret].present
        return [body[:custom_params][:key], body[:custom_params][:secret]]
      end
    end
  end

  url = "#{@key_url}.json?appkey=#{@app_key}";
  post = {
      username: username,
      password: password
  }
  uri = URI.parse url
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = post.to_json
  response = http.request(request)

  if response.code.to_i != 200
    return [nil, nil]
  end

  body = @serializer.deserialize(response.body)
  session_id = body[:id].to_s
  if !session_file.to_s.empty? && !session_id.empty?
    File.open(session_file, 'w') { |file| file.write(session_id) }
  end

  [body[:custom_params][:key], body[:custom_params][:secret]]
end
queueBatch(batch, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 410
def queueBatch(batch, config_id = nil)
  if config_id.nil?
    url = "#{@host}/document/batch.#{@format}"
  else
    url = "#{@host}/document/batch.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('queue_batch_documents')
  data = @serializer.serialize(batch, wrapper)
  result = runRequest('POST', url, 'get_processed_documents', data)
  if result != nil && result.is_a?(Array)
    onDocsAutoResponse(result)
    200
  else
    result
  end
end
queueCollection(task, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 474
def queueCollection(task, config_id = nil)
  if config_id.nil?
    url = "#{@host}/collection.#{@format}"
  else
    url = "#{@host}/collection.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('queue_collection')
  data = @serializer.serialize(task, wrapper)
  result = runRequest('POST', url, 'get_processed_collections', data)
  if result != nil && result.is_a?(Array)
    onCollsAutoResponse(result)
    200
  else
    result
  end
end
queueDocument(task, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 392
def queueDocument(task, config_id = nil)
  if config_id.nil?
    url = "#{@host}/document.#{@format}"
  else
    url = "#{@host}/document.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('queue_document')
  data = @serializer.serialize(task, wrapper)
  result = runRequest('POST', url, 'get_processed_documents', data)
  if result != nil && result.is_a?(Array)
    onDocsAutoResponse(result)
    200
  else
    result
  end
end
registerSerializer(serializer) click to toggle source
# File lib/semantria/session.rb, line 92
def registerSerializer(serializer)
  fail 'Serializer can\'t be null' if serializer.nil?

  @serializer = serializer
  @format = serializer.gettype()
end
removeBlacklist(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 190
def removeBlacklist(items, config_id = nil)
  if config_id.nil?
    url = "#{@host}/blacklist.#{@format}"
  else
    url = "#{@host}/blacklist.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('remove_blacklist')
  data = @serializer.serialize(items, wrapper)
  runRequest('DELETE', url, nil, data)
end
removeCategories(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 228
def removeCategories(items, config_id = nil)
  if config_id.nil?
    url = "#{@host}/categories.#{@format}"
  else
    url = "#{@host}/categories.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('remove_categories')
  data = @serializer.serialize(items, wrapper)
  runRequest('DELETE', url, nil, data)
end
removeEntities(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 342
def removeEntities(items, config_id = nil)
  if config_id.nil?
    url = "#{@host}/entities.#{@format}"
  else
    url = "#{@host}/entities.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('remove_entities')
  data = @serializer.serialize(items, wrapper)
  runRequest('DELETE', url, nil, data)
end
removePhrases(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 304
def removePhrases(items, config_id = nil)
  if config_id.nil?
    url = "#{@host}/phrases.#{@format}"
  else
    url = "#{@host}/phrases.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('remove_phrases')
  data = @serializer.serialize(items, wrapper)
  runRequest('DELETE', url, nil, data)
end
removeQueries(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 266
def removeQueries(items, config_id = nil)
  if config_id.nil?
    url = "#{@host}/queries.#{@format}"
  else
    url = "#{@host}/queries.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('remove_queries')
  data = @serializer.serialize(items, wrapper)
  runRequest('DELETE', url, nil, data)
end
removeTaxonomy(items, config_id = nil) click to toggle source
# File lib/semantria/session.rb, line 380
def removeTaxonomy(items, config_id = nil)
  if config_id.nil?
    url = "#{@host}/taxonomy.#{@format}"
  else
    url = "#{@host}/taxonomy.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('remove_taxonomy')
  data = @serializer.serialize(items, wrapper)
  runRequest('DELETE', url, nil, data)
end
setCallbackHandler(callback) click to toggle source
# File lib/semantria/session.rb, line 99
def setCallbackHandler(callback)
  if callback.class < CallbackHandler
    @callback = callback
  else
    fail "Parameter is not subclass of CallbackHandler #{callback}"
  end
end
updateBlacklist(items, config_id = nil, create = false) click to toggle source
# File lib/semantria/session.rb, line 178
def updateBlacklist(items, config_id = nil, create = false)
  if config_id.nil?
    url = "#{@host}/blacklist.#{@format}"
  else
    url = "#{@host}/blacklist.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('update_blacklist')
  data = @serializer.serialize(items, wrapper)
  runRequest((create ? 'POST' : 'PUT'), url, nil, data)
end
updateCategories(items, config_id = nil, create = false) click to toggle source
# File lib/semantria/session.rb, line 216
def updateCategories(items, config_id = nil, create = false)
  if config_id.nil?
    url = "#{@host}/categories.#{@format}"
  else
    url = "#{@host}/categories.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('update_categories')
  data = @serializer.serialize(items, wrapper)
  runRequest((create ? 'POST' : 'PUT'), url, nil, data)
end
updateConfigurations(items, create = false) click to toggle source
# File lib/semantria/session.rb, line 141
def updateConfigurations(items, create = false)
  url = "#{@host}/configurations.#{@format}"
  wrapper = get_type_wrapper('update_configurations')
  data = @serializer.serialize(items, wrapper)
  runRequest((create ? 'POST' : 'PUT'), url, nil, data)
end
updateEntities(items, config_id = nil, create = false) click to toggle source
# File lib/semantria/session.rb, line 330
def updateEntities(items, config_id = nil, create = false)
  if config_id.nil?
    url = "#{@host}/entities.#{@format}"
  else
    url = "#{@host}/entities.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('update_entities')
  data = @serializer.serialize(items, wrapper)
  runRequest((create ? 'POST' : 'PUT'), url, nil, data)
end
updatePhrases(items, config_id = nil, create = false) click to toggle source
# File lib/semantria/session.rb, line 292
def updatePhrases(items, config_id = nil, create = false)
  if config_id.nil?
    url = "#{@host}/phrases.#{@format}"
  else
    url = "#{@host}/phrases.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('update_sentiment_phrases')
  data = @serializer.serialize(items, wrapper)
  runRequest((create ? 'POST' : 'PUT'), url, nil, data)
end
updateQueries(items, config_id = nil, create = false) click to toggle source
# File lib/semantria/session.rb, line 254
def updateQueries(items, config_id = nil, create = false)
  if config_id.nil?
    url = "#{@host}/queries.#{@format}"
  else
    url = "#{@host}/queries.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('update_queries')
  data = @serializer.serialize(items, wrapper)
  runRequest((create ? 'POST' : 'PUT'), url, nil, data)
end
updateTaxonomy(items, config_id = nil, create = false) click to toggle source
# File lib/semantria/session.rb, line 368
def updateTaxonomy(items, config_id = nil, create = false)
  if config_id.nil?
    url = "#{@host}/taxonomy.#{@format}"
  else
    url = "#{@host}/taxonomy.#{@format}?config_id=#{config_id}"
  end

  wrapper = get_type_wrapper('update_taxonomy')
  data = @serializer.serialize(items, wrapper)
  runRequest((create ? 'POST' : 'PUT'), url, nil, data)
end

Private Instance Methods

get_type_handler(type) click to toggle source
# File lib/semantria/session.rb, line 578
def get_type_handler(type)
  if @serializer.gettype() == 'json'
    return nil
  end

  #only for xml serializer
  case
    when type == 'get_status' then return GetStatusHandler.new()
    when type == 'get_subscription' then return GetSubscriptionHandler.new()
    when type == 'get_configurations' then return GetConfigurationsHandler.new()
    when type == 'get_blacklist' then return GetBlacklistHandler.new()
    when type == 'get_categories' then return GetCategoriesHandler.new()
    when type == 'get_queries' then return GetQueriesHandler.new()
    when type == 'get_sentiment_phrases' then return GetSentimentPhrasesHandler.new()
    when type == 'get_entities' then return GetEntitiesHandler.new()
    when type == 'get_document' then return GetDocumentHandler.new()
    when type == 'get_processed_documents' then return GetProcessedDocumentsHandler.new()
    when type == 'get_collection' then return GetCollectionHandler.new()
    when type == 'get_processed_collections' then return GetProcessedCollectionsHandler.new()
    else return nil
  end
end
get_type_wrapper(type) click to toggle source
# File lib/semantria/session.rb, line 601
def get_type_wrapper(type)
  if @serializer.gettype() == 'json'
    nil
  end

  #only for xml serializer
  #if type == "update_configurations"
  #  return {"root" => "configurations", "added" => "configuration", "removed" => "configuration"}
  #elsif type == "update_blacklist"
  #  return {"root" => "blacklist", "added" => "item", "removed" => "item"}
  #elsif type == "update_categories"
  #  return {"root" => "categories", "added" => "category", "removed" => "category", "samples" => "sample"}
  #elsif type == "update_queries"
  #  return {"root" => "queries", "added" => "query", "removed" => "query"}
  #elsif type == "update_sentiment_phrases"
  #  return {"root" => "phrases", "added" => "phrase", "removed" => "phrase"}
  #elsif type == "update_entities"
  #  return {"root" => "entities", "added" => "entity", "removed" => "entity"}
  #elsif type == "queue_document"
  #  return {"root" => "document"}
  #elsif type == "queue_batch_documents"
  #  return {"root" => "documents", "item" => "document"}
  #elsif type == "queue_collection"
  #  return {"root" => "collection", "documents" => "document"}
  #else
  #  return nil
  #end
end
onCollsAutoResponse(response) click to toggle source
# File lib/semantria/session.rb, line 662
def onCollsAutoResponse(response)
  unless @callback.nil?
    @callback.onCollsAutoResponse(self, response)
  end
end
onDocsAutoResponse(response) click to toggle source
# File lib/semantria/session.rb, line 656
def onDocsAutoResponse(response)
  unless @callback.nil?
    @callback.onDocsAutoResponse(self, response)
  end
end
onError(response) click to toggle source
# File lib/semantria/session.rb, line 650
def onError(response)
  unless @callback.nil?
    @callback.onError(self, response)
  end
end
onRequest(request) click to toggle source
# File lib/semantria/session.rb, line 638
def onRequest(request)
  unless @callback.nil?
    @callback.onRequest(self, request)
  end
end
onResponse(response) click to toggle source
# File lib/semantria/session.rb, line 644
def onResponse(response)
  unless @callback.nil?
    @callback.onResponse(self, response)
  end
end
resolve_error(status, message = nil) click to toggle source
# File lib/semantria/session.rb, line 630
def resolve_error(status, message = nil)
  if status == 400 || status == 401 || status == 402 || status == 403 || status == 406 || status == 500
    onError({'status' => status, 'message' => message})
  else
    fail "HTTP error was found with status: #{status} and message: #{message}"
  end
end
runRequest(method, url, type = nil, post_data = nil) click to toggle source
# File lib/semantria/session.rb, line 539
def runRequest(method, url, type = nil, post_data = nil)
  request = AuthRequest.new(@consumer_key, @consumer_secret, @application_name, @use_compression)
  request.api_version=(@api_version)
  
  onRequest({'method' => method, 'url' => url, 'message' => post_data})
  
  response = request.authWebRequest(method, url, post_data)
  
  onResponse({'status' => response[:status], 'reason' => response[:reason], 'message' => response[:data]})

  status = response[:status]
  message = response[:reason]

  message = response[:data] unless response[:data].to_s.empty?

  if method == 'DELETE'
    if status == 200 || status == 202
      return status
    else
      resolve_error(status, message)
      return status
    end
  else
    if status == 200
      handler = get_type_handler(type)
      message = @serializer.deserialize(response[:data], handler)
      return message
    elsif status == 202
      if method == 'POST'
        return status
      else
        return nil
      end
    else
      resolve_error(status, message)
    end
  end
end