Module: Linguara

Extended by:
Utils
Defined in:
lib/linguara.rb,
lib/linguara/utils.rb,
lib/linguara/active_record.rb,
lib/linguara/configuration.rb

Defined Under Namespace

Modules: ActiveRecord, Utils Classes: Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Utils

options_to_xml, strip_blank_attributes

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



14
15
16
# File 'lib/linguara.rb', line 14

def configuration
  @configuration
end

Class Method Details

.accept_translation(body, params) ⇒ Object

Use this method in your controller to accept and save incoming translations



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/linguara.rb', line 22

def accept_translation(body, params)
  #raise ActiveRecord::StatementInvalid, 'Authorization data is missing or invalid' #TODO
  raise 'Malformed request body' if params[:translation].blank?
  target_language = params[:translation][:target_language]

  doc = Nokogiri::XML(body)

  paragraphs = doc.xpath('/translation/completed_translation/content/paragraph')
  paragraphs.each do |p|
    log("DOC: #{p.attribute('id')} --- #{p.inner_html}")
    key = p.attribute('id').to_s
    value = p.inner_html.to_s

    linguara_name, class_name,id,order,field_name = key.split('_')
    original_locale = I18n.locale
    element = class_name.constantize.find(id)
    
    I18n.locale = target_language
    element.send("#{field_name}=", value.gsub(/<p>(.*?)<\/p>/, "\\1\n") ).gsub(/<br {0,1}\/{0,1}>/, "\n")
    element.save(false)
    I18n.locale = original_locale
  end
end

.available_languagesObject



77
78
79
80
81
# File 'lib/linguara.rb', line 77

def available_languages
  Nokogiri::XML(send_languages_request.response.body).xpath("//language").map do |element|
    [element.xpath('name').inner_text, element.xpath('code').inner_text]
  end
end

.available_specializationsObject



83
84
85
86
87
# File 'lib/linguara.rb', line 83

def available_specializations
  Nokogiri::XML(send_specializations_request.response.body).xpath("//specialization").map do |element|
    [element.xpath('name').inner_text, element.xpath('id').inner_text]
  end
end

.configure {|configuration| ... } ⇒ Object

Yields:



16
17
18
19
# File 'lib/linguara.rb', line 16

def configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.handle_request_errorObject

Override this method if you want to perform some action when connection with linguara cannot be established e.g. log request or redo the send



91
92
93
# File 'lib/linguara.rb', line 91

def handle_request_error
  log("ERROR WHILE SENDING REQUEST TO LINGUARA: #{$!}")
end

.log(message) ⇒ Object

Log a linguara-specific line. Uses Rails.logger by default. Set Lingurara.config.log = false to turn off.



97
98
99
# File 'lib/linguara.rb', line 97

def log message
  logger.info("[linguara] #{message}") if logging?
end

.loggerObject

:nodoc:



101
102
103
# File 'lib/linguara.rb', line 101

def logger #:nodoc:      
  Rails.logger
end

.logging?Boolean

:nodoc:

Returns:

  • (Boolean)


105
106
107
# File 'lib/linguara.rb', line 105

def logging? #:nodoc:
  Linguara.configuration.log
end

.send_languages_request(options = {}) ⇒ Object

Sends languages request



60
61
62
63
# File 'lib/linguara.rb', line 60

def send_languages_request(options={})
  url= URI.parse("#{Linguara.configuration.server_path}api/languages.xml")
  send_linguara_request(url, :get, options_to_xml(options))
end

.send_specializations_request(options = {}) ⇒ Object

Sends specializations request



66
67
68
69
# File 'lib/linguara.rb', line 66

def send_specializations_request(options = {})
  url= URI.parse("#{Linguara.configuration.server_path}api/specializations.xml")
  send_linguara_request(url, :get, options_to_xml(options))
end

.send_status_query(translation_request_id) ⇒ Object

Sends status request



54
55
56
57
# File 'lib/linguara.rb', line 54

def send_status_query(translation_request_id)
  url= URI.parse("#{Linguara.configuration.server_path}api/translations/#{translation_request_id}/status.xml")
  send_linguara_request(url, :post, authorization_xml)
end

.send_translation_request(element, options = {}) ⇒ Object

Sends translation request



47
48
49
50
51
# File 'lib/linguara.rb', line 47

def send_translation_request(element, options = {})
  translation = Translation.new(element, options) 
  url= URI.parse("#{Linguara.configuration.server_path}api/translations.xml")
  send_linguara_request(url, :post, translation.to_xml(strip_blank_attributes(options).merge(authorization_options)))
end

.send_translators_request(options = {}) ⇒ Object

Sends translators request



72
73
74
75
# File 'lib/linguara.rb', line 72

def send_translators_request(options = {})
  url= URI.parse("#{Linguara.configuration.server_path}api/translators.xml")
  send_linguara_request(url, :get, options_to_xml(options))
end