Friday, November 27, 2009

utilizing Google spreadsheet API In Ruby

In this class you just need to edit the username and password and the googlespreadsheetkey... That it!!!!!!!!!!!!!!!

Finally You can able to post Your data into your google docs.



class Myspreadsheet

require 'net/http'
require 'net/https'
require 'rubygems'
require 'xmlsimple'

def posts #(uri, data, headers)
http = Net::HTTP.new('www.google.com', 443)
http.use_ssl = true
path = '/accounts/ClientLogin'
data = 'accountType=HOSTED_OR_GOOGLE&Email=username@gmail.com&Passwd=password&service=wise'

headers ={ 'Content-Type' => 'application/x-www-form-urlencoded'}
resp, data = http.post(path, data, headers)
cl_string = data[/Auth=(.*)/, 1]
headers["Authorization"] = "GoogleLogin auth=#{cl_string}"

spreadsheets_uri = 'http://spreadsheets.google.com/feeds/spreadsheets/private/full'


def get_feed(uri, headers=nil)
uri = URI.parse(uri)
Net::HTTP.start(uri.host, uri.port) do |http|
return http.get(uri.path, headers)
end
end
my_spreadsheets = get_feed(spreadsheets_uri, headers)


puts my_spreadsheets.body

doc = XmlSimple.xml_in(my_spreadsheets.body, 'KeyAttr' => 'name')
require 'pp'

puts pp doc
spreadsheet_key = doc["entry"][0]["id"][0][/full\/(.*)/, 1]
worksheet_feed_uri = "http://spreadsheets.google.com/feeds/worksheets/#{spreadsheet_key}/private/full"

worksheet_response = get_feed(worksheet_feed_uri, headers)
worksheet_data = XmlSimple.xml_in(worksheet_response.body, 'KeyAttr' => 'name')

puts pp worksheet_data

listfeed_uri = worksheet_data["entry"][0]["link"][0]["href"]

response = get_feed(listfeed_uri, headers)
listfeed_doc = XmlSimple.xml_in(response.body, 'KeyAttr' => 'name')

puts pp listfeed_doc

cellfeed_uri = worksheet_data["entry"][0]["link"][1]["href"]

response = get_feed(cellfeed_uri, headers)


# Parse into datastructure and print
cellfeed_doc = XmlSimple.xml_in(response.body, 'KeyAttr' => 'name')


puts pp cellfeed_doc
# Set up our POST url


#"http://spreadsheets.google.com/feeds/list/tcIStC1pH2D2ohWewXnPIbQ/od6/private/full"
end

def encode_query(params)

puts params.map(){ |k, v| uri_encode(k) + "=" + uri_encode(v) }.join("&")

return params.map(){ |k, v| uri_encode(k) + "=" + uri_encode(v) }.join("&")
end

def uri_encode(str)
return URI.encode(str, /#{URI::UNSAFE}|&/n)
end

def h(str)
return CGI.escapeHTML(str.to_s())
end

def http_request(method, url, data, header = {})
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = uri.scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start() do
path = uri.path + (uri.query ? "?#{uri.query}" : "")
if method == :delete
response = http.__send__(method, path, header)
else
response = http.__send__(method, path, data, header)
end
if !(response.code =~ /^2/)
raise(GoogleSpreadsheet::Error, "Response code #{response.code} for POST #{url}: " +
CGI.unescapeHTML(response.body))
end
return response.body
end
end

def post(uri, data)
@auth_token = nil
headers = Hash.new
headers["Content-Type"] = "application/atom+xml"
params = {
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => 'username',
"Passwd" => 'password',
"service" => "wise",

}
response = http_request(:post,
"https://www.google.com/accounts/ClientLogin", encode_query(params))
@auth_token = response.slice(/^Auth=(.*)$/, 1)
#headers = { 'Content-Type' => 'application/atom+xml'}

headers["Authorization"] = "GoogleLogin auth=#{@auth_token}"

uri = URI.parse(uri)
http = Net::HTTP.new(uri.host, uri.port)

puts http.post(uri.path, data, headers)
return http.post(uri.path, data, headers)
end




p = Myspreadsheet.new
p.posts
post_url = "http://spreadsheets.google.com/feeds/list/tcIStC1pH2D2ohWewXnPIbQ/od6/private/full"


new_row = '' 'ruby' '' 'http://ruby-lang.org'
p.post(post_url, new_row)


end

Drag drop images from the desktop

To drag and drop images from the desktop ,we can able to achieve it by applet

download the jar file
download


Then include this applet into yr view file





you can achive it by adding this code ... very simple

Friday, August 21, 2009

Creating cronjob in ubuntu

Type : crontab -e

Then we can mention our command
# m h dom mon dow command

*/15 * * * * /deploy/project/cronjobs/alert.sh (Path file for where we have that sh file)

This will run for every 15 minute.

Example for run the rake task:
----------------------------------------------------

In the sh file we need to write the following details,
open the alert.sh and type the following

cd /home/deploy/applicationdirectory
/usr/bin/rake alerts

here this is the script for running the alerts tasks

Upgrading rails application from old version to Lastest version.Up

For Ex: Our old rails application is rails 1.2.6 or less than recent version.

Then what we need to do is go to the application directory

Ex: gousalya@home-desktop:~/project/mormonlore$ rails .
exists
exists app/controllers
exists app/helpers
exists app/models
exists app/views/layouts
exists config/environments
exists config/initializers
create config/locales
exists db
exists doc
exists lib
exists lib/tasks
exists log
exists public/images
exists public/javascripts
exists public/stylesheets
exists script/performance
exists test/fixtures
exists test/functional
exists test/integration
create test/performance
exists test/unit
exists vendor
exists vendor/plugins
exists tmp/sessions
exists tmp/sockets
exists tmp/cache
exists tmp/pids
overwrite Rakefile? (enter "h" for help) [Ynaqdh] y
force Rakefile
overwrite README? (enter "h" for help) [Ynaqdh] y
force README
create app/controllers/application_controller.rb
overwrite app/helpers/application_helper.rb? (enter "h" for help) [Ynaqdh] n
skip app/helpers/application_helper.rb
overwrite config/database.yml? (enter "h" for help) [Ynaqdh] n
skip config/database.yml
overwrite config/routes.rb? (enter "h" for help) [Ynaqdh] n
skip config/routes.rb
create config/locales/en.yml
create config/initializers/backtrace_silencers.rb
overwrite config/initializers/inflections.rb? (enter "h" for help) [Ynaqdh] n
skip config/initializers/inflections.rb
identical config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.rb
create config/initializers/session_store.rb
overwrite config/environment.rb? (enter "h" for help) [Ynaqdh] y
force config/environment.rb
overwrite config/boot.rb? (enter "h" for help) [Ynaqdh] y
force config/boot.rb
overwrite config/environments/production.rb? (enter "h" for help) [Ynaqdh] y
force config/environments/production.rb
overwrite config/environments/development.rb? (enter "h" for help) [Ynaqdh] y
force config/environments/development.rb
overwrite config/environments/test.rb? (enter "h" for help) [Ynaqdh] y
force config/environments/test.rb
overwrite script/about? (enter "h" for help) [Ynaqdh] y
force script/about
overwrite script/console? (enter "h" for help) [Ynaqdh] y
force script/console

Now our application is migrated to recent version.

Skip Migration

To skip the Migration:

use --skip-migration

For Ex: script/generate model modelname --skip-migration