Class: BackgroundImages::Finder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(find_image_of = "Brad Pitt's Ghost") ⇒ Finder

Returns a new instance of Finder.



5
6
7
8
# File 'lib/background_images/finder.rb', line 5

def initialize(find_image_of = "Brad Pitt's Ghost")
  @find_image_of             = find_image_of
  @max_attempts              = 3
end

Instance Attribute Details

#find_image_ofObject

Returns the value of attribute find_image_of.



3
4
5
# File 'lib/background_images/finder.rb', line 3

def find_image_of
  @find_image_of
end

#max_attemptsObject

Returns the value of attribute max_attempts.



3
4
5
# File 'lib/background_images/finder.rb', line 3

def max_attempts
  @max_attempts
end

#resultsObject

Returns the value of attribute results.



3
4
5
# File 'lib/background_images/finder.rb', line 3

def results
  @results
end

Instance Method Details

#find_many(attempt = 1) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/background_images/finder.rb', line 10

def find_many(attempt=1)
  return results unless results.nil?

   if attempt > @max_attempts
     raise "Unable to get response from Google Image search"
   end

   response = JSON.parse(find_command)

   return find_many(attempt+1) unless valid_image_search_response?(response)

   results = response["responseData"]["results"]

   return results
end

#find_one(method = :first) ⇒ Object



26
27
28
29
30
# File 'lib/background_images/finder.rb', line 26

def find_one(method=:first)
  result = find_many.send(method)
  return find_one if result.nil?
  return result
end