Class: RecipeScraper::Recipe

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

Overview

represent a recipe fetched from an Url

Constant Summary collapse

MARMITON_HOST =
{ desktop: 'marmiton.org/', mobile: 'm.marmiton.org/' }.freeze
G750_HOST =
{ desktop: '750g.com' }.freeze
CUISINEAZ_HOST =
{ desktop: 'cuisineaz.com/' }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Recipe

Instanciate a Recipe object with data crawled from an url

Parameters:

  • url (String)

    representing an url from Marmiton or 750g website



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/recipe_scraper.rb', line 19

def initialize(url)
  if marmiton_host? url
    fetch_from_marmiton url

  elsif g750_host? url
    fetch_from_g750 url

  elsif cuisineaz_host? url
    fetch_from_cuisineaz url

  else
    raise ArgumentError, 'Instantiation cancelled (Host not supported).'
  end
end

Instance Attribute Details

#cooktimeObject (readonly)

Returns the value of attribute cooktime.



10
11
12
# File 'lib/recipe_scraper.rb', line 10

def cooktime
  @cooktime
end

#imageObject (readonly)

Returns the value of attribute image.



10
11
12
# File 'lib/recipe_scraper.rb', line 10

def image
  @image
end

#ingredientsObject (readonly)

Returns the value of attribute ingredients.



10
11
12
# File 'lib/recipe_scraper.rb', line 10

def ingredients
  @ingredients
end

#nb_of_personsObject (readonly)

Returns the value of attribute nb_of_persons.



10
11
12
# File 'lib/recipe_scraper.rb', line 10

def nb_of_persons
  @nb_of_persons
end

#preptimeObject (readonly)

Returns the value of attribute preptime.



10
11
12
# File 'lib/recipe_scraper.rb', line 10

def preptime
  @preptime
end

#stepsObject (readonly)

Returns the value of attribute steps.



10
11
12
# File 'lib/recipe_scraper.rb', line 10

def steps
  @steps
end

#titleObject (readonly)

Returns the value of attribute title.



10
11
12
# File 'lib/recipe_scraper.rb', line 10

def title
  @title
end

Instance Method Details

#to_hashHash

export object properties to hash

Returns:

  • (Hash)

    as object’s properties



37
38
39
40
41
42
43
44
# File 'lib/recipe_scraper.rb', line 37

def to_hash
  attrs = {}
  instance_variables.each do |var|
    str = var.to_s.gsub /^@/, ''
    attrs[str.to_sym] = instance_variable_get(var)
  end
  attrs
end

#to_jsonString

convert object properties to json

Returns:

  • (String)

    data formated in JSON



49
50
51
# File 'lib/recipe_scraper.rb', line 49

def to_json
  to_hash.to_json
end