Class: SVNCampfireNotifier::GoogleImage

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

Constant Summary collapse

URL =
'http://images.google.com/images?safe=active&q=%s'
RESULTS_REGEXP =
/setResults\((\[\[.+\]\])\);/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ GoogleImage

Returns a new instance of GoogleImage.



6
7
8
# File 'lib/svn_campfire_notifier/google_image.rb', line 6

def initialize(query)
  @query = query
end

Class Method Details

.feeling_lucky(query) ⇒ Object



29
30
31
# File 'lib/svn_campfire_notifier/google_image.rb', line 29

def self.feeling_lucky(query)
  new(query).feeling_lucky
end

Instance Method Details

#feeling_luckyObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/svn_campfire_notifier/google_image.rb', line 18

def feeling_lucky
  response = REST.get(url)
  if response.ok? and match = RESULTS_REGEXP.match(response.body)
    data = JSON.parse(match[1]).first
    image_url  = data[3]
    token      = data[2]
    image_host = data[14]
    "#{image_host}?q=tbn:#{token}#{image_url}"
  end
end

#queryObject



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

def query
  "kitty+#{@query}"
end

#urlObject



14
15
16
# File 'lib/svn_campfire_notifier/google_image.rb', line 14

def url
  URL % query
end