Skip to content

Instantly share code, notes, and snippets.

@PlanetRoast
Created November 8, 2017 10:36
Show Gist options
  • Save PlanetRoast/478a599a9ea5b56d0fea08777e302c54 to your computer and use it in GitHub Desktop.
Save PlanetRoast/478a599a9ea5b56d0fea08777e302c54 to your computer and use it in GitHub Desktop.
class Profile < ApplicationRecord
mount_uploader :avatar, AvatarUploader
geocoded_by :postcode
after_validation :geocode
has_many :profile_services
has_many :services, :through => :profile_services
has_many :testimonials
belongs_to :user
has_many :scrape_data
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_avatar
def suggest_name_for_avatar
name.downcase.gsub(" ", "-") rescue nil
end
def crop_avatar
avatar.recreate_versions! if crop_x.present?
end
end
module YellScrape
class Scrape
def initialize(html)
@html = Nokogiri::HTML(html)
end
def business_name
@business_name = @html.xpath("//h1").text
end
def phone
@html.xpath("//strong[@itemprop='telephone']").text
end
def facebook
@html.xpath("//a[@class='sprite-findUs-facebook']").attribute('href') rescue nil
end
def twitter
@html.xpath("//a[@class='sprite-findUs-twitter']").attribute('href') rescue nil
end
def googleplus
@html.xpath("//a[@class='sprite-findUs-googleplus']").attribute('href') rescue nil
end
def address
@html.xpath("//span[@itemprop='streetAddress']").text
end
def postcode
@html.xpath("//span[@itemprop='postalCode']").text
end
def website
business_capsule_links = @html.xpath("//div[@class='col-sm-19 businessCapsule--callToAction']/a")
if business_capsule_links.nil?
return ""
else
business_capsule_links.each do |l|
if l.text == "Visit Website"
return l.attribute("href")
else
return ""
end
end
end
end
def services_and_products
services = []
@html.xpath("//div[@class='bipContent--block services']/ul/li").children.each do |li|
services << li
end
return services
end
def business_overview
# A string containing whatever is in the 'business overview' box minus the h2
@html.xpath("//div[@class='bipContent--block aboutUs']/*[not(self::h2)]").to_s.html_safe
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment