VegaStudios
VegaStudios

Reputation: 324

Ruby - Remove HTML tags from CSV output

The code below exports the conversations an inbox perfectly. However, because the inbox is web-based the Message field contains raw HTML tags;

<div>
Hello,<br><br>
I had my profile switched
</div>

I've tried to use strip_tags on the end of threaded_conversation.threads.last.body and customer_message but to no avail. Is there a correct way to leave this string only text?

CSV.open("conversations_#{mailbox_id}.csv", "wb", col_sep: ';', headers: true, force_quotes: false) do |csv|
  csv << ["Number", "From", "Subject", "Created", "Message"]
  conversations.each do |conversation|
    customer = conversation.customer
    threaded_conversation = helpscout.conversation(conversation.id)
    sleep(0.3)
    customer_message =
              begin
                body_message = threaded_conversation.threads.last.body
              rescue
                body_message = 'exception'
              end
    csv << [conversation.number, customer.email, conversation.subject, conversation.createdAt, customer_message]
  end
end

Upvotes: 0

Views: 366

Answers (1)

Amadan
Amadan

Reputation: 198536

Use a proper HTML library:

require 'nokogiri'
text = Nokogiri::HTML(body).text
# => "\nHello,\nI had my profile switched\n\n"

Upvotes: 2

Related Questions