Class: TextConverter::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Service

Returns a new instance of Service.



37
38
39
40
41
42
# File 'lib/text_converter.rb', line 37

def initialize(name, options)
  @name = name
  @url = options['url']
  @charset = options['charset']
  @xpath = options['xpath']
end

Instance Attribute Details

#charsetObject (readonly)

Returns the value of attribute charset.



35
36
37
# File 'lib/text_converter.rb', line 35

def charset
  @charset
end

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/text_converter.rb', line 35

def name
  @name
end

#urlObject (readonly)

Returns the value of attribute url.



35
36
37
# File 'lib/text_converter.rb', line 35

def url
  @url
end

#xpathObject (readonly)

Returns the value of attribute xpath.



35
36
37
# File 'lib/text_converter.rb', line 35

def xpath
  @xpath
end

Instance Method Details

#build_url(text) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/text_converter.rb', line 48

def build_url(text)
  case charset
  when 'sjis'
    text = NKF.nkf('-Ws', text)
  when 'euc'
    text = NKF.nkf('-We', text)
  end

  url.gsub(/%s/, CGI.escape(text))
end

#convert(text) ⇒ Object



44
45
46
# File 'lib/text_converter.rb', line 44

def convert(text)
  parse(get(text))
end

#get(text) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/text_converter.rb', line 59

def get(text)
  result = open(build_url(text)).read

  case charset
  when 'sjis'
    result = NKF.nkf('-Sw', result)
  when 'euc'
    result = NKF.nkf('-Ew', result)
  end

  result
end

#parse(result) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/text_converter.rb', line 72

def parse(result)
  if xpath
    doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
    html = Nokogiri::HTML.parse(doctype + result, nil, 'utf-8')
    result = html.xpath(xpath).text
  end

  result
end