Antoine
Antoine

Reputation: 569

How to make an array of element in ruby from a string

I have a giant string as follow obtained from Nokogiri inside a <script> tag:

   ....com\\/shop\",\"url\":\"?search=espadrille&options=reserve-eligible\",\"slug\":\"options\",\"order\":null,\"matchesMainFilter\":null,\"name\":\"Reserve Eligible\",\"type\":\"options\",\"identifier\":\"reserve-eligible\"}],\"title\":\"Options\",\"identifier\":\"options\",\"remove_url\":\"?search=espadrille\",\"classification\":\"\",\"view_all_url\":\"\",\"count\":\"\",\"slug\":\"\"},{\"children\":[{\"id\":95,\"children\":[{\"id\":150,\"children\":[],\"count\":1,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=chanel&chanel=lambskin&search=espadrille\",\"slug\":\"lambskin\",\"order\":null,\"matchesMainFilter\":false,\"name\":\"Lambskin\",\"type\":\"brand\"}],\"count\":7,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=chanel&search=espadrille\",\"slug\":\"chanel\",\"order\":null,\"matchesMainFilter\":false,\"name\":\"Chanel\",\"type\":\"brand\",\"identifier\":\"chanel\"},{\"id\":98,\"children\":[],\"count\":1,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=louboutin&search=espadrille\",\"slug\":\"louboutin\",\"order\":null,\"matchesMainFilter\":false,\"name\":\"Christian Louboutin\",\"type\":\"brand\",\"identifier\":\"christian-louboutin\"},{\"id\":103,\"children\":[],\"count\":3,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=gucci&search=espadrille\",\"slug\":\"gucci\",\"order\":null,\"matchesMainFilter\":false,\"name\":\"Gucci\",\"type\":\"brand\",\"identifier\":\"gucci\"},{\"id\":104,\"children\":[],\"count\":1,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=hermes&search=espadrille\",\"slug\":\"hermes\",\"order\":null,\"matchesMainFilter\":false,\"name\":\"Hermes\",\"type\":\"brand\",\"identifier\":\"hermes\"},{\"id\":107,\"children\":[{\"id\":132,\"children\":[],\"count\":1,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=louis-vuitton&louis-vuitton=louis-vuitton-monogram&search=espadrille\",\"slug\":\"louis-vuitton-monogram\",\"order\":null,\"matchesMainFilter\":false,\"name\":\"Monogram\",\"type\":\"brand\"}],\"count\":2,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=louis-vuitton&search=espadrille\",\"slug\":\"louis-vuitton\",\"order\":null,\"matchesMainFilter\":false,\"name\":\"Louis Vuitton\",\"type\":\"brand\",\"identifier\":\"louis-vuitton\"},{\"id\":115,\"children\":[],\"count\":4,\"applied\":false,\"removeUrl\":\"https:\\/\\/www.fashionphile.com\\/shop\",\"url\":\"?brands=valentino&search=espadrille\",\"slug\":\"valentino\",\"order...

I want to find a way to get all the brands=xxxxxx inside an array like ["chanel", "LV"] or maybe a hash {brand1: "chanel", brand2: "LV"}.

--- EDIT ---

And how can I access <meta itemprop=\"brand\" content=\"Chanel\"> and associate it with its <span class=\"sale-price\" itemprop=\"price\" content=\"595.00\">in an array or hash like this:

hash = {chanel: "200", LV: "100"}

Here is the script without data to make it smaller:

<script>
    var bootstrappedShopResults = {"products":"<div class=\"container-fluid\">\n    <div class=\"product-flex\">\n            <\/div>\n<\/div>\n","meta":{"pagination":"","total":0,"itemsFrom":null,"itemsTo":null},"aggregations":[{"children":[],"title":"Price","identifier":"price","remove_url":"?","classification":"","view_all_url":"","count":"","slug":""},{"children":[],"title":"Options","identifier":"options","remove_url":"?","classification":"","view_all_url":"","count":"","slug":""},{"children":[],"title":"Brands","identifier":"brands","remove_url":"?","classification":"","view_all_url":"","count":"","slug":""},{"children":[],"title":"Condition","identifier":"condition","remove_url":"?","classification":"","view_all_url":"","count":"","slug":""}],"parameters":{"pageSize":180,"sort":"date-desc","search":"espadrille.json"},"appliedFilters":[],"mainFilter":null,"pageTitle":"Shop Pre owned Designer Handbags | Used Designer Bags | Fashionphile","metaDescription":"Fashionphile offers a wide selection of pre-owned designer handbags and accessories. Add quality, used designer bags and more to your collection today!","removeSearchUrl":"?pageSize=180&sort=date-desc"};
</script>

Upvotes: 1

Views: 66

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110685

str.gsub(/(?<=\bbrands=)[^&]+/).to_a
  #=> ["chanel", "chanel", "louboutin", "gucci", "hermes", "louis-vuitton",
  #    "louis-vuitton", "valentino"]

Tack on .uniq if desired.

This makes use of the fact that String#gsub returns an enumerator when used without a block.

Upvotes: 2

kim pastro
kim pastro

Reputation: 61

you can scan with a regex, like so:

brands_array = string.scan(/brands=([^&]+)/)

Which will provide:

[["chanel"], ["chanel"], ["louboutin"], ["gucci"], ["hermes"], ["louis-vuitton"], ["louis-vuitton"], ["valentino"]]

If you don't wanna repeated, just call uniq:

brands_array = string.scan(/brands=([^&]+)/).uniq

This will return:

[["chanel"], ["louboutin"], ["gucci"], ["hermes"], ["louis-vuitton"], ["valentino"]]

Upvotes: 2

Related Questions