Class: SocialParser::Provider::Base

Inherits:
Link
  • Object
show all
Defined in:
lib/social_parser/provider/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Link

#attributes, #initialize, #provider

Constructor Details

This class inherits a constructor from SocialParser::Link

Class Method Details

.parse(attrs) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/social_parser/provider/base.rb', line 10

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

Instance Method Details

#domainObject



36
37
38
# File 'lib/social_parser/provider/base.rb', line 36

def domain
  'com'
end

#embed_urlObject



40
41
42
# File 'lib/social_parser/provider/base.rb', line 40

def embed_url
  nil
end

#urlObject



31
32
33
34
# File 'lib/social_parser/provider/base.rb', line 31

def url
  return url_from_attributes if url_from_attributes
  "https://www.#{provider.to_s}.com/#{username}"
end

#usernameObject Also known as: id



22
23
24
25
26
27
28
29
# File 'lib/social_parser/provider/base.rb', line 22

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

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/social_parser/provider/base.rb', line 44

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