lib/ids_please.rb in ids_please-1.1.4 vs lib/ids_please.rb in ids_please-2.0.0.beta
- old
+ new
@@ -1,56 +1,24 @@
require 'uri'
require 'cgi'
-require_relative 'ids_please/base_parser'
-require_relative 'ids_please/facebook'
-require_relative 'ids_please/google_plus'
-require_relative 'ids_please/instagram'
-require_relative 'ids_please/blogger'
-require_relative 'ids_please/ameba'
-require_relative 'ids_please/hi5'
-require_relative 'ids_please/livejournal'
-require_relative 'ids_please/linkedin'
-require_relative 'ids_please/pinterest'
-require_relative 'ids_please/reddit'
-require_relative 'ids_please/twitter'
-require_relative 'ids_please/tumblr'
-require_relative 'ids_please/vimeo'
-require_relative 'ids_please/youtube'
-require_relative 'ids_please/soundcloud'
-require_relative 'ids_please/vkontakte'
-require_relative 'ids_please/odnoklassniki'
-require_relative 'ids_please/moikrug'
+require_relative 'ids_please/parsers'
+require_relative 'ids_please/grabbers'
class IdsPlease
- VERSION = '1.1.4'
+ def self.parsers
+ IdsPlease::Parsers
+ end
- attr_accessor :original, :unrecognized, :parsed
+ def self.grabbers
+ IdsPlease::Grabbers
+ end
- SOCIAL_NETWORKS = [
- IdsPlease::GooglePlus,
- IdsPlease::Vkontakte,
- IdsPlease::Twitter,
- IdsPlease::Facebook,
- IdsPlease::Instagram,
- IdsPlease::Blogger,
- IdsPlease::Ameba,
- IdsPlease::Hi5,
- IdsPlease::Linkedin,
- IdsPlease::Livejournal,
- IdsPlease::Reddit,
- IdsPlease::Pinterest,
- IdsPlease::Soundcloud,
- IdsPlease::Vimeo,
- IdsPlease::Youtube,
- IdsPlease::Odnoklassniki,
- IdsPlease::Tumblr,
- IdsPlease::Moikrug
- ]
+ attr_accessor :original, :unrecognized, :parsed, :grabbed
def initialize(*args)
- @original = args.dup
+ @original = args.flatten.dup
end
def recognize
@recognized = {}
@unrecognized = []
@@ -60,25 +28,47 @@
def recognized
Hash[@recognized.map { |parser, links| [ parser.to_sym, links ] }]
end
def parse
+ interact(:parsers)
+ end
+
+ def grab
+ interact(:grabbers)
+ end
+
+ private
+
+ def interact(interactors = :parsers)
recognize
- @parsed = Hash.new { |hash, parser| hash[parser.to_sym] = [] }
- @recognized.each do |parser, links|
- @parsed[parser].concat parser.parse(links)
+ interacted = Hash.new { |hash, network| hash[network] = [] }
+ @recognized.each do |network, links|
+ interactor = IdsPlease.send(interactors).by_symbol(network)
+ interacted[network].concat interactor.interact(links)
end
+ self.instance_variable_set(interacted_var(interactors), interacted)
+
+ interacted
end
- private
+ def interacted_var(interactors)
+ if interactors == :parsers
+ :@parsed
+ elsif interactors == :grabbers
+ :@grabbed
+ else
+ throw 'Wrong interactors type'
+ end
+ end
def recognize_link(link)
link = "http://#{link}" unless link =~ /\Ahttps?:\/\//
parsed_link = URI(URI.encode(link))
- SOCIAL_NETWORKS.each do |network|
+ IdsPlease::Parsers.each do |network|
if parsed_link.host =~ network::MASK
- @recognized[network] ||= []
- @recognized[network] << parsed_link
+ @recognized[network.to_sym] ||= []
+ @recognized[network.to_sym] << parsed_link
return
end
end
unrecognized << link
end