Home > OS >  setting conditional attributes for API connection
setting conditional attributes for API connection

Time:12-17

A method needs to instantiate a session with various attributes, some of which are optional

session = Checkout::Session.create({
            locale: I18n.locale,
            reference_id: id,
            customer_email: @user_mail,
            [...]
          })

The last shown attribute, customer_email, is optional but it should not be generated if the value does not exist.

customer_email: @user_mail unless !@user_email,

logically hits a syntax error because an additional param (the comma) is being produced

syntax error, unexpected ',', expecting end

and thus the API expects another attribute.

(customer_email: @user_mail, unless !@user_email)

also fails as there is confusion over the parenthesis

syntax error, unexpected ')', expecting then or ';' or '\n'

How should this syntax be cast?

CodePudding user response:

You need to extract the options hash into a variable and manipulate it before sending it to the Checkout::Session.create.

Something like this:

 options = {
   locale: I18n.locale,
   reference_id: id
 }
 options[:customer_email] = @user_mail if @user_email
 session = Checkout::Session.create(options)
  • Related