简体   繁体   中英

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":"js@mike.com",
            "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.

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

require 'json'

data = {
  'EmailAddress' => 'js@mike.com',
  '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, 'js@mike.com', @fname, custom_fields, true, true)

you can try with heredoc :

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM