Michael O Meara
Michael O Meara

Reputation: 49

Putting Variable with in single quotes?

Hi all I am making a request to the Campaign Monitor API and the data has to be held with in single quotes as a string but in JSON format. As it has to be single quotes I am unable to put in any variable values. Below is the code.

response = HTTParty.post(url, 
:basic_auth => auth, :body => '{
            "EmailAddress":"[email protected]",
            "Name":"#{@fname}",
            "CustomFields":[
                  {
                            "Key":"firstName",
                            "Value":"#{@fname}"
                        },
                        {
                          "Key":"country",
                            "Value":"#{@country}"
                        }
                        ],
            "Resubscribe":true,
            "RestartSubscriptionBasedAutoresponders":true
        }')

I have tried a few different methods such as breaking the string up and piecing it back together with the variable with in double quotes but that has failed as well for the request to be successful it has to be exact.

Upvotes: 0

Views: 2040

Answers (4)

Stefan
Stefan

Reputation: 114138

Instead of building a JSON structure by hand, you can build a Ruby hash and convert it to JSON:

require 'json'

data = {
  'EmailAddress' => '[email protected]',
  'Name' => @fname,
  'CustomFields' => [
    { 'Key' => 'firstName', 'Value' => @fname },
    { 'Key' => 'country', 'Value' => @country }
  ],
  'Resubscribe' => true,
  'RestartSubscriptionBasedAutoresponders' => true
}

response = HTTParty.post(url, basic_auth: auth, body: data.to_json)

Also note that there's a Ruby gem for the Campaign Monitor API: createsend-ruby

Using the gem, the above code translates to:

custom_fields = [
  { 'Key' => 'firstName', 'Value' => @fname },
  { 'Key' => 'country', 'Value' => @country }
]
response = CreateSend::Subscriber.add(auth, list_id, '[email protected]', @fname, custom_fields, true, true)

Upvotes: 3

Simone
Simone

Reputation: 21262

You can use here documents:

name = 'John'
<<EOS
This is #{name}
EOS

Alternatively you can use flexible quotes, they can handle both ' and " characters:

name = 'John'
%{
This is #{name}
}

Flexible quoting works with %() and %!! as well.

Upvotes: 0

davidrac
davidrac

Reputation: 10738

You can use heredoc as explained here

Double-quoting rules are also followed if you put double quotes around the identifier. However, do not put double quotes around the terminator.

puts <<"QUIZ"
Student: #{name}

1.\tQuestion: What is 4+5?
\tAnswer: The sum of 4 and 5 is #{4+5}
QUIZ

Upvotes: 1

Jean Bob
Jean Bob

Reputation: 565

you can try with heredoc :

response = HTTParty.post(url, 
  :basic_auth => auth, :body => <<-BODY_CONTENT
  {
        "EmailAddress":"[email protected]",
        "Name":"#{@fname}",
        "CustomFields":[
              {
                        "Key":"firstName",
                        "Value":"#{@fname}"
                    },
                    {
                      "Key":"country",
                        "Value":"#{@country}"
                    }
                    ],
        "Resubscribe":true,
        "RestartSubscriptionBasedAutoresponders":true
  }
BODY_CONTENT
)

Upvotes: 1

Related Questions