Module: FreshdeskAPI::Save

Includes:
ResponseHandler
Included in:
Create, Update
Defined in:
lib/freshdesk_api/actions.rb

Instance Method Summary collapse

Methods included from ResponseHandler

#handle_response

Instance Method Details

#save(options = {}, &block) ⇒ Object

Saves, returning false if it fails and attachibg the errors



68
69
70
71
72
73
74
75
# File 'lib/freshdesk_api/actions.rb', line 68

def save(options = {}, &block)
  save!(options, &block)
rescue FreshdeskAPI::Error::RecordInvalid => e
  @errors = e.errors
  false
rescue FreshdeskAPI::Error::ClientError
  false
end

#save!(options = {}) ⇒ Resource

If this resource hasn’t been deleted, then create or save it. Executes a POST if it is a Data#new_record?, otherwise a PUT.

Returns:

  • (Resource)

    created or updated object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/freshdesk_api/actions.rb', line 48

def save!(options = {})
  return false if respond_to?(:destroyed?) && destroyed?

  options = { request_namespace => attributes }

  if new_record?
    method = :post
    req_path = api_url(options)
  else
    method = :put
    req_path = api_url(options) + "/#{id}"
  end

  response = @client.make_request!(req_path, method, options)

  handle_response(response)
  return self
end