Class: Tweet

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tweet.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create!(tuit) ⇒ Tweet

It uses a Twitter::Tweet to find and update an existing Tweet or create a new one

Parameters:

  • tuit (Twitter::Tweet)

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/tweet.rb', line 19

def self.find_or_create!(tuit)
  opts = {
    id: tuit.id }

  if result = self.find_by(opts)
    result.update_attributes!(json: tuit.to_json)
  else
    result = self.new(opts.merge(json: tuit.to_json))
    user = User.find_or_create!(result)
    result.user = user
    result.save!
  end

  result
end

Instance Method Details

#created_atObject



43
44
45
46
# File 'app/models/tweet.rb', line 43

def created_at
  @created_at ||= Time.parse(source["created_at"])
  @created_at.in_time_zone
end

#sourceObject



35
36
37
# File 'app/models/tweet.rb', line 35

def source
  @source ||= OpenStruct.new(JSON.load(json))
end

#source_urlObject



39
40
41
# File 'app/models/tweet.rb', line 39

def source_url
  "https://twitter.com/#{user.username}/status/#{source.id_str}"
end