Class: Kuport::Message
- Inherits:
-
Object
- Object
- Kuport::Message
- Defined in:
- lib/kuport/message.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
-
.parse_page(agent, page) ⇒ Object
return [Message,…].
Instance Method Summary collapse
- #get_body_text(doc) ⇒ Object
-
#get_linked_page_doc(doc) ⇒ Object
Call if message is link.
- #get_message_doc(doc) ⇒ Object
-
#initialize(agent, doc, base_url) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object
- #parse_link(elem) ⇒ Object
- #parse_links(doc_mes) ⇒ Object
- #parse_title(doc) ⇒ Object
- #to_h ⇒ Object
- #to_json(*a) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(agent, doc, base_url) ⇒ Message
Returns a new instance of Message.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/kuport/message.rb', line 11 def initialize(agent, doc, base_url) @agent = agent @base_url = base_url @title = parse_title(doc) doc_mes = (doc) if doc_mes @body = get_body_text(doc_mes) else # When message is link doc_mes = get_linked_page_doc(doc) @body = get_body_text((doc_mes)) end @links = parse_links(doc_mes) end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
3 4 5 |
# File 'lib/kuport/message.rb', line 3 def agent @agent end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
3 4 5 |
# File 'lib/kuport/message.rb', line 3 def base_url @base_url end |
#body ⇒ Object (readonly)
Returns the value of attribute body.
3 4 5 |
# File 'lib/kuport/message.rb', line 3 def body @body end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
3 4 5 |
# File 'lib/kuport/message.rb', line 3 def links @links end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
3 4 5 |
# File 'lib/kuport/message.rb', line 3 def title @title end |
Class Method Details
.parse_page(agent, page) ⇒ Object
return [Message,…]
6 7 8 9 |
# File 'lib/kuport/message.rb', line 6 def self.parse_page(agent, page) doc = Kuport.get_page_doc(page) doc.css('.message > li').map{|mes| self.new(agent, mes, page.uri)} end |
Instance Method Details
#get_body_text(doc) ⇒ Object
37 38 39 |
# File 'lib/kuport/message.rb', line 37 def get_body_text(doc) doc.text.strip.gsub(/\r\n/, "\n").freeze rescue "Error in #{title}" end |
#get_linked_page_doc(doc) ⇒ Object
Call if message is link
42 43 44 45 46 |
# File 'lib/kuport/message.rb', line 42 def get_linked_page_doc(doc) link = parse_link(doc.at_css('a'))[:path] doc_new = Kuport.get_page_doc(agent.get(link)) doc_new.at_css('.portlet_module') end |
#get_message_doc(doc) ⇒ Object
33 34 35 |
# File 'lib/kuport/message.rb', line 33 def (doc) doc.at_xpath(".//*[contains(@class, 'message')]") end |
#inspect ⇒ Object
69 70 71 |
# File 'lib/kuport/message.rb', line 69 def inspect "title: #{title}\nbody: #{body}\nlinks: #{links}".freeze end |
#parse_link(elem) ⇒ Object
52 53 54 55 |
# File 'lib/kuport/message.rb', line 52 def parse_link(elem) {name: Kuport.escape_filename(elem.text), path: Kuport.to_abs_url(base_url, elem[:href])}.freeze end |
#parse_links(doc_mes) ⇒ Object
48 49 50 |
# File 'lib/kuport/message.rb', line 48 def parse_links(doc_mes) doc_mes.css('a').map{|elem| parse_link(elem)}.freeze rescue [{name: "Error in #{title}", path: ''}] end |
#parse_title(doc) ⇒ Object
29 30 31 |
# File 'lib/kuport/message.rb', line 29 def parse_title(doc) doc.children.select(&:text?).join.strip + doc.at_css('a').text.freeze end |
#to_h ⇒ Object
57 58 59 |
# File 'lib/kuport/message.rb', line 57 def to_h @data_hash ||= {title: title, body: body, links: links}.freeze end |
#to_json(*a) ⇒ Object
65 66 67 |
# File 'lib/kuport/message.rb', line 65 def to_json(*a) @data_json ||= to_h.to_json(*a) end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/kuport/message.rb', line 61 def to_s @data_str ||= ("#{title}\n\n#{body}\n\n" + links.map{|l| l[:name]}.join("\n")).freeze end |