I have Facebook likes saved in a table (they’re in a string) and do the following in the console to return the likes using the like method for the user model:
User.first.likes
=> "--- !seq:Koala::Facebook::GraphCollection \n- name: Rome Sweet Rome\n category:
Book\n id: \"136333439795671\"\n created_time: 2011-09-05T12:03:09+0000\n- name:
Drawn Together\n category: Tv show\n id: \"8694990902\"\n created_time:
2008-10-03T10:39:46+0000\n"
Below it is in YAML:
y User.first.likes
--- |
--- !seq:Koala::Facebook::GraphCollection
- name: Rome Sweet Rome
category: Book
id: "136333439795671"
created_time: 2011-09-05T12:03:09+0000
- name: Drawn Together
category: Tv show
id: "8694990902"
created_time: 2008-10-03T10:39:46+0000
=> nil
I want the end result to give me something like:
>> ["Rome sweet Rome", "Drawn Together"]
Split the string into separate lines, delimited by the
\ncharacter (or if it comes across as the string “\n”, use double-quotes to delimit on that string)Then collect all elements that start with “- name: ” into their own array:
Then take each of the elements in
name_elementsand strip out the leading"- name: "text, and remove leading an trailing whitespace