-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
215 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'memoizable' | ||
require 'twitter/entity/hashtag' | ||
require 'twitter/entity/symbol' | ||
require 'twitter/entity/uri' | ||
require 'twitter/entity/user_mention' | ||
require 'twitter/media_factory' | ||
|
||
module Twitter | ||
module Entities | ||
include Memoizable | ||
|
||
# @note Must include entities in your request for this method to work | ||
# @return [Array<Twitter::Entity::Hashtag>] | ||
def hashtags | ||
entities(Entity::Hashtag, :hashtags) | ||
end | ||
memoize :hashtags | ||
|
||
# @note Must include entities in your request for this method to work | ||
# @return [Array<Twitter::Media>] | ||
def media | ||
entities(MediaFactory, :media) | ||
end | ||
memoize :media | ||
|
||
# @note Must include entities in your request for this method to work | ||
# @return [Array<Twitter::Entity::Symbol>] | ||
def symbols | ||
entities(Entity::Symbol, :symbols) | ||
end | ||
memoize :symbols | ||
|
||
# @note Must include entities in your request for this method to work | ||
# @return [Array<Twitter::Entity::URI>] | ||
def uris | ||
entities(Entity::URI, :urls) | ||
end | ||
memoize :uris | ||
alias_method :urls, :uris | ||
|
||
# @note Must include entities in your request for this method to work | ||
# @return [Array<Twitter::Entity::UserMention>] | ||
def user_mentions | ||
entities(Entity::UserMention, :user_mentions) | ||
end | ||
memoize :user_mentions | ||
|
||
private | ||
|
||
# @return [Boolean] | ||
def entities? | ||
!@attrs[:entities].nil? && @attrs[:entities].any? { |_, array| !array.empty? } | ||
end | ||
memoize :entities? | ||
|
||
# @param klass [Class] | ||
# @param key [Symbol] | ||
def entities(klass, key) | ||
if entities? | ||
Array(@attrs[:entities][key.to_sym]).map do |entity| | ||
klass.new(entity) | ||
end | ||
else | ||
warn "#{Kernel.caller.first}: To get #{key.to_s.tr('_', ' ')}, you must pass `:include_entities => true` when requesting the #{self.class}." | ||
[] | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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