Class: Alc::Alc

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

Constant Summary collapse

BASE_URI =
"http://eow.alc.co.jp/"
CHAR_CODE =
"/UTF-8/"

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Alc

Returns a new instance of Alc.



19
20
21
22
23
24
# File 'lib/alc.rb', line 19

def initialize(params)
  @agent = WWW::Mechanize.new
  @agent.user_agent_alias = 'Windows IE 7'
  @agent.get(make_uri_from_params(params))
  @words = params
end

Instance Method Details

#get_meaning_text_from_pageObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/alc.rb', line 45

def get_meaning_text_from_page
  meanings = Array.new
  @agent.page.search('li div').each do |div|
    lis = div.search('li')
    if lis.size != 0
      meanings << lis.inject('') { |text, li| text + "  " + li.inner_text + "\n" }
    else
      meanings << "  " + div.inner_text
    end
  end
  return meanings
end

#get_midashi_text_from_pageObject



35
36
37
38
39
40
41
42
43
# File 'lib/alc.rb', line 35

def get_midashi_text_from_page
  return @agent.page.search('li span.midashi').map do |midashi| 
    inner_text = midashi.inner_text
    @words.each do |word|
      inner_text.sub!(/(#{word})/i) { TermColor.parse("<on_blue>#{$1}</on_blue>") }
    end
    inner_text
  end
end

#make_query_from_params(params) ⇒ Object



62
63
64
# File 'lib/alc.rb', line 62

def make_query_from_params(params)
  return params.map { |param| URI.encode(param) }.join('+')
end

#make_result_text_from_pageObject



26
27
28
29
30
31
32
33
# File 'lib/alc.rb', line 26

def make_result_text_from_page
  text = ""
  meanings = get_meaning_text_from_page
  get_midashi_text_from_page.each_with_index do |midashi, index|
    text += "" + midashi + "\n" + meanings[index] + "\n"
  end
  return text
end

#make_uri_from_params(params) ⇒ Object



58
59
60
# File 'lib/alc.rb', line 58

def make_uri_from_params(params)
  return BASE_URI + make_query_from_params(params) + CHAR_CODE
end