I'm having some real difficultly trying to send multiple email addresses in json to the sendgrid API.
Here is my code that works for one email address:
#result = HTTParty.post("https://api.sendgrid.com/api/newsletter/lists/email/add.json",
:body => { :list => "#{#survey.name}_#{#survey.id}", :data => '{ "name": "John Smith", "email": "john#smith.com" }', :api_user => 'XXXXX', :api_key => 'XXXXX'})
But in ruby using the :data post attribute how can I add another email address?
The following don't work - life would be too easy lol
:data => '[{"email" => "nick#sendgrid.com"},{"email" => "jane#example.com"}]'
OR
:data => '{[{"email" => "nick#sendgrid.com"},{"email" => "jane#example.com"}]}'
Apparently SendGrid expects the following....
data[]={"email" => "nick#sendgrid.com"}&data[]={"email" => "jane#example.com"}
How I can create a post parameter in rails that does this?!?
You could create your :data param as an array of JSONs, like so:
:data => ['{ "name" : "Foo Bar", "email" : "foo#bar.com" }','{ "name" : "Jon Snow", "email" : "jon#snow.com" }']
Related
I have a Rails 4.2 application that does different business depending on host name. I know this practice is frowned upon in certain circles but I managed to get the same code base working on about 400 websites, the amount of time|cpu|ram|db|money|squirrels saved is tremendous.
Eg:
For www.example.com for user john#doe.com it might show Product 1 with a $5000 price tag;
For www.blah.com for all usrs it might show Product 1 with a $123 price tag. And a shipping fee of $400.
This is all based on host name and a number of variables stored into session when users visit and/or log in to the website.
I'd like to write a bunch of specs to test various aspects of the application but I need to be able to set the request.host, format and some things in the session.
How do I get this done?
Later edit:
I managed to do a find and replace through the source code and get the session/request access done from within some helper methods. Now I can stub within my specs as follows:
allow_any_instance_of(ApplicationHelper).to receive(:detect_user_info_by_ip).with("ip").and_return( { :provider => "dummy", :ip => "127.0.0.1", :lat => 1.0, :latitude => 1.0, :lon => 2.1, :long => 2.1, :longitude => 2.1, :regi
on => "EU", :country => "GB", :city => "Testville", :country_iso2 => "GB", :success => true} )
allow_any_instance_of(ApplicationHelper).to receive(:detect_user_info_by_ip).and_return( { :provider => "dummy", :ip => "127.0.0.1", :lat => 1.0, :latitude => 1.0, :lon => 2.1, :long => 2.1, :longitude => 2.1, :region => "EU",
:country => "GB", :city => "Testville", :country_iso2 => "GB", :success => true} )
Bottom line is: easier to refactor and stub than access the request and session objects from within specs.
I have created a rails app with rails 3.2 and ruby 2.1.2. I have used gibbon gem for accessing mail chimp API for create/edit/delete mail chimp list and also to manage subscribers. I am unable to create new mail chimp list using methods provided by gibbon. But I am able to get already created(default lists in mail chimp web app) lists. I want to know how to create a mail chimp list. I did not find examples for the same in github page.
Eventually I got the solution. I can create mail chimp list using following code by using Gibbon 2.2.4.
gibbon = Gibbon::Request.new()
params = {
"name" => name,
"contact" => {
"company" => "Your Company",
"address1" => "address one",
"address2" => "address two",
"city" => "city",
"state" => "state",
"zip" => "zip-code",
"country" => "country name",
"phone" => "phone"
},
"permission_reminder" => "You are receiving this email, because you subscribed our product.",
"campaign_defaults" => {
"from_name" => "Test user",
"from_email" => "testuser#test.com",
"subject" => "",
"language" => "en"
},
"email_type_option" => true
}
mail_chimp_list = gibbon.lists.create(body: params)
my gibbon.rb file looks like
Gibbon::Request.api_endpoint = "https://****.api.mailchimp.com"
Gibbon::Request.api_key = "api-key"
Gibbon::Request.timeout = 15
I'm trying to send a POST request to an api with a hash as the parameters of the request. The api in question is freshdesk's api. I'm using rails 4.
They require that i send a request in this form:
Code
{
"helpdesk_ticket":{
"description":"Some details on the issue ...",
"subject":"Support needed..",
"email":"hulk#outerspace.com",
"priority":1, "status":2
},
"cc_emails":"superman#marvel.com,avengers#marvel.com"
}
Sample Curl
curl -u user#yourcompany.com:test -H "Content-Type: application/json"
-d '{ "helpdesk_ticket": { "description": "Details about the issue...", "subject": "Support Needed...", "email":
"hulk#outerspace.com", "priority": 1, "status": 2 }, "cc_emails":
"superman#marvel.com,avengers#marvel.com" }' -X POST
http://domain.freshdesk.com/helpdesk/tickets.json
This is currently what I have
Controller
def create
email = params[:email]
description = params[:description]
subject = params[:subject]
payload = [{"helpdesk_ticket" => { 'email' => email, 'description' => description, 'subject' => subject, 'priority' => 1, 'status' => 2}}].to_json
trololol = Freshdesk.post('/helpdesk/tickets.json',
:body => payload
)
debugger
redirect_to "/support"
end
class Freshdesk
include HTTParty
#apikey = "Somethingladida"
format :json
base_uri "http://genericname.freshdesk.com/"
basic_auth #apikey, ""
end
View
=form_tag support_index_path do
=label_tag :email
=text_field_tag :email,params[:email]
=label_tag :description
=text_field_tag :description
=label_tag :subject
=text_field_tag :subject
=submit_tag "Submit", class:"button"
The main problem is this: I'm getting a 500 internal server error, so I'm presuming that the request that i'm trying to craft is not in the format that the freshdesk api requires
Cheers! :)
You can mirror that structure exactly by doing something like this:
def freshdesk_hash
{
"helpdesk_ticket":{
"description":"Some details on the issue ...",
"subject":"Support needed..",
"email":"hulk#outerspace.com",
"priority":1, "status":2
},
"cc_emails":"superman#marvel.com,avengers#marvel.com"
}
end
But to me it looks like you're putting the hash into an array ([{...).
I working on an existing code base for a client. The app is built in rails 2.3 and uses the fedex plugin. Now i have develop a functionality for shipping the goods using international priority service type, the problem i'm facing is when ever i make a call to the fedex api for a shipping label it returns an error
"Fedex::FedexError: Unable to get label from Fedex: Customs Value is required"
Can some one help me how to mention the customs value for the label.
This is how my code is:
price, labels, tracking_number = fedex.label(
:shipper => { :contact => shipper, :address => origin },
:recipient => { :contact => recipient, :address => destination.merge(:residential => false) },
:service_type => service_type,
:packages => [{:length => 3,
:height => 4,
:width => 5,
:weight => 5,
}],
:weight => 5,
:commodities => commodities,
:customs_value =>{:currency => "USD", :amount => "100" },
:residential => is_residential_destination
)
Thanks in advance.
I'm trying to subscribe and update members of a Mailchimp list from my Rails 3.2 app via the API.
Everything is working fine, except that I am unable to set interest groups.
What is the correct format for setting groupings.
I've tried
merge_vars: 'GROUPINGS' => [
[
'id' => group_id,
'groups' => ['array', 'of', 'groups']
]
]
and
merge_vars: 'GROUPINGS' => [
[
'id' => group_id,
'groups' => "comma,separated,groups"
]
]
and
merge_vars: 'GROUPINGS' => [
0 => [
'id' => group_id,
'groups' => "comma,separated,groups"
]
]
and several other variations.
Nothing seems to work and the docs are unclear
Took me some time but I finnaly figured out the correct format :
#gb.lists.subscribe({:id => list_id,
:email => {:email => self.email},
:merge_vars => {:FNAME => self.firstname,
:groupings => [{:id => grouping_id,
:groups => ["name","of","groups"]
}]
},
:double_optin => false,
:send_welcome => false,
:update_existing => true})
I'm using rails 3.1.11 and gibbon 0.5.0
Hope this will help !