1

I have this list of meta tags in my view HTML (after the page loads). The tag is generated dynamically,

@meta = "\n  <meta content=\content1\">\n  <meta content=\content2\">\n  <meta content='content2\">\n  ....... <meta content=\"2019/01/10 09:59:59 +0900\" name=\"r_end\">\n \n"

I wanted to fetch the value 2019/01/10 09:59:59 +0900 inside content i.e.<meta content=\"2019/01/10 09:59:59 +0900\" name=\"r_end\">. Is there a way to get the value of the content from the meta tag.

Sam
  • 1,623
  • 1
  • 19
  • 31
  • 2
    If it is _your_ view, you probably have put it there. – Stefan Mar 05 '19 at 09:04
  • this meta tag is generated dynamically, I have to grab the value and pass it some other variable. – Sam Mar 05 '19 at 09:05
  • 2
    How is it generated? Could you please add code of your view? Not generated html but your `.html.erb` (or maybe slim) view – Vasilisa Mar 05 '19 at 09:08
  • It is not there in any of the erb files, we have content management tool from there we are fetching the entire HTML and rendering it. I wanted to grab the value in the "content" and store in some variable and pass on. – Sam Mar 05 '19 at 09:11
  • 1
    @Sam _"we are fetching the entire HTML and rendering it"_ – so you have the HTML containing the `` tag as a string? – Stefan Mar 05 '19 at 09:14
  • @Stefan yes the final generated HTML at my end contains that meta tag not as a string but as an HTML element. – Sam Mar 05 '19 at 09:15
  • 1
    @Sam _"not as a string but as an HTML element"_ – what do you mean by that? What kind of Ruby class is it? – Stefan Mar 05 '19 at 09:17
  • @Sam There should be code somewhere that generates that string. It is probably located in a place not under your control. You need to search through your entire system to find it. – sawa Mar 05 '19 at 09:19
  • @sawa I found that variable "@meta" in my code returns; "\n \n \n – Sam Mar 05 '19 at 09:27
  • @Sam Try to extract the code fragment(s) that you think is relevant, and add that to your question with explanation. Hopefully, you will get some help. – sawa Mar 05 '19 at 09:31
  • 2
    @Sam It should be much easier to find how `@meta` is generated rather than trying to parse `@meta` to extract what you want. – sawa Mar 05 '19 at 09:34
  • @Sam that `@meta` value looks very much like a string. You might want to edit your question and include that information. – Stefan Mar 05 '19 at 09:34
  • @Stefan Done, edited it ! – Sam Mar 05 '19 at 09:41
  • @Sam is the CMS you're fetching the data from part of your Rails setup (like a gem or engine)? If so, you should dig into its code to find the source of that meta tag. You might be able to retrieve it directly or maybe provide the value yourself explicitly. – Stefan Mar 05 '19 at 09:50

2 Answers2

3

Given a @meta variable containing some HTML snippet as a string:

@meta = <<-HTML
  <meta name="foo" content="content1">
  <meta name="bar" content="content2">
  <meta content="2019/01/10 09:59:59 +0900" name="r_end">
HTML

You can use Nokogiri to parse it:

require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse(@meta)
doc.at_css('meta[name="r_end"]')['content']
#=> "2019/01/10 09:59:59 +0900"

at_css returns the first element matching the given CSS selector and [] returns the value for the given attribute.

Stefan
  • 109,145
  • 14
  • 143
  • 218
0

How about a using simple regular expression to capture the value using String#scan.

This will work only if the name of metatag doesn't change

@meta = "\n  <meta content=\content1\">\n  <meta content=\content2\">\n  <meta content='content2\">\n  ....... <meta content=\"2019/01/10 09:59:59 +0900\" name=\"r_end\">\n \n"

@meta.scan(/content=\"(.*)\" name=\"r_end\"/) 
#=> [["2019/01/10 09:59:59 +0900"]]

Explanation:

The above code will capture the value of content with metatag name="r_end"

If you think there might be some other HTML elements with name="r_end" you might need to add some other identifier in the regex

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88