class SocialMediaParser::Provider::Base

Public Class Methods

parse(attributes) click to toggle source
# File lib/social_media_parser/provider/base.rb, line 7
def self.parse(attributes)
  providers.map do |provider|
    SocialMediaParser::Provider.const_get(provider.capitalize).new(attributes)
  end.find(&:valid?) or ::SocialMediaParser::Link.new(attributes)
end

Private Class Methods

providers() click to toggle source

Does a file name lookup in the providers/ folder and outputs all file names, except for this base file

# File lib/social_media_parser/provider/base.rb, line 46
def self.providers
  @providers ||= Dir.entries(__dir__)
    .reject{|f| File.directory? f }.map{|s| s.gsub(".rb", "")} - ["base"]
end

Public Instance Methods

url() click to toggle source
# File lib/social_media_parser/provider/base.rb, line 22
def url
  return url_from_attributes if url_from_attributes
  "https://www.#{provider}.com/#{username}"
end
username() click to toggle source
# File lib/social_media_parser/provider/base.rb, line 13
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_username_from_url
  end
end
valid?() click to toggle source
# File lib/social_media_parser/provider/base.rb, line 27
def valid?
  (@provider and @provider.downcase == provider) or
  (username and URI.parse(url_from_attributes).host.match("#{provider}.com"))
rescue URI::BadURIError, URI::InvalidURIError
  false
end

Private Instance Methods

parse_username_from_url() click to toggle source

Common social media url format, like http(s)://(www.)[provider].com/ Overwrite this in subclasses when url formatting is different

# File lib/social_media_parser/provider/base.rb, line 38
def parse_username_from_url
  URI.parse(url_from_attributes).path.split("/")[1]
rescue URI::BadURIError, URI::InvalidURIError
  nil
end