Class: TweetSearch::Tweet

Inherits:
Object
  • Object
show all
Defined in:
lib/tweetsearch/tweet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Tweet

Returns a new instance of Tweet.



6
7
8
9
10
11
# File 'lib/tweetsearch/tweet.rb', line 6

def initialize(data)
  @id = data['id']
  @created_at = DateTime.parse(data['created_at'])
  @text = data['text']
  @media_urls = data.dig('entities', 'media')&.map { |media| media['media_url_https'] }
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



4
5
6
# File 'lib/tweetsearch/tweet.rb', line 4

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/tweetsearch/tweet.rb', line 4

def id
  @id
end

#media_urlsObject (readonly)

Returns the value of attribute media_urls.



4
5
6
# File 'lib/tweetsearch/tweet.rb', line 4

def media_urls
  @media_urls
end

#textObject (readonly)

Returns the value of attribute text.



4
5
6
# File 'lib/tweetsearch/tweet.rb', line 4

def text
  @text
end

Class Method Details

.find_by(tags:, **options) ⇒ Object



13
14
15
16
17
# File 'lib/tweetsearch/tweet.rb', line 13

def self.find_by(tags:, **options)
  tweets = TwitterClient.search_tweets(tags)
  tweets.select! { |data| data.dig('entities', 'media') } if options[:media_only]
  tweets.map { |data| new(data) }
end