module Smartling::Contexts

Methods for dealing wth the Smartling Contexts API

Constants

InvalidContent

Public Class Methods

valid_content?(content) click to toggle source
# File lib/smartling/contexts.rb, line 70
def self.valid_content?(content)
  %i[read content_type original_filename].all? do |required_method|
    content.respond_to?(required_method)
  end
end

Public Instance Methods

context(project_id: @project_id, context_uid:) click to toggle source
# File lib/smartling/contexts.rb, line 16
def context(project_id: @project_id, context_uid:)
  path = "/context-api/v2/projects/#{project_id}/contexts/#{context_uid}"
  get(path)
end
context_matches(project_id: @project_id, match_id:) click to toggle source

GETs the results of an async match

# File lib/smartling/contexts.rb, line 63
def context_matches(project_id: @project_id, match_id:)
  path = "/context-api/v2/projects/#{project_id}/match/#{match_id}"
  get(path)
end
contexts(project_id: @project_id, name_filter: nil, offset: nil, type: nil) click to toggle source
# File lib/smartling/contexts.rb, line 6
def contexts(project_id: @project_id, name_filter: nil, offset: nil,
             type: nil)
  path = "/context-api/v2/projects/#{project_id}/contexts"
  query = {}
  query[:nameFilter] = name_filter unless name_filter.nil?
  query[:offset] = Integer(offset) unless offset.nil?
  query[:type] = type.to_s.upcase unless type.nil?
  get(path, query: query)
end
delete_context(project_id: @project_id, context_uid:) click to toggle source
# File lib/smartling/contexts.rb, line 48
def delete_context(project_id: @project_id, context_uid:)
  path = "/context-api/v2/projects/#{project_id}/contexts/#{context_uid}"
  delete(path)
end
download_context(project_id: @project_id, context_uid:) click to toggle source
# File lib/smartling/contexts.rb, line 42
def download_context(project_id: @project_id, context_uid:)
  path = "/context-api/v2/projects/#{project_id}/contexts"
  path += "/#{context_uid}/content"
  get(path)
end
match_context(project_id: @project_id, context_uid:, hashcodes: []) click to toggle source

POSTs to the /match/async endpoint

# File lib/smartling/contexts.rb, line 54
def match_context(project_id: @project_id, context_uid:, hashcodes: [])
  path = "/context-api/v2/projects/#{project_id}/contexts/#{context_uid}"
  path += '/match/async'
  query = { stringHashcodes: hashcodes }.to_json
  headers = { 'Content-Type' => 'application/json' }
  post(path, query: query, headers: headers)
end
upload_context(project_id: @project_id, content:, name: nil) click to toggle source

Uploads a new context. Content must quack like an UploadIO.

# File lib/smartling/contexts.rb, line 22
def upload_context(project_id: @project_id, content:, name: nil)
  path = "/context-api/v2/projects/#{project_id}/contexts"
  raise(InvalidContent, content) unless Contexts.valid_content?(content)
  body = { content: content }
  body[:name] = name unless name.nil?
  post(path, body: body)
end
upload_context_and_match(project_id: @project_id, content:, match_params: nil, name: nil) click to toggle source

Uploads a new context. Content must quack like an UploadIO.

# File lib/smartling/contexts.rb, line 31
def upload_context_and_match(project_id: @project_id, content:,
                             match_params: nil, name: nil)
  path = "/context-api/v2/projects/#{project_id}/contexts"
  path += '/upload-and-match-async'
  raise(InvalidContent, content) unless Contexts.valid_content?(content)
  body = { content: content }
  body[:name] = name unless name.nil?
  body[:matchParams] = match_params unless match_params.nil?
  post(path, body: body)
end