0

I need to create multiple "items" to be used within a json request .

Here's a request that works:

customs_info = EasyPost::CustomsInfo.create(
        eel_pfc: 'NOEEI 30.37(a)',
        customs_certify: true,
        customs_signer: "first_name last_name",
        contents_type: 'merchandise',
        contents_explanation: '',
        restriction_type: 'none',
        restriction_comments: '',
        # non_delivery_option: 'abandon',
        customs_items: [
          {
          description: "asdf",
          quantity: 1,
          weight: 23,
          value: 23,
          # hs_tariff_number: '654321',
          origin_country: 'US'
          },
          {
          description: "1234568",
          quantity: 1,
          weight: 23,
          value: 23,
          # hs_tariff_number: '654321',
          origin_country: 'US'
          }
        ]
      )

What I need is to not need to manually set the customs_items.

I tried:

customs_info = EasyPost::CustomsInfo.create(
        eel_pfc: 'NOEEI 30.37(a)',
        customs_certify: true,
        customs_signer: "#{shipping_address.shipping_address_final.first_name} #{shipping_address.shipping_address_final.last_name}",
        contents_type: 'merchandise',
        contents_explanation: '',
        restriction_type: 'none',
        restriction_comments: '',
        # non_delivery_option: 'abandon',
        customs_items: [
        vendor_line_items.map do |li|
          {
          description: "#{li.shop_product.product.item.title}",
          quantity: li.quantity,
          weight: li.shop_product.product.weight,
          value: li.shop_product.price,
          # hs_tariff_number: '654321',
          origin_country: 'US'
          }
        end
        ]
      )

Error Statement: Parameters to create Custom Item(s) invalid or missing

How can I create the loop to work with the JSON request and work like the first example that works manually?

2
  • 5
    map already returns an array. Remove square brackets around it. customs_items: vendor_line_items.map .... Commented Aug 15, 2019 at 5:41
  • Wow you the best Commented Aug 15, 2019 at 5:58

1 Answer 1

1

If you remove the [] that is surrounding the vendor_line_itmes.map code, you will be good to go.

customs_info = EasyPost::CustomsInfo.create(
    # ...
    customs_items: vendor_line_items.map do |li|
        {
            description: "#{li.shop_product.product.item.title}",
            quantity: li.quantity,
            # ...
        }
    end
    )

The map operation returns an array so the json you are currently generating would look like (note the array of arrays in customs_info):

{ 
    "eel_pfc": "NOEEI 30.37(a)",
    ...

    "customs_info": [
        [
            {
                "description": "...",
                "quantity": 5,
                ...
            }
        ]
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.