class MarmitonCrawler::Recipe

Constants

MARMITON_HOST
MARMITON_MOBILE_HOST

Attributes

cooktime[R]
image[R]
ingredients[R]
preptime[R]
steps[R]
title[R]

Public Class Methods

new(url) click to toggle source
# File lib/marmiton_crawler.rb, line 18
def initialize(url)
  if url.include? MARMITON_HOST or url.include? MARMITON_MOBILE_HOST

    url.gsub! MARMITON_MOBILE_HOST, MARMITON_HOST

    page =  Nokogiri::HTML(open(url).read)
    @title = page.css('h1.m_title span.item span.fn').text

    # get times
    @preptime = page.css('p.m_content_recette_info span.preptime').text.to_i
    @cooktime = page.css('p.m_content_recette_info span.cooktime').text.to_i

    # get ingredients
    ingredients_text = page.css('div.m_content_recette_ingredients').text
    @ingredients = sanitize(ingredients_text).split '- '
    @ingredients.delete_at(0) # to delete the first `Ingrédients (pour 2 personnes) :`

    # get steps
    steps_text = page.css('div.m_content_recette_todo').text
    @steps = sanitize(steps_text).split '. '
    @steps.delete_at(0) # to delete the first `Ingrédients (pour 2 personnes) :`

    # get image
    @image = page.css('a.m_content_recette_illu img.m_pinitimage').attr('src').to_s
    

  else
    raise ArgumentError, "Instantiation cancelled (ulr not from #{MARMITON_HOST})." 
  end
end

Public Instance Methods

to_hash() click to toggle source

export all informations to an array

# File lib/marmiton_crawler.rb, line 51
def to_hash
  attrs = Hash.new
  instance_variables.each do |var|
    str = var.to_s.gsub /^@/, ''
    attrs[str.to_sym] = instance_variable_get(var)
  end
  attrs
end
to_json() click to toggle source
# File lib/marmiton_crawler.rb, line 61
def to_json
  return self.to_hash.to_json
end

Private Instance Methods

sanitize(text) click to toggle source

remove `rn` & unwanted espaces

# File lib/marmiton_crawler.rb, line 69
def sanitize text
  ['  ', '\r\n', "\r\n"].each { |text_to_remove| text.gsub!(text_to_remove,'')}
  return text
end