Class: TextConverter::Service
- Inherits:
-
Object
- Object
- TextConverter::Service
- Defined in:
- lib/text_converter.rb
Instance Attribute Summary collapse
-
#charset ⇒ Object
readonly
Returns the value of attribute charset.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#xpath ⇒ Object
readonly
Returns the value of attribute xpath.
Instance Method Summary collapse
- #build_url(text) ⇒ Object
- #convert(text) ⇒ Object
- #get(text) ⇒ Object
-
#initialize(name, options) ⇒ Service
constructor
A new instance of Service.
- #parse(result) ⇒ Object
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, ) @name = name @url = ['url'] @charset = ['charset'] @xpath = ['xpath'] end |
Instance Attribute Details
#charset ⇒ Object (readonly)
Returns the value of attribute charset.
35 36 37 |
# File 'lib/text_converter.rb', line 35 def charset @charset end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
35 36 37 |
# File 'lib/text_converter.rb', line 35 def name @name end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
35 36 37 |
# File 'lib/text_converter.rb', line 35 def url @url end |
#xpath ⇒ Object (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 |