Skip to content

Instantly share code, notes, and snippets.

@eric-famiglietti
Created December 6, 2012 19:01
Show Gist options
  • Save eric-famiglietti/4227160 to your computer and use it in GitHub Desktop.
Save eric-famiglietti/4227160 to your computer and use it in GitHub Desktop.
OO Design Exercise Solution
class SocialExtractor
require 'nokogiri'
require 'open-uri'
def self.get_social_profile url
# If it's not a Twitter URL, throw exception
if url.nil? || !url.include?('twitter.com')
raise Exception, "Invalid URL."
end
# Make call to URL to grab content
doc = Nokogiri.HTML(open(url))
# Parse content
profile_card = doc.css('.profile-card-inner').first
fullname = profile_card.css('.fullname').first.text.strip
username = profile_card.css('.username').first.text.strip
bio = profile_card.css('.bio').first.text.strip
location_and_url = profile_card.css('.location-and-url').first
location = location_and_url.css('.location').text.strip
url = location_and_url.css('.url a').first.attr('title').strip
# Put content into response object
SocialProfileResponse.new fullname, username, bio, location, url
end
end
class SocialProfileResponse
attr_reader :fullname, :username, :bio, :location, :url
def initialize fullname, username, bio, location, url
@fullname = fullname
@username = username
@bio = bio
@location = location
@url = url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment