class Transer::CLI

Public Instance Methods

dic() click to toggle source
# File lib/transer/cli.rb, line 17
def dic
    usage()
end
ej(*word) click to toggle source
# File lib/transer/cli.rb, line 27
def ej(*word)
    sentence = ""
    word.each do |w|
        sentence += "+" + w 
    end
    search(sentence, 'english', 'japanese', 'EJdict')
end
je(word) click to toggle source
# File lib/transer/cli.rb, line 22
def je(word)
    search(word, 'japanese', 'english', 'EdictJE')
end
trans(*word) click to toggle source
# File lib/transer/cli.rb, line 38
def trans(*word)
    if options[:ej]
        sentence = options[:ej]
        word.each do |w|
            sentence += "+" + w  
        end
        trans_word(sentence, 'en', 'ja')
    elsif options[:je]
        trans_word(options[:je], 'ja', 'en')
    else
        usage()
    end
end
trans_word(word, from, to) click to toggle source
# File lib/transer/cli.rb, line 90
def trans_word(word, from, to)
    https = Net::HTTP.new('www.googleapis.com',443)
    https.use_ssl = true
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE
    https.verify_depth = 5

    puts '------------------------------------------------------------------------------------------------------------------------------------------'
    printf('%25s' + '%8s' + '%-75s', from, ' ', to)
    puts ''
    puts '------------------------------------------------------------------------------------------------------------------------------------------'
    https.start {
      response = https.get('/language/translate/v2?key=AIzaSyBCFuxk01XH39Tcw2C_7hUXlbFYmZ1hYjc&q=' + word  + '&source=' + from  +  '&target=' + to)
      result = JSON.parse(response.body)
      printf('%25s' + '%8s' + '%-75s', word, ' ', result["data"]["translations"][0]["translatedText"] );
      puts ''
    }
    puts '------------------------------------------------------------------------------------------------------------------------------------------'
end
usage() click to toggle source
# File lib/transer/cli.rb, line 109
def usage
    puts ''
    puts '  usage : dic  <subcommmand> <--options> [word]'
    puts ''
    puts 'subcommands :'
    puts '  je       japanese -> english'
    puts '  ej       english -> japanese'
    puts '  trans  translation sentension by google api'
    puts ''
    puts 'options :'
    puts '  --je  your japanese sentence translation to english sentence by google api'
    puts '  --ej  your english sentence translation to japanese sentence by google api'
    puts ''
end