Created
July 9, 2011 20:45
-
-
Save meltzerj/1073951 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResultsController < ApplicationController | |
require 'json' | |
require 'uri' | |
require 'open-uri' | |
def create | |
@search = params[:search] | |
base_url = "https://www.googleapis.com/customsearch/v1?" | |
start = (params[:page].to_i - 1) * 10 + 1 | |
stream = open("#{base_url}key=AIzaSyAX8I1Q6cLamuWhuD1NGBE9yz4WTmz1cdo&cx=004953598739448255459:gfzhltgb8zi&q=#{@search}&start=#{start}&alt=json") | |
raise 'web service error' if (stream.status.first != '200') | |
result = JSON.parse(stream.read) | |
@site_array = Array.new | |
@uuid_hash = Hash.new | |
result['items'].each do |r| | |
# escaped_url = URI.escape(r['link'], Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).gsub("%","-") | |
@site_array << r['link'] | |
if Result.where(:link => r['link']).present? | |
else | |
job_id = ImageGenerator.create(:url => r['link']) | |
@uuid_hash[r['link']] = job_id | |
end | |
end | |
respond_to do |format| | |
format.html | |
format.js | |
end | |
end | |
def searchforward | |
redirect_to "/results/#{params[:search][:search]}/1" | |
end | |
def status | |
uuid_hash = JSON.parse(params[:uuids]) | |
status_hash = Hash.new | |
uuid_hash.each do |url, uuid| | |
status = Resque::Status.get(uuid) | |
status_hash["#{uuid}"] = Hash['url' => url, 'status' => status] | |
end | |
number = 0 | |
status_hash.each do |key, value| | |
if value['status'] == "working" | |
number += 1 | |
end | |
end | |
if number == 0 | |
status_hash['isDone'] = 'yes' | |
end | |
respond_to do |format| | |
format.html | |
format.json {render :json => status_hash.to_json} | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pollingAJAX() { | |
$.ajax({ | |
method: "POST", | |
url: "/status", | |
data: {uuids: '<%= @uuid_hash.to_json %>'}, | |
}, | |
success: function(data){ | |
var obj = $.parseJSON(data); | |
if(obj.isDone == "yes"){ | |
}else{ | |
obj.each(function(result) { | |
if(result.status == "completed"){ | |
$('a[href="#{result.url}"]').html(''); | |
} | |
}); | |
pollingAJAX(); | |
} | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
json (1.5.3) lib/json/common.rb:148:in `initialize' | |
json (1.5.3) lib/json/common.rb:148:in `new' | |
json (1.5.3) lib/json/common.rb:148:in `parse' | |
app/controllers/results_controller.rb:40:in `status' | |
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:61:in `redirect_to' | |
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument' | |
activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in `instrument' | |
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument' | |
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:59:in `redirect_to' | |
app/controllers/results_controller.rb:36:in `searchforward' | |
actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action' | |
actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action' | |
actionpack (3.0.9) lib/abstract_controller/base.rb:150:in `process_action' | |
actionpack (3.0.9) lib/action_controller/metal/rendering.rb:11:in `process_action' | |
actionpack (3.0.9) lib/abstract_controller/callbacks.rb:18:in `process_action' | |
activesupport (3.0.9) lib/active_support/callbacks.rb:436:in `_run__1818408654__process_action__931070980__callbacks' | |
activesupport (3.0.9) lib/active_support/callbacks.rb:410:in `send' | |
activesupport (3.0.9) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks' | |
activesupport (3.0.9) lib/active_support/callbacks.rb:94:in `send' | |
activesupport (3.0.9) lib/active_support/callbacks.rb:94:in `run_callbacks' | |
actionpack (3.0.9) lib/abstract_controller/callbacks.rb:17:in `process_action' | |
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:30:in `process_action' | |
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument' | |
activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in `instrument' | |
activesupport (3.0.9) lib/active_support/notifications.rb:52:in `instrument' | |
actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:29:in `process_action' | |
actionpack (3.0.9) lib/action_controller/metal/rescue.rb:17:in `process_action' | |
actionpack (3.0.9) lib/abstract_controller/base.rb:119:in `process' | |
actionpack (3.0.9) lib/abstract_controller/rendering.rb:41:in `process' | |
actionpack (3.0.9) lib/action_controller/metal.rb:138:in `dispatch' | |
actionpack (3.0.9) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' | |
actionpack (3.0.9) lib/action_controller/metal.rb:178:in `action' | |
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in `call' | |
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in `dispatch' | |
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:27:in `call' | |
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `call' | |
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `recognize' | |
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:82:in `optimized_each' | |
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize' | |
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call' | |
actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:493:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/head.rb:14:in `call' | |
rack (1.2.3) lib/rack/methodoverride.rb:24:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/params_parser.rb:21:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/flash.rb:182:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/cookies.rb:302:in `call' | |
activerecord (3.0.9) lib/active_record/query_cache.rb:32:in `call' | |
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache' | |
activerecord (3.0.9) lib/active_record/query_cache.rb:12:in `cache' | |
activerecord (3.0.9) lib/active_record/query_cache.rb:31:in `call' | |
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:in `call' | |
activesupport (3.0.9) lib/active_support/callbacks.rb:416:in `_run_call_callbacks' | |
actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:44:in `call' | |
rack (1.2.3) lib/rack/sendfile.rb:107:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call' | |
railties (3.0.9) lib/rails/rack/logger.rb:13:in `call' | |
rack (1.2.3) lib/rack/runtime.rb:17:in `call' | |
activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call' | |
rack (1.2.3) lib/rack/lock.rb:11:in `call' | |
rack (1.2.3) lib/rack/lock.rb:11:in `synchronize' | |
rack (1.2.3) lib/rack/lock.rb:11:in `call' | |
actionpack (3.0.9) lib/action_dispatch/middleware/static.rb:30:in `call' | |
railties (3.0.9) lib/rails/application.rb:168:in `call' | |
railties (3.0.9) lib/rails/application.rb:77:in `send' | |
railties (3.0.9) lib/rails/application.rb:77:in `method_missing' | |
railties (3.0.9) lib/rails/rack/log_tailer.rb:14:in `call' | |
rack (1.2.3) lib/rack/content_length.rb:13:in `call' | |
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start' | |
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start' | |
rack (1.2.3) lib/rack/handler/webrick.rb:13:in `run' | |
rack (1.2.3) lib/rack/server.rb:217:in `start' | |
railties (3.0.9) lib/rails/commands/server.rb:65:in `start' | |
railties (3.0.9) lib/rails/commands.rb:30 | |
railties (3.0.9) lib/rails/commands.rb:27:in `tap' | |
railties (3.0.9) lib/rails/commands.rb:27 | |
script/rails:6:in `require' | |
script/rails:6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment