How to integrate ConstantContact in ruby-on-rails with an sample code or example

Occasional Participant

How to integrate ConstantContact in ruby-on-rails with an sample code or example

 

Hey this Mohan

 

Please help me, how to integrate constantcontact in ruby-on-rail. I have tried but I am getting the 400 error and I have goggled it but I have not find any solution for it.

 

And how to code the XML file to post the details to create a contact in the contact list and how to the get response for the contact is created or not in constant contact 

 

Please can any one help me 

 

Thank you in advance 

 

 

3 REPLIES 3
Moderator

Without the details of your XML that you're sending, it is impossible to help you with the 400 error.  400 errors indicate that the content of your request has problems in it, could be values of XML nodes or the structure of your XML in general.

 

There is an open source Ruby Gem that handles all of the XML processing for you so you don't have to code all of that from scratch.  I would highly recommend using that instead of doing it from hand.  You can find the gem here: https://rubygems.org/gems/constant_contact

 

If you want to continue to code your own solution from scratch, you'll need to post your raw XML for the request here (please hide personal information) and provide us with the URL and request type for your request returning the 400 error.

 

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

Here is my form this value are go to  method in my controller  

 

<div id="full_width">
<div class="cream_bkg">
<h1>Suggest A Business</h1>
<div class="full_width_content center_fix">
<h2>Do you have a favourite business that you would like to see on Home Shabang? Tell us and we will invite them!!</h2>
<h3><%= flash[:error] %></h3>
<!--form method="post" action=""-->
<%= form_for :coupon,:url=> {:controller=>'coupons',:action=>'comments'},:html=>{:id=> 'cc_coupon_form'} do |f| %>
<table class="form_table suggest_business" cellpadding="0" cellspacing="0">
<tr>
<th></th>
<th></th>
</tr>
<tr>
<td><label for ="email"> Email:</lable></td>
<td>
<span class="blue_field">
<!--input id="email" class="text_field" type="text" name="" /-->
<%= f.text_field :email, :id =>'email',:class=>'text_field' %></span>
<span id= "email_address" class="error_msg" style="display:none;">Please enter the Email Address.</span>
</td>
</tr>
<tr>
<td><label for="company_name">Company Name:</label></td>
<td>
<span class="blue_field">
<!--input id="company_name" class="text_field" type="text" name="" /-->
<%= f.text_field :company_name,:id =>'company',:class=>'text_field' %></span>
<span id="name" class="error_msg" style ="display:none;"> Please enter the company name.</span>
</td>
</tr>
<tr>
<td><label for="state">State:</label></td>
<td>
<span class="blue_field">
<!--input id="state" class="text_field" type="text" name="" /-->
<%= f.text_field :state, :id =>'state',:class=>'text_field' %></span>
<span id= "business_state" class="error_msg" style="display:none;">Please enter the state.</span>
</td>
</tr>
<tr>
<td><label for="city">City:</label></td>
<td><span class="blue_field">
<%= f.text_field :city, :class=> 'text_field' %>
<!--input id="city" class="text_field" type="text" name=""/></span--></td>
</tr>
<tr>
<td><label for="comment">Comment:</label></td>
<td><span class="blue_field">
<!--textarea id="comment" class="text_area" rows="" cols="" name=""></textarea-->
<%= f.text_area :comment,:class=> 'text_area' %>
</span>
</td>
</tr>
<tr>
<td></td>
<td><!--input class="submit_btn" type="button" value="Send" /-->
<%= f.submit "Send", :class=> "submit_btn", :id=>'send',
:type=> 'button' %>
</td>
</tr>
</table>
<% end %>
</div>
</div>
</div>

 

Here is method in my controller

 

def comments
unless params[:coupon].blank?
unless params[:coupon][:email].blank?
unless params[:coupon][:company_name].blank?
create_contact_on_constant_contact(params[:coupon])
else
flash[:error] = "Comapny Name is a mandatory field"
redirect_to static_suggest_business_path
end
else
flash[:error] = "Email is a mandatory field"
redirect_to static_suggest_business_path
end
else
flash[:error] = "Email is a mandatory field"
redirect_to static_suggest_business_path
end
end

 

 


def create_contact_on_constant_contact(coupon)
logger.debug "\n\n Coupon: #{coupon.inspect}\n\n"
unless coupon.blank?
#require 'httparty'
require 'net/http'
require 'uri'
xml_string_response = xml_string(coupon)
logger.debug "\n\n XML string: #{xml_string_response.inspect}\n\n"
options = {
:headers => {
"Content-Type" => "application/atom+xml",
"HTTP_AUTHORIZATION" =>
Base64.encode64("#{'chandrareddy' + '%' + 'username' + ':' + 'password' }")
},
:body => xml_string_response,
}
#url = URI.parse("https://api.constantcontact.com/ws/customers/#{'chandrareddy'}/contacts")
response = Net::HTTP.post_form(URI.parse("https://api.constantcontact.com/ws/customers/#{'chandrareddy'}/contacts"), options)
logger.debug "\n\n Response1: #{response.inspect}\n\n"
logger.debug "\n\n Response2: #{response.body.inspect}\n\n"
end
end

 

Here is the xml code 


def xml_string( coupon )
opt_in='ACTION_BY_CUSTOMER'
data = {:email_address => "#{coupon[:email]}", :company_name => "#{coupon[:company_name]}", :contact_lists => ''}
xml = <<EOF
<entry xmlns="http://www.w3.org/2005/Atom"><title type="text"></title><updated>#{Time.now.strftime("%Y-%m-%dT%H:%M:%S.000Z")}</updated><author></author><id>data:,none</id><summary type="text">Contact</summary><content type="application/vnd.ctct+xml"><Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
EOF

data.each do |key, val|
node = camelize(key.to_s)

if key == :contact_lists
xml << %Q(<ContactLists>)
#val.each do |list_id|
# xml<< %Q( <ContactList id="#{ContactList.url_for( list_id )}" />\n)
xml << %Q(<ContactList id="http://api.constantcontact.com/ws/customers/#{'chandrareddy'}/lists/1" />)
#end
xml << %Q(</ContactLists>)
else
xml << %Q(<#{node}>#{val}</#{node}>)
end
end

xml += <<EOF
<OptInSource>#{opt_in}</OptInSource></Contact></content></entry>
EOF
xml
end

def camelize( string )
string.split( /[^a-z0-9]/i ).map{ |w| w.capitalize }.join
end

 

First I have use the httparty gem to post  request  it showing me the 400 error and tried with net/http libery in ruby it showing me the error end of file reached 

 

I think totally the xml formate is wrong. can you please helpe me how code the right xml formate or can you please give me the xml code which is already used  

 

And in the {CC_Username} need to give in ' ' and in url  list/1 we need to give the join my list name where?

 

 

Please  help me thank you in Advance

 

 

 

Hey Mohan,

 

I sent you an email yesterday in regards to your error.  Your code would not work on my server.  If you could please respond to that email with the rails version, the application as a whole attached or the XML outputted as a string so we can delimit the error from its output.

Ryan Davis
Quality Engineer
Developer Portal

View API documentation, code samples, get your API key.

Visit Page