Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Using tweet entities all the time #108

Open
wants to merge 7 commits into from

1 participant

This page is out of date. Refresh to see the latest.
View
1  lib/earthquake.rb
@@ -27,4 +27,5 @@
help
commands
id_var
+ entities
).each { |name| require_dependency File.expand_path("../earthquake/#{name}", __FILE__) }
View
89 lib/earthquake/entities.rb
@@ -0,0 +1,89 @@
+module Earthquake
+ module Entities
+ class << self
+ def apply(item)
+ if item["retweeted_status"] && item["truncated"]
+ text = apply(item["retweeted_status"])
+ screen_name = item["retweeted_status"]["user"]["screen_name"]
+ return "RT #{"@#{screen_name}".c(Earthquake.color_of(screen_name))}: #{text}"
+ end
+ text, item_entities = item.values_at("text", "entities")
+ if item_entities
+ text = text.dup
+ parse(item_entities).reverse_each do |entity|
+ entity.apply(text)
+ end
+ end
+ text.u
+ end
+
+ private
+
+ def parse(item_entities)
+ item_entities.flat_map{|type, entities|
+ entities.map{|entity|
+ klass = type.classify
+ const_get(klass).new(entity) if const_defined?(klass)
+ }.compact
+ }.sort_by!(&:position)
+ end
+ end
+
+ class Base
+ def initialize(entity)
+ @entity = entity
+ @first, @last = entity["indices"]
+ end
+
+ def position
+ @first
+ end
+
+ def apply(text)
+ text[@first...@last] = colored_text
+ text
+ end
+
+ def string
+ raise NotImplementedError, "need to define `string'"
+ end
+
+ private
+
+ def colored_text
+ s = string
+ s.c(Earthquake.color_of(s))
+ end
+ end
+
+ class UrlBase < Base
+ def string
+ Earthquake.config[:expand_url] && @entity["expanded_url"] || @entity["url"]
+ end
+
+ private
+
+ def colored_text
+ string.c(:url)
+ end
+ end
+
+ class Url < UrlBase
+ end
+
+ class Medium < UrlBase
+ end
+
+ class Hashtag < Base
+ def string
+ "#" + @entity["text"]
+ end
+ end
+
+ class UserMention < Base
+ def string
+ "@" + @entity["screen_name"]
+ end
+ end
+ end
+end
View
16 lib/earthquake/output.rb
@@ -100,23 +100,9 @@ def color_of(screen_name)
id = id2var(item["id"])
- text = (item["retweeted_status"] && item["truncated"] ? "RT @#{item["retweeted_status"]["user"]["screen_name"]}: #{item["retweeted_status"]["text"]}" : item["text"]).u
+ text = Entities.apply(item)
text.gsub!(/\s+/, ' ') unless config[:raw_text]
text.prepend("\n") if config[:raw_text]
- text = text.coloring(/@[0-9A-Za-z_]+/) { |i| color_of(i) }
- text = text.coloring(/(^#[^\s]+)|(\s+#[^\s]+)/) { |i| color_of(i) }
- if config[:expand_url]
- entities = (item["retweeted_status"] && item["truncated"]) ? item["retweeted_status"]["entities"] : item["entities"]
- if entities
- entities.values_at("urls", "media").flatten.compact.each do |entity|
- url, expanded_url = entity.values_at("url", "expanded_url")
- if url && expanded_url
- text = text.sub(url, expanded_url)
- end
- end
- end
- end
- text = text.coloring(URI.regexp(["http", "https"]), :url)
if item["_highlights"]
item["_highlights"].each do |h|
Something went wrong with that request. Please try again.