Sha256: 1ec1a6d0814381a6df8fa3714e2f77925c5dfb84045a32d234ec37246bc7e230

Contents?: true

Size: 928 Bytes

Versions: 7

Compression:

Stored size: 928 Bytes

Contents

require 'public_suffix'
require 'uri'

module SocialParser

  class Link

    def initialize(attrs)
      attrs.each do |key, value|
        instance_variable_set("@#{key}", value) unless value.nil?
      end
    end

    def username
      nil
    end

    def provider
      nil
    end

    def url
      url_from_attributes
    end

    def attributes
      {
        url:      url,
        provider: provider,
        username: username
      }
    end

    private

    def url_from_attributes
      valid_url_format(@url) or valid_url_format(@url_or_username)
    end

    def valid_url_format(url)
      uri = URI.parse(url)
      return uri.to_s if %w(http https).include?(uri.scheme)
      return "http://#{url}" if PublicSuffix.valid?(URI.parse("http://#{url}").host)
    rescue URI::BadURIError, URI::InvalidURIError
      nil
    end

    def invalid_url_format?(url)
      !valid_url_format(url)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
social_parser-1.0.6 lib/social_parser/link.rb
social_parser-1.0.5 lib/social_parser/link.rb
social_parser-1.0.4 lib/social_parser/link.rb
social_parser-1.0.3 lib/social_parser/link.rb
social_parser-1.0.2 lib/social_parser/link.rb
social_parser-1.0.1 lib/social_parser/link.rb
social_parser-1.0.0 lib/social_parser/link.rb