Question

I am working with an API and I am trying to post the following request in a helper

module ShoppingCartHelper

def update_order
  HTTParty.post("APIURL",
            :body => {
            @all_lines.each do |line|
            :ProductCode => line.ProductCode,
            :Quantity => line.Quantity,
            :UnitPrice => line.UnitPrice,
            :Pages =>
            [
                {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                        { 
                            :AssetNumber => line.AssetNumber,
                            :Name => line.Name,
                            :HiResImage => line.HiResImage, 
                            :CropMode => line.CropMode
                        }
                    ]
                }
            ]
        end
        }.to_json,
      :headers => { 'Content-Type' => 'application/json' })
end

end

When I do this I get all kinds of syntax errors and unexpected characters. @all_lines is all of the lines in the database for an order and I'm trying to build that json body for each line it finds.. please help!.

I'm not using something like jbuilder because all of those fields I'm using are just in one table.. they aren't linked together to where I could include them. Unless there's some other way to create custom labels

UPDATE: I Need this structure to work but im getting syntax error:

  HTTParty.post("APIURL",
            :body => 
            "Lines" =>
            [
            @all_lines.map do |line|
             {
               :ProductCode => line.ProductCode,
               :Quantity => line.Quantity,
               :UnitPrice => line.UnitPrice,
               :Pages =>
               [
                  {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                      { 
                        :AssetNumber => line.AssetNumber,
                        :Name => line.Name,
                        :HiResImage => line.HiResImage, 
                        :CropMode => line.CropMode
                      }
                    ]
                  }
               ]
            } 
          end.to_json],
      :headers => { "Content-Type" => "application/json"})

The Request body from API needs to look like this:

"Lines":
    [
        {
            "ProductCode":"5x7",
            "Quantity":2,
            "UnitPrice":1.25,
            "Pages":
            [
                {
                    "PageNumber":1,
                    "Assets":
                    [
                        {
                            "AssetNumber":1,
                            "Name":"000-R20110419153212.jpg",
                            "HiResImage":"http:\/\/www.google.com\/xyz.jpg",
                            "CropMode":"FILLIN"
                        }
                    ]
                }
            ]
        }
    ]
Was it helpful?

Solution

The each API returns the array itself (without modification). You might want to use map, which returns an array of the results of the block:

[1, 2, 3].each { |x| x*2 }
# => [1, 2, 3]
[1, 2, 3].map { |x| x*2 }
# => [2, 4, 6]

So try (you also missed the location of the curly braces of the hash result in your block):

def update_order
  HTTParty.post("APIURL",
            :body => 
            @all_lines.map do |line|
             {
               :ProductCode => line.ProductCode,
               :Quantity => line.Quantity,
               :UnitPrice => line.UnitPrice,
               :Pages =>
               [
                  {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                      { 
                        :AssetNumber => line.AssetNumber,
                        :Name => line.Name,
                        :HiResImage => line.HiResImage, 
                        :CropMode => line.CropMode
                      }
                    ]
                  }
               ]
            } 
          end.to_json,
      :headers => { 'Content-Type' => 'application/json' })
end

With 'Lines' wrapper:

HTTParty.post("APIURL",
            :body => 
            {"Lines" =>
            [
            @all_lines.map do |line|
             {
               :ProductCode => line.ProductCode,
               :Quantity => line.Quantity,
               :UnitPrice => line.UnitPrice,
               :Pages =>
               [
                  {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                      { 
                        :AssetNumber => line.AssetNumber,
                        :Name => line.Name,
                        :HiResImage => line.HiResImage, 
                        :CropMode => line.CropMode
                      }
                    ]
                  }
               ]
            } 
          end]}.to_json,
      :headers => { "Content-Type" => "application/json" })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top