How to update all fields in MailChimp API batch subscribe using Ruby and Gibbon - ruby

I am using Ruby 1.9.3 without Rails and version 1.0.4 of the Gibbon gem.
I have referrals populated with my list and can send the following to MailChimp with Gibbon. However, only the email address and email type fields are populated in the list in MailChimp. What am I doing wrong that is prohibiting all the merge fields from being imported via API?
Here is the batch and map of the list.
referrals.each_slice(3) do |batch|
begin
prepared_batch = batch.map do |referral|
{
:EMAIL => {:email => referral['client_email']},
:EMAIL_TYPE => 'html',
:MMERGE6 => referral['field_1'],
:MMERGE7 => referral['field_2'],
:MMERGE8 => referral['field_3'],
:MMERGE9 => referral['field_4'],
:MMERGE11 => referral['field_5'],
:MMERGE12 => referral['field_6'],
:MMERGE13 => referral['field_7'],
:MMERGE14 => referral['field_8'],
:MMERGE15 => referral['field_9'],
:FNAME => referral['client_first_name']
}
end
#log.info("prepared_batch : #{prepared_batch}")
result = #gibbon.lists.batch_subscribe(
:id => #mc_list_id,
:batch => prepared_batch,
:double_optin => false,
:update_existing => true
)
#log.info("#{result}")
rescue Exception => e
#log.warn("Unable to load batch into mailchimp because #{e.message}")
end
end
The above executes successfully. However, only the email address and email type are populated but most of the fields should be populated.
Here is my log output for one of the prepared_batches. I replaced the real values with Value. I used my own email for testing.
I, [2013-11-11T09:01:14.778907 #70827] INFO -- : prepared_batch : [{:EMAIL=>
{:email=>"jason+6#marketingscience.co"}, :EMAIL_TYPE=>"html", :MMERGE6=>"Value",
:MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13 0:00",
:MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>"10/18/13 19:09", :MMERGE15=>"Value",
:FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+7#marketingscience.co"}, :EMAIL_TYPE=>"html",
:MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/6/13
0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
:FNAME=>"Value"}, {:EMAIL=>{:email=>"jason+8#marketingscience.co"}, :EMAIL_TYPE=>"html",
:MMERGE6=>"Value", :MMERGE7=>"Value", :MMERGE8=>nil, :MMERGE9=>nil, :MMERGE11=>"8/7/13
0:00", :MMERGE12=>"Value", :MMERGE13=>nil, :MMERGE14=>nil, :MMERGE15=>"Value",
:FNAME=>"Value"}]
Here is the log output of result from the MailChimp call.
I, [2013-11-11T09:01:14.778691 #70827] INFO -- : {"add_count"=>3, "adds"=>
[{"email"=>"jason+3#marketingscience.co", "euid"=>"ab512177b4", "leid"=>"54637465"},
{"email"=>"jason+4#marketingscience.co", "euid"=>"eeb8388524", "leid"=>"54637469"},
{"email"=>"jason+5#marketingscience.co", "euid"=>"7dbc84cb75", "leid"=>"54637473"}],
"update_count"=>0, "updates"=>[], "error_count"=>0, "errors"=>[]}
Any advice on how to get all the fields to update in MailChimp is appreciated. Thanks.

Turns out the documentation for using the Gibbon gem to batch subscribe is not correct. You need to add the :merge_vars struct to contain the fields other than email and email type. My final code looks like the following. I'm also going to update this code in its entirety at: https://gist.github.com/analyticsPierce/7434085.
referrals.each_slice(3) do |batch|
begin
prepared_batch = batch.map do |referral|
{
:EMAIL => {:email => referral['email']},
:EMAIL_TYPE => 'html',
:merge_vars => {
:MMERGE6 => referral['field_1'],
:MMERGE7 => referral['field_2'],
:MMERGE8 => referral['field_3'],
:MMERGE9 => referral['field_4'],
:MMERGE11 => referral['field_5'],
:MMERGE12 => referral['field_6'],
:MMERGE13 => referral['field_7'],
:MMERGE14 => referral['field_8'],
:MMERGE15 => referral['field_9'],
:FNAME => referral['first_name']
}
}
end
#log.info("prepared_batch : #{prepared_batch}")
result = #gibbon.lists.batch_subscribe(
:id => #mc_list_id,
:batch => prepared_batch,
:double_optin => false,
:update_existing => true
)
#log.info("#{result}")
rescue Exception => e
#log.warn("Unable to load batch into mailchimp because #{e.message}")
end
end

Related

savon gem example_body not coming properly

I have a wsdl url to give request and get response this is my code(using savon gem for this)
client = Savon.new('http://services.chromedata.com/Description/7a?wsdl')
service = :Description7a
port = :Description7aPort
operation = :getDivisions
division = client.operation(service, port, operation)
I am able to print example_body like
division.example_body
=> {:DivisionsRequest=>{:accountInfo=>{:_number=>"string", :_secret=>"string", :_country=>"string", :_language=>"string", :_behalfOf=>"string"}, :_modelYear=>"int"}}
and i'm able to set values like
division.body = {.........}
other operation such like
operationlist = client.operations(service, port)
=> ["getVersionInfo", "getModelYears", "getDivisions", "getSubdivisions", "getModels", "getStyles", "describeVehicle", "getCategoryDefinitions", "getTechnicalSpecificationDefinitions"]
I used describe vehicle
desc_veh = client.operation(service, port, "describeVehicle")
whose example_body is like
desc_veh.example_body
=> {:VehicleDescriptionRequest=>{}}
so unable to set values for desc_veh.body and the use the .call function
I don know whether it is a savon gem problem or the wsdl url problem
Your code could look like this:
gem "savon", "~> 2.0"
require "savon"
client = Savon.client(
:wsdl => 'http://services.chromedata.com/Description/7a?wsdl',
:convert_request_keys_to => :camelcase,
:log => true,
:log_level => :debug,
:pretty_print_xml => true
)
res = client.call(:get_divisions,
message: { :param1 => 'value1', :param2 => 'value2' }
)
print res.to_hash
The parameters are simply a hash in key/value pairs.

Image not loading after updating a Google document

I have a problem trying to update a Google document containing an image. In the first revision, the image will load as expected. But after updating it with the same HTML code I keep getting a spinner instead of the image.
I am using the Ruby gem created by Google (https://github.com/google/google-api-ruby-client).
Here is my test code:
# Setting up the client instance
require "google/api_client"
require "tempfile"
client = Google::APIClient.new
client.authorization.client_id = "<CLIENTID>"
client.authorization.client_secret = "<CLIENTSECRET>"
client.authorization.redirect_uri = "<REDIRECTURI>"
client.authorization.scope = "https://www.googleapis.com/auth/drive"
client.authorization.access_token = "<ACCESSTOKEN>"
client.authorization.refresh_token = "<REFRESHTOKEN>"
drive = client.discovered_api("drive", "v2")
# Creating the document (IMAGE DISPLAYED CORRECTLY)
file = drive.files.insert.request_schema.new({"title" => "Test document", "mimeType" => "text/html"})
temp = Tempfile.new "temp.html"
temp.write "<h1>Testing!</h1><p>Lorem ipsum.</p><img width='400px' src='http://www.digitaleconomics.nl/wp-content/uploads/2013/04/see-how-your-google-results-measure-up-with-google-grader-video-6b8bbb4b41.jpg'>"
temp.rewind
media = Google::APIClient::UploadIO.new(temp, "text/html")
result = client.execute(:api_method => drive.files.insert, :body_object => file, :media => media, :parameters => {"uploadType" => "multipart", "convert" => true})
temp.close
# Updating the document (GETTING SPINNER INSTEAD OF IMAGE)
file = client.execute(:api_method => drive.files.get, :parameters => {"fileId" => result.data.to_hash["id"]}).data
file.title = "Updated test document"
temp = Tempfile.new "temp.html"
temp.write "<h1>Testing!</h1><p>Lorem ipsum.</p><img width='400px' src='http://www.digitaleconomics.nl/wp-content/uploads/2013/04/see-how-your-google-results-measure-up-with-google-grader-video-6b8bbb4b41.jpg'>"
temp.rewind
media = Google::APIClient::UploadIO.new(temp, "text/html")
result = client.execute(:api_method => drive.files.update, :body_object => file, :media => media, :parameters => {"uploadType" => "multipart", "convert" => true, "fileId" => result.data.to_hash["id"], "newRevision" => false})
temp.close
Also, setting newRevision to false does not prevent a new revision from being created.
Can anyone help me out?

Does OpenStruct has support for nesting ? If Yes, How can I construct XML using Nokogiri? If not, Can this be done using Nokogiri and JSon?

The Earlier solution provided by #Jamie seems to be working fine for only few scenario's. The solution is
require 'nokogiri'
require 'ostruct'
xml = <<-'XML'
<Catalog>
<Interface></Interface>
<Dialog></Dialog>
<Manifest></Manifest>
</Catalog>
XML
collection = [
OpenStruct.new(book: '1984', pen: 'George Orwell'),
OpenStruct.new(book: 'Thinking, Fash and Slow', pen: 'Daniel Kahneman')
]
doc = Nokogiri::XML(xml)
catalog = doc.root
node_set = Nokogiri::XML::NodeSet.new(doc)
collection.each do |object|
book = Nokogiri::XML::Node.new('Book', doc)
book_author = Nokogiri::XML::Node.new('Book_Author', doc)
book.content = object.book
book_author.content = object.pen
node_set << book
node_set << book_author
end
catalog.first_element_child.before(node_set)
puts doc.to_xml
While I have the below XML to be constructed using Nokogiri. I just wanted to know, if this can't be done using OpenStruct. Could this be done using Json? If Yes, Can you please suggest me how to construct this using Nokogiri and Json.
Below is this XML, I want to construct...
<CatalogOrder>
<CatalogStoreNumber>657758</CatalogStoreNumber>
<CatalogStoreId>CTH6536</CatalogStoreId>
<CatalogStoreLocation>UnitedStates</CatalogStoreLocation>
<CatalogOwnerId>TYCT11190</CatalogOwnerId>
<CatalogOwner>McGrawHill Pub.</CatalogOwner>
<CatalogList>
<CatalogProductInfo>
<ProductType>Book</ProductType>
<ProductName>The Client</ProductName>
<ProductId>CRSUS113246A</ProductId>
<ProductCategory>Crime And Suspense</ProductCategory>
<ProductSubCategory>Vintage Books</ProductSubCategory>
<ProductPrice>45.50 USD</ProductPrice>
<ProductAuthor>
<AuthorFirstName>John Grisham</AuthorFirstName>
<AuthorMiddleName>Willbur</AuthorMiddleName>
<AuthorContact>19876648981</AuthorContact>
</ProductAuthor>
</CatalogProductInfo>
<CatalogProductInfo>
<ProductType>Pen</ProductType>
<ProductName>Reynolds</ProductName>
<ProductId>PRREY546647</ProductId>
<ProductCategory>Misc. Stationary</ProductCategory>
<ProductSubCategory>Stationary Items</ProductSubCategory>
<ProductPrice>3.00 USD</ProductPrice>
<ProductAuthor>
<AuthorFirstName>Ryan Reynolds</AuthorFirstName>
<AuthorMiddleName>William</AuthorMiddleName>
<AuthorContact>16577589898</AuthorContact>
</ProductAuthor>
</CatalogProductInfo>
<CatalogListType>ProductCollection</CatalogListType>
<CatalogListSource>Web</CatalogListSource>
</CatalogList>
<CatalogVerificationLog>
<CatalogVerificationStatus>Verified</CatalogVerificationStatus>
<CatalogVerificationDateTime>2012-03-12T13:00:15+5:30</CatalogVerificationDateTime>
<CatalogVerificationId>64774A</CatalogVerificationId>
<CatalogVerificationRequestedBy>User_121</CatalogVerificationRequestedBy>
</CatalogVerificationLog>
</CatalogOrder>
I have data that is organized in OpenStruct format below...
require 'ostruct'
require 'nokogiri'
collection = [
OpenStruct.new(:catalogStoreNumber => '657758',
:catalogStoreId => 'CTH6536',
:catalogStoreLocation => 'UnitedStates',
:catalogOwnerId => 'TYCT11190',
:catalogOwner => 'McGrawHill Pub.',
:catalogList => OpenStruct.new(
:catalogProductInfo => OpenStruct.new(
:productType => 'Book',
:productName => 'The Client',
:productId => 'CRSUS113246A',
:productCategory => 'Crime And Suspense',
:productSubcategory => 'Vintage Books',
:productPrice => '45.50 USD',
:productAuthor => OpenStruct.new(
:authorFirstName =>'John Grisham',
:authorMiddleName=> 'Willburt',
:authorContact => '19876648981')),
:catalogProductInfo => OpenStruct.new(
:productType => 'Pen',
:productName => 'Reynolds',
:productId => 'PRREY546647',
:productCategory => 'Misc. Stationary',
:productSubcategory => 'Stationery Items',
:productPrice => '3.00 USD',
:productManufacturer => 'Reynolds Inc.',
:productAuthor => OpenStruct.new(
:authorFirstName => 'Ryan Reynolds',
:authorMiddleName => 'William',
:authorContact => '16577589898')),
:catalogListType => 'ProductCollection',
:catalogListSource => 'Web'
),
:catalogVerificaitionLog => OpenStruct.new(
:catalogVerificationStatus => 'Verified',
:catalogVerificationDateTime => '2012-03-12T13:00:15+5:30',
:catalogVerificationId => '64774A',
:catalogVerificationRequestedBy => 'user_121')
)]
I am not sure whether it is possible with OpenStruct, as it lacks Nesting capabilities. Is there any other Way to use JSon to achieve this without any outstanding limitations ? Please correct my earlier code.
To see this earlier question, How to Add child Nodes in NodeSet using Nokogiri
Click [here] (http://stackoverflow.com/questions/10300095/how-to-add-child-nodes-in-nodeset-using-nokogiri)
I see below issues with OpenStruct...
i want to access the "productType" of first "catalogProductInfo" and i said "collection.catalogList.catalogProductInfo.productType.content" and I get the below error
"undefined method `productType' for # (NoMethodError)
I got stuck here and want to get out if this situation. If data can't be organized using OpenStruct. Can this be done using JSon. ? Also suggest me the way to do it Nokogiri and JSon. Help me out.

fog and dyn destroy record

I'm currently using Fog to manage Dyn DNS provider. According the documentation, there's a destroy method on the DNS record object. However, when I call destroy, on a record, nothing happens... the method just returns true, but it is never deleted. Here's the code I'm using:
#dynect = Fog::DNS.new(
:provider => "dynect",
:dynect_customer => "CUSTOMER",
:dynect_username => "USERNAME",
:dynect_password => 'PASSWORD'
)
#zone = #dynect.zones.get('zone.example.com')
#record = #zone.records.find{|r| r.name == 'master.zone.example.com' && r.type == 'CNAME'}
#record.destroy
#zone.save
This will return true, but nothing ever happens - the DNS record still exists on Dyn.
How do I delete a record with Fog and Dyn?
Turns out you need to then publish (not save) the zone... This is not obvious, since other providers, like AWS, don't require. Here's an updated code snippet:
#dynect = Fog::DNS.new(
:provider => "dynect",
:dynect_customer => "CUSTOMER",
:dynect_username => "USERNAME",
:dynect_password => 'PASSWORD'
)
#zone = #dynect.zones.get('zone.example.com')
#record = #zone.records.find{|r| r.name == 'master.zone.example.com' && r.type == 'CNAME'}
#record.destroy
#zone.publish # changed this

How to geocode a Twitter status update using the Ruby OAuth Gem

Using the following Ruby code, I can send a status update over the Twitter API to my account. According to the Twitter API Docs I would expect it to be geocoded, but it isn't. What am I doing wrong?
require 'oauth'
consumer = OAuth::Consumer.new('<MY_CONSUMER_KEY>', '<MY_CONSUMER_SECRET>', :site => "http://api.twitter.com", :scheme => :header)
access_token = OAuth::AccessToken.from_hash(consumer, :oauth_token => '<MY_OAUTH_TOKEN>', :oauth_token_secret => '<MY_OAUTH_SECRET>')
access_token.request(:post, "http://api.twitter.com/1/statuses/update.xml", {"Content-Type" => "application/xml", "status" => "This Tweet is from Zuerich in Switzerland", "lat" => "47.3807", "long" => "8.537", "display_coordinates" => "true", "geo_enabled" => "true"})
It works ok. You should enable "Add a location to your tweets" in your twitter profile settings.

Resources