```ruby
require 'open-uri'
require 'open_uri_redirections'

class Link

  attr_reader :title

  def initialize(url)
    @url = url
    @title = self.grab_title
  end

  def grab_title
    begin
      body = open(@url, :allow_redirections => :all).read
    rescue => e
      case e
      when OpenURI::HTTPError
        return @url
      when SocketError
        return @url
      else
        raise e
      end
    end

    doc = Oga.parse_html(body)

    if doc.at_css('title') != nil
      return doc.at_css('title').text
    else
      return @url
    end
  end

end
```

Refactor Link Model to remove duplicate code
user
```ruby
require 'open-uri'
require 'open_uri_redirections'

class Link

  attr_reader :title

  def initialize(url)
    @url = url
    @title = self.grab_title
  end

  def grab_title
    begin
      body = open(@url, :allow_redirections => :all).read
    rescue => e
      case e
      when OpenURI::HTTPError
        return @url
      when SocketError
        return @url
      else
        raise e
      end
    end

    doc = Oga.parse_html(body)
    title = doc.at_css('title')

    title ? title.text : @url
  end

end
```
assistant