Class: JustTheRecipe::Searcher
- Inherits:
-
Object
- Object
- JustTheRecipe::Searcher
- Defined in:
- lib/just-the-recipe/searcher.rb
Instance Method Summary collapse
- #api_get ⇒ Object
-
#initialize(search_term) ⇒ Searcher
constructor
A new instance of Searcher.
- #scrape_recipe(url) ⇒ Object
Constructor Details
#initialize(search_term) ⇒ Searcher
Returns a new instance of Searcher.
3 4 5 |
# File 'lib/just-the-recipe/searcher.rb', line 3 def initialize(search_term) @search_term = search_term end |
Instance Method Details
#api_get ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/just-the-recipe/searcher.rb', line 9 def api_get begin base = 'https://api.edamam.com/search' q = @search_term app_id = JustTheRecipe.app_id app_key = JustTheRecipe.app_key url = "#{base}?q=#{q}&app_id=#{app_id}&app_key=#{app_key}" uri = URI.parse(url) response = Net::HTTP.get_response(uri) results = JSON.parse(response.body) hits = results["hits"] good_url = "" hits.each do |hit| url = hit["recipe"]["url"] if JustTheRecipe::Scraper.new(url).valid_url? good_url = url break end end scrape_recipe(good_url) rescue puts "\nSorry we couldn't find a valid recipe with that search term. Make sure you've added in your API credentials, or try a different search term." end end |
#scrape_recipe(url) ⇒ Object
39 40 41 |
# File 'lib/just-the-recipe/searcher.rb', line 39 def scrape_recipe(url) JustTheRecipe::Scraper.new(url).get_recipe_by_schema end |