class Oversetter::Glosbe::Translate

Fetches translations from Glosbe.

Public Instance Methods

authors(authors) click to toggle source

Prints a list of translation authors.

@param authors [Hash] List of author/URL pairs.

# File lib/oversetter/glosbe/translate.rb, line 74
def authors(authors)
        authors.map { |k,v|
                source = v
                puts Rainbow("Author|#{source['N']}|URL|#{source['url']}").bright
        }
end
entities(result) click to toggle source

Replaces HTML entities w/ UTF-8 characters.

@param result [String] JSON string of search results.

# File lib/oversetter/glosbe/translate.rb, line 65
def entities(result)
        Encoding.default_external = "UTF-8"
        coder = HTMLEntities.new
        result = coder.decode(result.force_encoding('UTF-8'))
        return result
end
get_trans(search, params) click to toggle source

Main operations.

@param search [String] The word or phrase to search for. @param params [Hash] The search parameters to use.

# File lib/oversetter/glosbe/translate.rb, line 12
def get_trans(search, params)
        func, result = 'translate', nil
        tran = Oversetter::Glosbe.new
        result = tran.get_word(search, func, params, result)
        if result =~ /"/i
                q = Oversetter::Glosbe::Translate.new
                q.quote(result)
        end
        if result =~ /&[a-z]*;/i
                ent = Oversetter::Glosbe::Translate.new
                ent.entities(result)
        end
        result = MultiJson.load(result)
        if result['result'] == 'ok'
                st = { 'searchterm' => URI.decode(search) }
                type = { 'type' => 'translation' }
                Oversetter.tofile(st)
                Oversetter.tofile(type)
                authors = result['authors']
                if params[:auth] == true
                        a = Oversetter::Glosbe::Translate.new
                        a.authors(authors)
                end
                label = 'Translation'
                Oversetter.label(label)
                tuc = result['tuc'] #array of hashes
                x, y = 0, tuc.length - 1
                while x <= y
                        item = tuc[x]
                        mean = item['meanings'] #array of hashes
                        if mean != nil
                                m = Oversetter::Glosbe::Translate.new
                                m.mean(mean)
                        end
                        phrase = item['phrase'] #hash
                        if phrase != nil
                                p = Oversetter::Glosbe::Translate.new
                                p.phrase(phrase)
                        end
                        x += 1
                end
        end
end
mean(mean) click to toggle source

Meaning handler.

@param mean [Array] Array of hashes.

# File lib/oversetter/glosbe/translate.rb, line 83
def mean(mean)
        a, b = 0, mean.length - 1
        while a <= b
                meaning = mean[a] #hash
                mtext = meaning['text']
                mlang = meaning['language']
                print Rainbow("Meaning|").bright
                puts "#{mtext}|#{mlang}|"
                mt = { 'meaning' => mtext }
                ml = { 'language' => mlang }
                Oversetter.tofile(mt)
                Oversetter.tofile(ml)
                a += 1
        end
end
phrase(phrase) click to toggle source

Phrase handler.

@param phrase [Hash] A list of phrase/language pairs.

# File lib/oversetter/glosbe/translate.rb, line 101
def phrase(phrase)
        ptext = phrase['text']
        plang = phrase['language']
        print Rainbow("Phrase|").bright
        print "#{ptext}|#{plang}|"
        pt = { 'phrase' => ptext }
        pl = { 'language' => plang }
        Oversetter.tofile(pt)
        Oversetter.tofile(pl)
end
quote(result) click to toggle source

Replaces &quot; HTML entity w/ character escapes for JSON.

@param result [String] JSON string of search results.

# File lib/oversetter/glosbe/translate.rb, line 58
def quote(result)
        result = result.gsub('&quot;', "\\\"")
        return result
end