Skip to content

Instantly share code, notes, and snippets.

@piyushrajput
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save piyushrajput/3c8abb0ee073d4107b29 to your computer and use it in GitHub Desktop.

Select an option

Save piyushrajput/3c8abb0ee073d4107b29 to your computer and use it in GitHub Desktop.
# helper that takes a feedItem hash and converts it to HTML, including
# properly structuring any included message segments like @mentions.
def message_segments(msg_segments)
# an array of hashes - each hash a segment
html = ''
msg_segments.each do |segment|
html << case segment['type']
when 'Text'
segment['text']
when 'Link'
%Q(<a href="#{segment['url']}">#{segment['text']}</a>)
when 'Mention'
%Q(<a href="/users/#{segment['user']['id']}">#{segment['text']}</a>)
when 'Hashtag'
%Q(<a class="hashtag">#{segment['text']}</a>)
else # every segment has a text field, this ensures that new unknown segments don't break the app.
segment['text']
end
end
html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment