Google Calendar Ruby API - ruby

I'm working on an app that needs to update a google calendar.
By following the Google Calendar Developer Guide at:
http://code.google.com/apis/calendar/v3/using.html#creating_events
event = {
'summary' => 'Appointment',
'location' => 'Somewhere',
'start' => {
'dateTime' => '2011-06-03T10:00:00.000-07:00'
},
'end' => {
'dateTime' => '2011-06-03T10:25:00.000-07:00'
}
}
result = client.execute(:api_method => service.events.insert,
:parameters => {'calendarId' => 'primary'},
:body => JSON.dump(event),
:headers => {'Content-Type' => 'application/json'})
I get the following error:
TypeError (Expected body to respond to :each.):
I have tried to do event.to_json as well.

Just a couple of suggestions from the top of my head which may help. Please try them and inform us if these help.
Wrap :body into array:
result = client.execute(:api_method => service.events.insert,
:parameters => {'calendarId' => 'primary'},
:body => [JSON.dump(event)],
:headers => {'Content-Type' => 'application/json'})
Or use:body_object instead of body, and don't jsonify the event hash:
result = client.execute(:api_method => service.events.insert,
:parameters => {'calendarId' => 'primary'},
:body_object => event,
:headers => {'Content-Type' => 'application/json'})
Take a look at http://code.google.com/p/google-api-ruby-client/source/browse/lib/google/api_client/reference.rb, and you'll understand how I came up with these suggestions.

Related

Ruby Curl::Multi.http 302 redirect problems

I seem to be having a problem with 302 redirects using curb (Ruby's curl programs)
Here's the code snippet that is ** NOT working** (it's NOT doing the 302 redirect)
easy_options = {:follow_location => true, :enable_cookies => true, :useragent => 'curb', :cookiefile => 'cookie.txt'}
multi_options = {:pipeline => true}
url_fields = [
{
:url => 'https://x.y.z/webapps/login/',
:method => :post,
:follow_location => true,
:enable_cookies => true,
:useragent => 'curb',
:post_fields => {
'user_id' => 'xxxx',
'password' => 'xxxx',
'action' => 'login',
'encoded_pw' => Base64.strict_encode64('xxxx')},
}
]
Curl::Multi.http(url_fields,{:pipeline => true}) do |easy, code, method|
puts easy.header_str
end
Here's the code snippet that appears to be working (it's doing the 302 redirect)
easy_options = {:follow_location => true, :enable_cookies => true, :useragent => 'curb', :cookiefile => 'cookie.txt'}
multi_options = {:pipeline => true}
url_fields = [
{ :url => 'https://x.y.z/webapps/login/',
:post_fields => {
'user_id' => 'xxxx',
'password' => 'yyyy',
'action' => 'login',
'encoded_pw' => Base64.strict_encode64('yyyy')}}
]
Curl::Multi.post(url_fields, easy_options, multi_options) do|easy|
# do something interesting with the easy response
puts easy.last_effective_url
end
Question: Why is the first block not doing the 302 redirect as expected? :follow_location is set to true?
Thanks in advance!
Let me know if you need more information

Upload Captions YouTube Data API ruby

I am trying to upload captions to YouTube using the Data API. However I can't find in the reference or in the forum any example in Ruby. In specific how to send the actual caption file (xml).
body = {
:snippet => {
:videoId => videoId,
:language => "English",
:name => "English"
}
}
captions_insert_response = client.execute(
:api_method => youtube.captions.insert,
:parameters => {
:part => body.keys.join(',')
},
:body_object => body
)
where and how do I add the caption file? I tried doing it like uploading a video, but it didn't seem to work. This line was added after ":body_object"
:media => Google::APIClient::UploadIO.new(captions_file, 'text/xml')
Thanks
I solved the issue changing the language in the snippet to "en".
This is the complete request if someone needs it.
body = {
:snippet => {
:videoId => videoId,
:language => "en",
:name => "English"
}
}
captions_insert_response = client.execute(
:api_method => youtube.captions.insert,
:body_object => body,
:media => Google::APIClient::UploadIO.new(captions_file, 'text/xml'),
:parameters => {
'uploadType' => 'multipart',
:part => body.keys.join(',')
}
)

Can't get value from nested pair in Omniauth hash.

I'll try to keep this simple, my previous wording was maybe a bit too verbose:
Here is the example Omniauth hash: https://github.com/mkdynamic/omniauth-facebook
I can access and save some values from this but not others. The field is writable, so I know its just my syntax (beginner, sorry!)
{
:provider => 'facebook',
:uid => '1234567',
:info => {
:nickname => 'jbloggs',
:email => 'joe#bloggs.com',
:name => 'Joe Bloggs',
:first_name => 'Joe',
:last_name => 'Bloggs',
:image => 'http://graph.facebook.com/1234567/picture?type=square',
:urls => { :Facebook => 'http://www.facebook.com/jbloggs' },
:location => 'Palo Alto, California',
:verified => true
},
:credentials => {
:token => 'ABCDEF...', # OAuth 2.0 access_token, which you may wish to store
:expires_at => 1321747205, # when the access token expires (it always will)
:expires => true # this will always be true
},
:extra => {
:raw_info => {
:id => '1234567',
:name => 'Joe Bloggs',
:first_name => 'Joe',
:last_name => 'Bloggs',
:link => 'http://www.facebook.com/jbloggs',
:username => 'jbloggs',
:location => { :id => '123456789', :name => 'Palo Alto, California' },
:gender => 'male',
:email => 'joe#bloggs.com',
:timezone => -8,
:locale => 'en_US',
:verified => true,
:updated_time => '2011-11-11T06:21:03+0000'
}
}
}
I can do this to get gender and save it.
location:auth.extra.raw_info["gender"]
Obviously though I dont want to save gender to location. I want to get "Palo Alto" and save it. But this doesn't work.
location.auth.extra.raw_info["location"]["name"]
What am I doing wrong? When I try it in console, I'm able to get the value.
try this
location.auth.extra.raw_info.location.name
or this
location.auth.extra.raw_info[:location][:name]
Yeah, what you suggested was what I was trying...and it turns out we were right but FB had changed how that hash was set up so it wasn't working. Lesson learned: subscribe FB's notifications next time :)

Adding address to mailchimp using Gibbon gem

Im using the gibbon 0.4.6 with ruby 1.9.3p392, and I tried to add the address of my contacts but I couldn't find the correct format of the parameters.
respuesta = gb.listSubscribe({
:id => lista_id, :email_address => email,
:merge_vars => {'FNAME' => nombre, 'LNAME' => apellido,
'MMERGE3' => ['addr1' => 'aqui', 'addr2' => 'Alla', 'city' => 'Mexico DF',
'zip' => '06700', 'country' => 'MX']
}
})
Update
As Amro suggested, now Im using Gibbon 1.0, but I have the same problem:
I used this
respuesta = gb.lists.subscribe({
:id => lista_id, :email => {:email => email},
:merge_vars => {'FNAME' => nombre, 'LNAME' => apellido,
'MMERGE3' => {'addr1' => 'aqui', 'addr2' => 'Alla', 'city' => 'Mexico DF', 'zip' => '06700', 'country' => 'MX'},
'MMERGE4' => 'Mi nota '
}
})
But the address(MMERGE3) wasn't registered at MailChimp.
Any idea is welcome.
Your current code looks reasonable to me. Have you tried also passing "update_existing" with a value of true? If that address is already subscribed then it won't work otherwise since "update_existing" defaults to false.
Old Answer for API 1.3
I'm Gibbon's maintainer. In this case, MailChimp's docs say the type is an "array," but they mean an associative array (i.e. a Ruby hash). So try something like this:
respuesta = gb.listSubscribe({
:id => lista_id, :email_address => email,
:merge_vars => {'FNAME' => nombre, 'LNAME' => apellido,
'MMERGE3' => {'addr1' => 'aqui', 'addr2' => 'Alla', 'city' => 'Mexico DF',
'zip' => '06700', 'country' => 'MX'}
}
})
Also, API 1.3 has been deprecated. I suggest upgrading to Gibbon 1.0, which hits MailChimp API 2.0. The syntax is a little different so be sure to check out the 2.0 docs and Gibbon's updated README here.

How to not to pass argument if empty?

I upgraded ruby to 1.9.3 from 1.8.x Not sure if pony gem was upgraded during that process too but the point is that I was using this code to send out an emails
Pony.mail(
:to => to,
:from => from,
:subject => subject,
:body => Nokogiri::HTML(body_with_footer).text,
:html_body => body_with_footer, #.gsub("\n","<BR>"),
:attachments => attachment_to_send,
:via => :smtp,
:via_options => {
:address => $smtp,
:port => $smtp_port,
:enable_starttls_auto => false
}
)
attachment_to_send should be a hash of files to be attached. When the hash was empty no attachment was send. Now I got a pony error complaining about the hash being "".
So I introduced a if condition attachment_to_send=="" so I call pony with or without the attachment part.
Is there any way to manage that? So I have only one code where I call pony?
handled with ternary operator attachment_to_send.empty? ? nil : attachment_to_send
details = {
:to => to,
:from => from,
:subject => subject,
:body => Nokogiri::HTML(body_with_footer).text,
:html_body => body_with_footer, #.gsub("\n","<BR>"),
:attachments => attachment_to_send.empty? ? nil : attachment_to_send ,
:via => :smtp,
:via_options => {
:address => $smtp,
:port => $smtp_port,
:enable_starttls_auto => false
}
Pony.mail(details)
prepare your attachment array by checking empty condition following way,
tmp_hash = {:to => to,
:from => from,
:subject => subject,
:body => Nokogiri::HTML(body_with_footer).text,
:html_body => body_with_footer, #.gsub("\n","<BR>"),
:via => :smtp,
:via_options => {
:address => $smtp,
:port => $smtp_port,
:enable_starttls_auto => false
}
}
and
tmp_hash[:attachments] => attachment_to_send
tmp_hash[:attachments] => nil if attachment_to_send.empty?
or
directly,
tmp_hash[:attachments] => attachment_to_send if not attachment_to_send.empty?
and then
Pony.mail( tmp_hash)
should work

Resources