Sha256: d17287718ca24dd92244db055cb5a1c070ab4e038e62f3a442e494a156d423f2

Contents?: true

Size: 773 Bytes

Versions: 1

Compression:

Stored size: 773 Bytes

Contents

require 'uri'
require 'json'
require 'open-uri'
require 'arb/dict/version'

module Arb
  module Dict
    def self.query(entity)
      tmp_hash = JSON.parse(open("http://dict.youdao.com/jsonresult?q=#{URI.encode(entity)}&type=1").read)
      main_hash = { entity: entity }
      if entity.ascii_only?
        main_hash[:phonetic_us]=strict_value(tmp_hash['sm'],nil)
        main_hash[:phonetic_uk]=strict_value(tmp_hash['uksm'],nil)
      else
        main_hash[:phonetic]=strict_value(tmp_hash['sm'],nil)
      end
      main_hash[:translation]=strict_value(tmp_hash['basic'],nil)
      main_hash
    end
    private
    def self.strict_value(str,default)
      return default unless str
      return default if str.is_a? String and str.size<=0
      str
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arb-dict-0.2.4 lib/arb/dict.rb