Sha256: 39e41ba0b6a3e660d8fc27292438cd7e4e8e3a49f80f490730b174e56c525e20

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

#!/usr/bin/env ruby
require 'uri'
require 'csv'
require 'slop'
require 'json'
require 'open-uri'

define_method :fmt_json do |main_hash,*args|
  puts main_hash.to_json
end

define_method :fmt_csv do |main_hash,separator|
  tmp_arr = [main_hash[:entity], main_hash[:phonetic_us], main_hash[:phonetic_uk],main_hash[:translation].join(separator)]
  puts tmp_arr.to_csv
end

define_method :fmt_raw do |main_hash,*args|
  puts main_hash[:entity], ''
  puts "US[#{main_hash[:phonetic_us]}]"
  puts "UK[#{main_hash[:phonetic_uk]}]"
  puts '' 
  puts main_hash[:translation]
end

begin
  opts = Slop.parse do |o|
    o.string *%w{-f --format}, 'available choices: json,csv,raw(default)', default: 'raw'
    o.string *%w{-s --separator},'translations separator used in csv format(default: NEWLINE)',default: "\n"
  end
rescue Slop::MissingArgument
  puts 'Missing arguments!'
rescue Slop::UnknownOption
  puts 'Unknown options!'
end

if opts
  entity = opts.args.first
  if entity
    tmp_hash = JSON.parse(open("http://dict.youdao.com/jsonresult?q=#{URI.encode(entity)}&type=1").read)
    main_hash = {
        entity: entity,
        phonetic_us: (tmp_hash['sm'] || 'No US Phonetic'),
        phonetic_uk: (tmp_hash['uksm'] || 'No UK Phonetic'),
        translation: (tmp_hash['basic'] || 'No Translation')
    }
    send "fmt_#{opts[:format]}", main_hash,opts[:separator]
  else
    puts 'Missing target, please specify a word or phrase!'
    exit
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arb-dict-0.1.6 bin/arb-dict