Class: Translate::Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/translate.rb

Constant Summary collapse

OPTIONS =
{ :more => false, :from => :en, :to => :fr, :width => WIDTH }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, options = {}) ⇒ Translation

Ensure width will be an integer, and provides an empty array of items.



69
70
71
72
73
74
75
76
77
# File 'lib/translate.rb', line 69

def initialize(expression, options = {})
	self.expression = expression
	options = OPTIONS.merge(options)
	for k in options.keys
		self.send("#{k}=", options[k]) if self.respond_to?(k)
	end
	@items = []
	@width = @width.to_i
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



64
65
66
# File 'lib/translate.rb', line 64

def errors
  @errors
end

#expressionObject

Returns the value of attribute expression.



64
65
66
# File 'lib/translate.rb', line 64

def expression
  @expression
end

#fromObject

Returns the value of attribute from.



64
65
66
# File 'lib/translate.rb', line 64

def from
  @from
end

#itemsObject

Returns the value of attribute items.



64
65
66
# File 'lib/translate.rb', line 64

def items
  @items
end

#moreObject

Returns the value of attribute more.



64
65
66
# File 'lib/translate.rb', line 64

def more
  @more
end

#toObject

Returns the value of attribute to.



64
65
66
# File 'lib/translate.rb', line 64

def to
  @to
end

#widthObject

Returns the value of attribute width.



64
65
66
# File 'lib/translate.rb', line 64

def width
  @width
end

Instance Method Details

#combinationObject

Provide the from, to combination. Example: from en to fr will give enfr. Usefull to build the final url.



81
82
83
# File 'lib/translate.rb', line 81

def combination
	@combination ||= "#{from}#{to}"
end

Only used when translate is called from command line. Print results in the shell.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/translate.rb', line 120

def print
	lambda { puts "No results found." ; exit }.call if @items.empty?
	half = (width - 4) / 2 # 4 : 2 slash and space
	puts @items.inject("") { |o, item|
		str  = item == @items.first ? "#{separator}\n#{@expression.center(width, ' ')}\n" : ""
		str  += "#{separator}\n" if item.new_line
		str  += " %-#{half}s|"		% item.description.truncate(half)
		str  += " %-#{half}s\n"  % item.translation.truncate(half)
		str  += "#{separator}\n" if item == @items.last
		o + str
	}
end

Only used when translate is called from command line. Print errors in the shell.



135
136
137
138
139
# File 'lib/translate.rb', line 135

def print_errors
	puts @errors.inject("Some errors encountered :") { |str, e|
		str += "\n\t- #{e}"
	}
end

#retrieveObject

We need to provide an User-Agent otherwise we’re redirected to yahoo.



164
165
166
167
168
# File 'lib/translate.rb', line 164

def retrieve
	request = Net::HTTP::Get.new(url.path)
	request.add_field('User-Agent', 'translate')
	Net::HTTP.new(url.host, url.port).request(request).body
end

#separatorObject

Only used when translate is called from command line. Define the separator between two lines.



87
88
89
# File 'lib/translate.rb', line 87

def separator
	@separator ||= '-' * width
end

#translateObject

the final results array.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/translate.rb', line 93

def translate
	return nil unless valid?
	continue = false
	@items.clear
	klass = Language.respond_to?("klass_#{combination}") ? Language.send("klass_#{combination}") : "2"
	tree.search("table.Rtbl#{klass}/tr").each do |child|
		head = child.search("td.Head:first").first
		continue = !! (
			head.html =~ /Principal/ || 
			head.attributes['title'] =~ /Principal/ ||
			(head.html =~ /Additional/ and more) ||
			(head.attributes['title'] =~ /Additional/)) if head.respond_to?(:html)
		unless ! continue or child.classes =~ /(evenEx|oddEx)/
			# strip html tags
			description = child.search("td.FrCN#{klass}/*").to_s.sanitize
			translation = child.search("td.ToW#{klass}/*").to_s.sanitize
			@items << Item.new({ 
				:description 	=> description, 
				:translation 	=> translation,
				:new_line			=> ! child.search("td.FrW#{klass}").empty?
			}) unless description.empty? and translation.empty?
		end
	end
end

#treeObject

Provide the full document retrieved from the url.



142
143
144
# File 'lib/translate.rb', line 142

def tree
	@tree ||= Hpricot.parse(retrieve)
end

#urlObject

Wordreference full url.



147
148
149
# File 'lib/translate.rb', line 147

def url
	@url ||= URI::parse(URI.escape("#{URL}#{combination}/#{expression}"))
end

#valid?Boolean

Check if the translation is valid, ie. options exists and an expression has been providen. Build an array of errors which will be printed to the end user.

Returns:

  • (Boolean)


154
155
156
157
158
159
160
# File 'lib/translate.rb', line 154

def valid?
			@errors = []
			@errors << "Expression can't be blank." if expression.blank?
			@errors << "Translation from #{from} to #{to} is not available, use translate -l to show available languages." unless Translate::Language.available_translation?(from, to)
			@errors << "Width must be an integer >= #{MIN_WIDTH}." unless width.is_a?(Integer) and width >= MIN_WIDTH
			@errors.empty?
end