Class: Profound::Input
- Inherits:
-
Object
show all
- Defined in:
- lib/profound.rb
Defined Under Namespace
Classes: EmptyQueryResult
Constant Summary
collapse
- Keywords =
[
'forest', 'jungle', 'prairie', 'fields', 'flowers' 'beach', 'city', 'skyline',
'trail', 'lake', 'sky', 'sunrise', 'dawn', 'dusk', 'scenery', 'island', 'kitten', 'horse', 'puppy'
]
- Qualifiers =
[
'beautiful', 'gorgeous', 'magnificient', 'superb', nil
]
Instance Method Summary
collapse
Constructor Details
#initialize(source, options = {}) ⇒ Input
Returns a new instance of Input.
40
41
42
43
|
# File 'lib/profound.rb', line 40
def initialize(source, options = {})
@source = source || [Qualifiers.sample, Keywords.sample].join(" ")
@options = options
end
|
Instance Method Details
#download ⇒ Object
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/profound.rb', line 49
def download
image = search
response = Net::HTTP.get_response(URI.parse(image.uri))
tmp = Tempfile.new("profound")
tmp.write(response.body)
tmp.rewind
tmp.path
end
|
#path ⇒ Object
45
46
47
|
# File 'lib/profound.rb', line 45
def path
File.exists?(@source) ? @source : download
end
|
#search ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/profound.rb', line 60
def search
query = [@source, [@options[:width], @options[:height]].compact.join("x")].join(" ")
image = Google::Search::Image.new(:query => query, :image_size => :huge, :file_type => :jpg).select{ |img|
(@options[:width].nil? || @options[:width].to_s == img.width.to_s) &&
(@options[:height].nil? || @options[:height].to_s == img.height.to_s)
}.sample
raise EmptyQueryResult, "Could not find images matching #{query}" unless image
image
end
|