Skip to content

Commit a0e1992

Browse files
author
julianfoo
committed
WIP: Refactor Expansions object
1 parent e3327ff commit a0e1992

1 file changed

Lines changed: 40 additions & 8 deletions

File tree

lib/tweetkit/response.rb

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class Annotations
111111
attr_accessor :context_annotations, :entity_annotations
112112

113113
def initialize(context_annotations, entity_annotations)
114-
@context_annotations = Context.new(context_annotations)
115-
@entity_annotations = Entity.new(entity_annotations)
114+
@context_annotations = Context.new(context_annotations) unless context_annotations.nil? || context_annotations.empty?
115+
@entity_annotations = Entity.new(entity_annotations) unless entity_annotations.nil? || entity_annotations.empty?
116116
end
117117

118118
class Context
@@ -121,40 +121,72 @@ class Context
121121
attr_accessor :annotations
122122

123123
def initialize(annotations)
124-
@annotations = annotations
124+
@annotations = annotations.collect { |annotation| Annotation.new(annotation) }
125125
end
126126

127127
def each(*args, &block)
128128
annotations.each(*args, &block)
129129
end
130+
131+
class Annotation
132+
attr_accessor :domain, :entity
133+
134+
def initialize(annotation)
135+
@domain = annotation['domain']
136+
@entity = annotation['entity']
137+
end
138+
end
130139
end
131140

132141
class Entity
133142
include Enumerable
134143

135-
attr_accessor :annotations
144+
attr_accessor :annotations, :mentions
136145

137-
def initialize(annotations)
138-
@annotations = annotations
146+
def initialize(entity_annotations)
147+
@annotations = entity_annotations['annotations'].collect { |annotation| Annotation.new(annotation) } if entity_annotations['annotations']
148+
@mentions = entity_annotations['mentions'].collect { |annotation| Mention.new(annotation) } if entity_annotations['mentions']
139149
end
140150

141151
def each(*args, &block)
142152
annotations.each(*args, &block)
143153
end
144154

145155
class Annotation
146-
class Domain
156+
attr_accessor :end, :probability, :start, :text, :type
157+
158+
def initialize(annotation)
159+
@end = annotation['end']
160+
@probability = annotation['probability']
161+
@start = annotation['start']
162+
@text = annotation['normalized_text']
163+
@type = annotation['type']
147164
end
165+
end
148166

149-
class Entity
167+
class Mention
168+
attr_accessor :end, :id, :start, :username
169+
170+
def initialize(mention)
171+
@end = mention['end']
172+
@id = mention['id']
173+
@start = mention['start']
174+
@username = mention['username']
150175
end
151176
end
152177
end
153178
end
154179
end
155180

156181
class Expansions
182+
attr_accessor :media, :places, :polls, :tweets, :users
183+
157184
def initialize(expansions)
185+
@media = expansions['media']
186+
@places = expansions['places']
187+
@polls = expansions['polls']
188+
@tweets = expansions['tweets']
189+
@users = expansions['users']
158190
end
159191
end
160192

0 commit comments

Comments
 (0)