Class: Kuport::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = get_message_doc(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(get_message_doc(doc_mes))
  end

  @links = parse_links(doc_mes)
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



3
4
5
# File 'lib/kuport/message.rb', line 3

def agent
  @agent
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



3
4
5
# File 'lib/kuport/message.rb', line 3

def base_url
  @base_url
end

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/kuport/message.rb', line 3

def body
  @body
end

Returns the value of attribute links.



3
4
5
# File 'lib/kuport/message.rb', line 3

def links
  @links
end

#titleObject (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 get_message_doc(doc)
  doc.at_xpath(".//*[contains(@class, 'message')]")
end

#inspectObject



69
70
71
# File 'lib/kuport/message.rb', line 69

def inspect
  "title: #{title}\nbody: #{body}\nlinks: #{links}".freeze
end


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


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_hObject



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_sObject



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