module Save

Public Instance Methods

delete_failures(hsh) click to toggle source

Remove negatives from the contacts hash. Deletes a key value pair with a value of either nil or false. Remember that false is a stored in hash as a string.

# File lib/gimme_poc/save.rb, line 47
def delete_failures(hsh)
  hsh.delete_if { |_k, v| v.nil? || v == 'false' }
end
save_available_contacts(url, hsh = scan_for_contacts) click to toggle source

Saves any available contact info to @contact_links.

# File lib/gimme_poc/save.rb, line 52
def save_available_contacts(url, hsh = scan_for_contacts)
  if something_to_save?(hsh)
    LogMessages.saving_contact_info(url)
    if hsh.is_a?(Hash)
      hsh.each do |k, v|
        save_link(k, v) # saves to @contact_links
      end
      delete_failures(@contact_links)
      puts "#{@contact_links}".cyan # same as @contact_links
    else
      fail ArgumentError, "expected hash but got #{hsh.class}"
    end
    Gimme::Search::POC.new(url, @contact_links)
  else
    LogMessages.nothing_to_save
    return
  end
end
scan_for_contacts() click to toggle source

Returns anything that is possible to save, otherwise returns nil. Booleans for phone, email, or contact form will display True or False.

Add periods to link hrefs to prevent false positives. Must escape periods with a backslash or else it will be a regex wild card.

# File lib/gimme_poc/save.rb, line 16
def scan_for_contacts
  {
    contactpage: link_with_href('contact'),
    email_present: "#{email_available?}",
    phone_present: "#{phone_available?}",
    contact_form: "#{contactform_available?}",
    facebook: link_with_href('facebook\.'),
    twitter: link_with_href('twitter\.'),
    youtube: link_with_href('youtube\.'),
    googleplus: link_with_href('plus\.google\.'),
    linkedin: link_with_href('linkedin\.')
  }
rescue => e
  puts "Error: #{e}"
end
something_to_save?(hsh) click to toggle source

Boolean, returns true if anything is present after running scan_for_contacts and deleting failures. Remember that false is a string in the hash

# File lib/gimme_poc/save.rb, line 6
def something_to_save?(hsh)
    hsh.reject! { |k, v| v.nil? || v == 'false' }.any?
end