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"
}
]
}
]
}
]