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



43
44
45
# File 'lib/svn_campfire_notifier/google_image.rb', line 43

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

Instance Method Details

#extra_optionsObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/svn_campfire_notifier/google_image.rb', line 12

def extra_options
  {
    'isch' => '1',
    'itp'  => project.image_type,
    'ic'   => 'specific',
    'ics'  => project.color
  }.inject([]) do |serialized, (key, value)|
    serialized << "#{key}:#{value}"
  end.join(',')
end

#feeling_luckyObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/svn_campfire_notifier/google_image.rb', line 31

def feeling_lucky
  puts url
  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



23
24
25
# File 'lib/svn_campfire_notifier/google_image.rb', line 23

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

#urlObject



27
28
29
# File 'lib/svn_campfire_notifier/google_image.rb', line 27

def url
  "#{URL}&q=#{query}&tbs=#{extra_options}"
end