Class: Turl::Link

Inherits:
ApplicationRecord show all
Defined in:
lib/turl/link.rb

Class Method Summary collapse

Class Method Details

.from_response!(resp, tweet) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/turl/link.rb', line 11

def self.from_response!(resp, tweet)
  url = resp.expanded_url.to_s
  find_or_initialize_by(normalized_url: normalize(url)).tap do |u|
    if u.new_record?
      begin
        title = URI.open(u.normalized_url) do |resp|
          Nokogiri::HTML(resp.read).title
        end
      rescue => ex
        Turl.logger.error "Error when detecting title for #{u.normalized_url}: #{ex}"
      end

      u.update!(
        title: title,
      )
    else
      u.save!
    end

    u.tweets << tweet unless u.tweets.include?(tweet)
  end
end

.ignored?(url) ⇒ Boolean

TODO: Make it configurable

Returns:

  • (Boolean)


7
8
9
# File 'lib/turl/link.rb', line 7

def self.ignored?(url)
  url.expanded_url.host == 'twitter.com'
end

.normalize(url) ⇒ Object



34
35
36
# File 'lib/turl/link.rb', line 34

def self.normalize(url)
  Normalizer.normalize(url)
end