Ruby on rails - trying to create estimate_items within estimates

Hi,

I’m working on an app which integrates with Freeagent. What I’m trying to
achieve should be pretty simple in theory - I just want to be able to
create an estimate (which works fine) whilst at the same time add
estimate_items to it…

I have a variable called freeagent_token which I am making the request
with, and an @job variable which takes the current job/user information.
I’m currently trying to add test data for the estimate_items.

Here is the code for the post request:

freeagent_token.post(‘estimates’, :params => {:estimate => {‘project’ =>
@job.accounts_link,
‘contact’
=> @job.client.accounts_link,
‘dated_on’
=> Date.today,
‘status’
=> “Draft”,

‘estimate_type’ => ‘Estimate’,
‘currency’
=> ‘GBP’}},

‘estimate_items’ =>
[
{
:position
=> 1,
:item_type
=> “Hours”,
:quantity
=> “1.03333333”,
:price
=> “12.2”,
:description
=> “sada”,
:sales_tax_value
=> “20.0”
}
],
:headers
=> {‘Content-Type’ => ‘application/json’, ‘Accept’ => ‘application/json’})

This is based on the freeagent docs:

{ “estimate”:
{
“contact”:“https://api.freeagent.com/v2/contacts/1”,
“reference”:“001”,
“estimate_type”:“Estimate”,
“dated_on”:“2011-09-15”,
“status”:“Draft”,
“notes”:“An example of some additional text.”,
“currency”:“GBP”,
“estimate_items”: [
{
“item_type”:“Hours”,
“quantity”:“1.03333333”,
“price”:“12.2”,
“description”:“sada”,
“sales_tax_value”:“20.0”
}
]
}}

The estimate is created absolutely fine, but the estimate_items just come
out as an empty array.

I’ve tried a PUT request for the estimate too:

freeagent_token.put(“https://api.freeagent.com/v2/estimates/1188030”, :
params => {:estimate =>
{

“estimate_items” => [
{

‘position’ => 1,

‘item_type’ => ‘Hours’,

‘quantity’ => ‘3’,

‘price’ => ‘234’,

‘description’ => ‘test description’,

‘sales_tax_rate’ => ‘20.0’
}
]
}
},
:headers
=> {‘Content-Type’ => ‘application/json’, ‘Accept’ => ‘application/json’})

But this just gives me a very unhelpful error: “Completed 500 Internal
Server Error in 64770ms (ActiveRecord: 0.4ms)”

The docs suggest you can make a direct post request to the estimate_items,
ie: freeagent_token.put(“estimate_items”) but it doesnt recognize this as
valid request.

Can anyone point me in the right direciton?? It’s really hard to find any
ruby/rails specific documentation on this - and I feel like its something
that should be a lot simpler to do!

Cheers

Any views or opinions expressed are solely those of the author and do not
necessarily represent those of Purr Digital Ltd. This email and any
attachments to it may be confidential and are intended solely for the use
of the individual to whom it is addressed. If you are not the intended
recipient of this email, you must neither take any action based upon its
contents, nor copy or show it to anyone. Please contact the sender if you
believe you have received this email in error.

Hi - any chance you could shed any light on this? Currently really holding
back the project at the moment!

Cheers

Hi - and many thanks for your response

Hi, and many thanks for your response,

I have copied and pasted the exact response that you put in and
unfortunately still getting the same error ‘OAuth2::Error in
JobsController#create_estimate’.

This is the post request:

  response = freeagent_token.post('estimates', :params => {
                                                            :estimate => 

{
‘project’
=> @job.accounts_link,
‘contact’
=> @job.client.accounts_link,
‘dated_on’
=> Date.today,
‘status’
=> “Draft”,

‘estimate_type’ => ‘Estimate’,
‘currency’
=> ‘GBP’,

‘estimate_items’ =>
[
{
:position
=> 1,
:item_type
=> “Hours”,
:quantity
=> “1.03333333”,
:price
=> “12.2”,
:description
=> “sada”,
:sales_tax_value
=> “20.0”
}
]
}
},
:headers => {
‘Content-Type’ => ‘application/json’, ‘Accept’ => ‘application/json’})

Its telling me the error is on the first line:

response = freeagent_token.post(‘estimates’, :params => {

This seems moreorless the same error as before.

Any ideas?

Hi James,

I’m working on an app which integrates with Freeagent. What I’m trying to achieve should be pretty simple in theory - I just want to be able to create an estimate (which works fine) whilst at the same time add estimate_items to it…

I have a variable called freeagent_token which I am making the request with, and an @job variable which takes the current job/user information. I’m currently trying to add test data for the estimate_items.

It looks like your estimate_items hash is nested as a top level parameter, rather than under the estimates parameters. With a little formatting, your pasted params look like the following:

:params => {
:estimate => {
‘project’ => @job.accounts_link,
‘contact’ => @job.client.accounts_link,
‘dated_on’ => Date.today,
‘status’ => “Draft”,
‘estimate_type’ => ‘Estimate’,
‘currency’ => ‘GBP’
}
},
‘estimate_items’ =>
[
{
:position => 1,
:item_type => “Hours”,
:quantity => “1.03333333”,
:price => “12.2”,
:description => “sada”,
:sales_tax_value => “20.0”
}
]

If you move the estimate items up inside the estimate hash, then creating the estimate will create the items against it in the same request.

:params => {
:estimate => {
‘project’ => @job.accounts_link,
‘contact’ => @job.client.accounts_link,
‘dated_on’ => Date.today,
‘status’ => “Draft”,
‘estimate_type’ => ‘Estimate’,
‘currency’ => ‘GBP’,
‘estimate_items’ =>
[
{
:position => 1,
:item_type => “Hours”,
:quantity => “1.03333333”,
:price => “12.2”,
:description => “sada”,
:sales_tax_value => “20.0”
}
]
}
},

Once you’ve created the estimate if you want to add or edit estimate items, then you need to be using the estimate_items endpoints against the estimate, rather than trying to update the estimate directly. See FreeAgent Developer Dashboard for more details on those. Sounds like you’ll be golden once the estimate items are being created alongside the estimate itself though.

Hope that helps,
C
Caius Durling
Engineer, FreeAgent

One Edinburgh Quay, 133 Fountainbridge, Edinburgh, EH3 9QG
FreeAgent Central Ltd. Registered in sunny Scotland SC316774

Hey,> On 22 Jan 2016, at 11:39, jamesbennett@purrdigital.com wrote:

I have copied and pasted the exact response that you put in and unfortunately still getting the same error 'OAuth2::Error in JobsController#create_estimate’.

Looking at that response you’re passing the parameters through in the correct nesting and I’d expect that to work.

Could you include the full request you’re sending to the API (sanitise any sensitive information, tokens, etc), timestamps of the requests you’ve made, or the raw response from the API, and I will try to shed light on why it’s failing. (http://httpbin.org http://httpbin.org/ can be useful for finding out the exact details of the request you’re sending.)

Thanks,
C
Caius Durling
Engineer, FreeAgent

One Edinburgh Quay, 133 Fountainbridge, Edinburgh, EH3 9QG
FreeAgent Central Ltd. Registered in sunny Scotland SC316774

Thanks again for the response, this seems a strange one!

So I’ve just done a post from the rails project and got the same error:

2016-02-01 10:00:29 +0000

same nondescript OAuth2::Error.

this DOES work with a curl request in the command line:

curl -H “Authorization: Bearer *******” -H “Accept: application/xml” -H
“Content-type: application/json” https://api.freeagent.com/v2/estimates -X
POST -d '{

“estimate”:{

“contact”: “Log in to your FreeAgent account****”,

“status”: “Draft”,

“currency”: “GBP”,

“project” : “Log in to your FreeAgent account****”,

“estimate_type” : “Estimate”, “estimate_items”:
[{“item_type”:“Hours”,“quantity”:“1.03333333”,“price”:“12.2”,“description”:“sada”,“sales_tax_value”:“20.0”}],

“dated_on” : “2015-01-31T00:00:00+00:00”}

}’

Created at: 2016-02-01T12:11:09Z

any ideas on why the ruby syntax fails?

Just FYI - the following request to create an estimate without estimate
items works fine:

  freeagent_token.post('estimates', :params => {
                                                :estimate => {
                                                  'project' => @job.

accounts_link,
‘contact’ => @job.
client.accounts_link,
‘dated_on’ => Date.
today,
‘status’ => “Draft”,
‘estimate_type’ =>
‘Estimate’,
‘currency’ => ‘GBP’}},
:headers => {
‘Content-Type’ => ‘application/json’, ‘Accept’ => ‘application/json’})