Sha256: 063d5fa020116bf29fd9d2c393700baf3ef6380674304aa1c7a76d01e6c55fd5
Contents?: true
Size: 1.59 KB
Versions: 2
Compression:
Stored size: 1.59 KB
Contents
require 'social_parser/link' module SocialParser class InvalidURIError < Exception; end module Provider class Base < ::SocialParser::Link def self.parse(attrs) if attrs[:provider] SocialParser::Provider.const_get(attrs[:provider].to_s.capitalize).new(attrs) else providers.map do |provider| SocialParser::Provider.const_get(provider.to_s.capitalize).new(attrs) end.find(&:valid?) or ::SocialParser::Link.new(attrs) end end def username return @username if @username if @url_or_username and invalid_url_format?(@url_or_username) @url_or_username elsif url_from_attributes parse_from_url end end def url return url_from_attributes if url_from_attributes "https://www.#{provider.to_s}.com/#{username}" end def domain 'com' end def embed_url nil end def valid? (@provider and @provider == provider) or (username and URI.parse(url_from_attributes).host.match("#{provider.to_s}.#{domain}")) rescue URI::BadURIError, URI::InvalidURIError false end alias_method :id, :username private def parse_from_url URI.parse(url_from_attributes).path.split('/')[1] rescue URI::BadURIError, URI::InvalidURIError nil end def self.providers @providers ||= [:facebook, :github, :twitter, :youtube, :instagram, :linkedin, :medium, :qiita, :pinterest, :google, :vimeo] end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
social_parser-1.1.0 | lib/social_parser/provider/base.rb |
social_parser-1.0.8 | lib/social_parser/provider/base.rb |