class EthicalClothing::Scraper

Public Class Methods

get_and_create_brands() click to toggle source

scrape page and instantiate brands

# File lib/ethical_clothing/scraper.rb, line 10
def self.get_and_create_brands
  session = Capybara::Session.new(:poltergeist)
  session.visit("http://simplylivandco.com/blog/best-places-to-buy-affordable-ethical-fashion")
  brand_elements = session.all("h3")

  brand_elements.each do |element|
    #grab brand name, price, and url
    #Skip any element that is not a brand
    if element.text.include?(':')
      info = element.text.split(':')
      name = info[0]
      price = info[1]
      url = element.first('a')['href']

      brand = EthicalClothing::Brand.new(name, price, url)

    else
      nil
    end
  end
  nil
end
get_description() click to toggle source

scrape website for description

# File lib/ethical_clothing/scraper.rb, line 34
def self.get_description
  session = Capybara::Session.new(:poltergeist)
  session.visit("http://simplylivandco.com/blog/best-places-to-buy-affordable-ethical-fashion")
  # desc_holder = []
  desc_elements = session.all("p")

  desc_elements.collect do |element|
    element.text
  end[3..-1]



end
match() click to toggle source

Add description as attribute to to brand instance

# File lib/ethical_clothing/scraper.rb, line 49
def self.match
  descs = self.get_description
  brands = EthicalClothing::Brand.all

  #iterate through brands and descriptions and match them based on index
  i = 0
  brands.each do |brand|
    j = 0
    descs.each do |desc|
      brand.description = desc if i == j
      j += 1
    end
    i += 1
  end
  #Get rid of second "Bamboo Body that appears twice on website list"
  EthicalClothing::Brand.all.delete_at(39)
end