Skip to content

Commit e93d8a0

Browse files
Parse and store html element h1, h2, h3 and a tag contents
1 parent 115f3e3 commit e93d8a0

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

app/controllers/contents_controller.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@ def parse
66
begin
77
response = HTTParty.get(params[:ref_url])
88
doc = Nokogiri::HTML(response.body)
9-
puts doc.at('meta[property="og:title"]')['content']
109

11-
render json: { success: true, code: response.code, message: response.message, title: doc.at('meta[property="og:title"]')['content'] }
10+
%w[h1 h2 h3 a].each do |tag|
11+
doc.xpath("//#{tag}").each do |element|
12+
extract_element(element, tag)
13+
end
14+
end
15+
16+
render json: { success: true, code: response.code, message: response.message }
1217
rescue Exception => e
1318
render json: e
1419
end
1520
end
21+
22+
private
23+
def extract_element(element, tag='')
24+
content = Content.new
25+
content.url = params[:ref_url]
26+
content.tag = tag
27+
content.content = element.text
28+
content.save!
29+
end
1630
end

0 commit comments

Comments
 (0)