Last active
December 30, 2015 17:49
-
-
Save davekaro/7863843 to your computer and use it in GitHub Desktop.
Marginally better version of https://github.com/JamesChevalier/hashtagged/blob/master/app.rb#L17-L27
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
timeline = client.user_timeline(@user_name, { count: 200, include_entities: true }) | |
hashtag_list = timeline.map { |tweet| tweet.hashtags.collect(&:text) } | |
@hashtags = hashtag_list.uniq.map do |tag| | |
{ text: tag, | |
weight: hashtag_list.grep(tag).size, | |
link: { href: "https://twitter.com/search?q=%23#{tag}", target: '_blank', title: "#{tag}" } } | |
end | |
@hashtags = @hashtags.to_json |
Oh and https://github.com/JamesChevalier/hashtagged/blob/master/app.rb#L18 should be
hashtag_list = timeline.flat_map { |tweet| tweet.hashtags.map(&:text) }
instead of
hashtag_list = timeline.map { |tweet| tweet.hashtags.map(&:text) }.flatten
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Re:
map
vscollect
. Whatever works. I've heard many different preferences. I like to usecollect
when I'm just "collecting" one attribute of the objects in the array. I like to usemap
when I'm "mapping" the array to a different array, via some kind of transform.Re: multi-line map block. I wasn't aware of the difference in associativity of the
{
character and thedo
keyword. However, you should check out http://devblog.avdi.org/2011/07/26/the-procedurefunction-block-convention-in-ruby/. I still follow the old way of line length. I think your block is functional, so sticking with curly braces would follow the new convention. I don't think you'll have the associativity issue here since you are doing assignment, which doesn't take two parameters.Re:
count
vsgrep(tag).size
. I had never seedgrep
used on an arary. It's neat. But I think thecount
method is much easier to read. Bonus if it's slightly faster :)