Class: AsianCharacter::Word

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Word

Returns a new instance of Word.



5
6
7
8
# File 'lib/asian_character/word.rb', line 5

def initialize(word)
  @word = word
  @cache = false
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



3
4
5
# File 'lib/asian_character/word.rb', line 3

def cache
  @cache
end

Instance Method Details

#pinyinObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/asian_character/word.rb', line 10

def pinyin
  if @pinyin
    @cache= true
    return @pinyin 
  end
  @cache = false
  url = 'http://chinese.biodinfo.org/'\
        'ChineseCharactorWebService.asmx/'\
        'GetWordPinyin?word=' + 
         URI.escape(@word) + '&psd=1'
  res = RestClient.get(url)
  res.gsub!(/\s/, ' ')
  res=res.match(%r|<string.*>([a-z0-9]+)</string>|)
  @pinyin = res ? res[1].strip : nil 
end

#sound_urlObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asian_character/word.rb', line 26

def sound_url
  if @sound_url
    @cache = true
    return @sound_url 
  end
  @cache = false
  url = 'http://chinese.biodinfo.org/'\
         'ChineseCharactorWebService.asmx/'\
         'GetWordPinyinSoundMP3?word=' + 
          URI.escape(@word) + '&psd=1'
  res = RestClient.get(url)
  res.gsub!(/\s/, ' ')
  res=res.match(%r|<string.*>\s*(.+)\s*</string>|)
  @sound_url = res ? res[1].strip : nil 
end