
This is what I got.
<% for image in @google_images %>
<%= link_to(image_tag(image.thumbnail, :style => 'padding:3px; border:solid 1px #EEE; margin:2px; width:100px; float:left'), image.original) %>
<% end %>
<%= clear %>
<%= link_to_unless (params[:start] == 0), 'Prev', :overwrite_params => { :start => (params[:start] - 8) } %> |
<%= link_to_unless (params[:start] > 0 && @google_images.blank?), 'Next', :overwrite_params => { :start => (params[:start] + 8) } %>
def google_images
params[:start] = params[:start].to_i
@page_title = "Import image from Google"
@google_images = GoogleImage.all(params[:keywords], params[:start])
end
class GoogleImage
require 'json'
attr_accessor :thumbnail, :original, :name, :position
def initialize(params)
super()
self.name = params[:name]
self.thumbnail = params[:thumbnail]
self.original = params[:original]
self.position = params[:position]
end
def self.find (keyword, position = 0)
url = "http://ajax.googleapis.com/ajax/services/search/images?rsz=large&start=#{position}&v=1.0&q=#{CGI.escape(keyword)}"
json_results = open(url) {|f| f.read };
results = JSON.parse(json_results)
image_array = results['responseData']['results']
image = image_array[0] if image_array
google_image = self.new(:thumbnail => image['tbUrl'], :original => image['unescapedUrl'], :position => position, :name => keyword.titleize)
end
def self.all (keyword, position = 0)
return [] if (keyword.nil? || keyword.strip.blank?)
url = "http://ajax.googleapis.com/ajax/services/search/images?rsz=large&start=#{position}&v=1.0&q=#{CGI.escape(keyword)}"
json_results = open(url) {|f| f.read };
results = JSON.parse(json_results)
begin
image_array = results['responseData']['results']
google_images = image_array.map{|image| self.new(:thumbnail => image['tbUrl'], :original => image['unescapedUrl'], :name => keyword.titleize) }
google_images.each_index{|i| google_images[i].position = position + i }
rescue
[]
end
end
end
<%= link_to image_tag('icon/facebook.gif'), {:controller => 'articles', :action => 'facebook', :id => @article}, :popup => ['new_window','width=600,height=400'], :rel => 'nofollow' %>
def facebook_post
@article = Article.find(params[:id])
url = CGI.escape(url_for(@article, :only_path => false)
title = CGI.escape(@article.title)
redirect_to "http://www.facebook.com/sharer.php?u=#{url}&t=#{title}"
end