-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
entities.rb
96 lines (82 loc) · 2.29 KB
/
entities.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
# @return [Boolean]
def entities?
!@attrs[:entities].nil? && @attrs[:entities].any? { |_, array| array.any? }
end
memoize :entities?
# @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
# @return [Boolean]
def hashtags?
hashtags.any?
end
memoize :hashtags?
# @note Must include entities in your request for this method to work
# @return [Array<Twitter::Media>]
def media
extended_entities = entities(MediaFactory, :media, :extended_entities)
extended_entities.empty? ? entities(MediaFactory, :media) : extended_entities
end
memoize :media
# @return [Boolean]
def media?
media.any?
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
# @return [Boolean]
def symbols?
symbols.any?
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
# @return [Boolean]
def uris?
uris.any?
end
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
# @return [Boolean]
def user_mentions?
user_mentions.any?
end
memoize :user_mentions?
private
# @param klass [Class]
# @param key2 [Symbol]
# @param key1 [Symbol]
def entities(klass, key2, key1 = :entities)
@attrs.fetch(key1.to_sym, {}).fetch(key2.to_sym, []).collect do |entity|
klass.new(entity)
end
end
end
end