Class: BackgroundImages::Finder
- Defined in:
- lib/background_images/finder.rb
Instance Attribute Summary collapse
-
#find_image_of ⇒ Object
Returns the value of attribute find_image_of.
-
#max_attempts ⇒ Object
Returns the value of attribute max_attempts.
-
#results ⇒ Object
Returns the value of attribute results.
Instance Method Summary collapse
- #find_many(attempt = 1) ⇒ Object
- #find_one(method = :first) ⇒ Object
-
#initialize(find_image_of = "Brad Pitt's Ghost") ⇒ Finder
constructor
A new instance of Finder.
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_of ⇒ Object
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_attempts ⇒ Object
Returns the value of attribute max_attempts.
3 4 5 |
# File 'lib/background_images/finder.rb', line 3 def max_attempts @max_attempts end |
#results ⇒ Object
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 |