Class: MarmitonCrawler::Recipe

Inherits:
Object
  • Object
show all
Defined in:
lib/marmiton_crawler.rb

Constant Summary collapse

MARMITON_HOST =
'http://www.marmiton.org/'
MARMITON_MOBILE_HOST =
'http://m.marmiton.org/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Recipe

Returns a new instance of Recipe.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

Instance Attribute Details

#cooktimeObject (readonly)

Returns the value of attribute cooktime.



13
14
15
# File 'lib/marmiton_crawler.rb', line 13

def cooktime
  @cooktime
end

#imageObject (readonly)

Returns the value of attribute image.



13
14
15
# File 'lib/marmiton_crawler.rb', line 13

def image
  @image
end

#ingredientsObject (readonly)

Returns the value of attribute ingredients.



13
14
15
# File 'lib/marmiton_crawler.rb', line 13

def ingredients
  @ingredients
end

#preptimeObject (readonly)

Returns the value of attribute preptime.



13
14
15
# File 'lib/marmiton_crawler.rb', line 13

def preptime
  @preptime
end

#stepsObject (readonly)

Returns the value of attribute steps.



13
14
15
# File 'lib/marmiton_crawler.rb', line 13

def steps
  @steps
end

#titleObject (readonly)

Returns the value of attribute title.



13
14
15
# File 'lib/marmiton_crawler.rb', line 13

def title
  @title
end

Instance Method Details

#to_hashObject

export all informations to an array



51
52
53
54
55
56
57
58
# 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_jsonObject



61
62
63
# File 'lib/marmiton_crawler.rb', line 61

def to_json
  return self.to_hash.to_json
end