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'
RESULTS_REGEXP =
/setResults\((\[\[.+\]\])\);/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ GoogleImage

Returns a new instance of GoogleImage.



8
9
10
# File 'lib/svn_campfire_notifier/google_image.rb', line 8

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



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

def project
  @project
end

Class Method Details

.feeling_lucky(query) ⇒ Object



35
36
37
# File 'lib/svn_campfire_notifier/google_image.rb', line 35

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

Instance Method Details

#feeling_luckyObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/svn_campfire_notifier/google_image.rb', line 20

def feeling_lucky
  puts url
  response = REST.get(url)
  if response.found? and (location = response.headers['location'].first)
    response = REST.get(location)
  end
  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



12
13
14
# File 'lib/svn_campfire_notifier/google_image.rb', line 12

def query
  "#{project.subject}+#{project.revision}"
end

#urlObject



16
17
18
# File 'lib/svn_campfire_notifier/google_image.rb', line 16

def url
  "#{URL}&q=#{query}&imgcolor=#{project.color}&imgtype=#{project.image_type}"
end