module DeskApi::Resource::SCRUD

{DeskApi::Resource::SCRUD} handles all the search, create, read, update and delete functionality on the {DeskApi::Resource}

@author Thomas Stachl <tstachl@salesforce.com> @copyright Copyright © 2013-2016 Salesforce.com @license BSD 3-Clause License

@example search for cases {DeskApi::Resource}

cases = DeskApi.cases.search(subject: 'Test')

Public Instance Methods

by_id(id, options = {})
Alias for: find
create(params = {}) click to toggle source

This method will POST to the Desk.com API and create a new resource

@param params [Hash] the params to create the resource @return [DeskApi::Resource] the newly created resource

# File lib/desk_api/resource/scrud.rb, line 47
def create(params = {})
  new_resource(@_client.post(clean_base_url, params).body, true)
end
delete() click to toggle source

Deletes the {DeskApi::Resource}.

@return [Boolean] has the resource been deleted?

# File lib/desk_api/resource/scrud.rb, line 72
def delete
  @_client.delete(href).status === 204
end
find(id, options = {}) click to toggle source

Returns a {DeskApi::Resource} based on the given id

@param id [String/Integer] the id of the resource @param options [Hash] additional options (currently only embed is supported) @return [DeskApi::Resource] the requested resource

# File lib/desk_api/resource/scrud.rb, line 92
def find(id, options = {})
  res = new_resource(self.class.build_self_link("#{clean_base_url}/#{id}"))

  if options[:embed]
    options[:embed] = [options[:embed]] if !options[:embed].is_a?(Array)
    res.embed(*options[:embed])
  end

  res.exec!
end
Also aliased as: by_id
update(params = {}) click to toggle source

Use this method to update a {DeskApi::Resource}, it'll PATCH changes to the Desk.com API

@param params [Hash] the params to update the resource @return [DeskApi::Resource] the updated resource

# File lib/desk_api/resource/scrud.rb, line 56
def update(params = {})
  changes = filter_update_actions params
  changes.merge!(filter_links(params)) # quickfix
  changes.merge!(filter_suppress_rules(params)) # another quickfix -- this is getting gross
  params.each_pair{ |key, value| send("#{key}=", value) if respond_to?("#{key}=") }
  changes.merge!(@_changed.clone)

  reset!
  @_definition, @_loaded = [@_client.patch(href, changes).body, true]

  self
end

Protected Instance Methods

clean_base_url() click to toggle source

Returns a clean base url

@example removes the search if called from a search resource

'/api/v2/cases/search' => '/api/v2/cases'

@example removes the id if your on a specific resource

'/api/v2/cases/1' => '/api/v2/cases'

@return [String] the clean base url

# File lib/desk_api/resource/scrud.rb, line 113
def clean_base_url
  Addressable::URI.parse(href).path.gsub(/\/(search|\d+)$/, '')
end

Private Instance Methods

filter_suppress_rules(params = {}) click to toggle source

Filters the suppress_rules param

@param params [Hash] @return [Hash]

# File lib/desk_api/resource/scrud.rb, line 140
def filter_suppress_rules(params = {})
  params.select{ |key, _| key.to_s == 'suppress_rules' }
end
filter_update_actions(params = {}) click to toggle source

Filters update actions from the params

@see dev.desk.com/API/customers/#update @param params [Hash] @return [Hash]

# File lib/desk_api/resource/scrud.rb, line 124
def filter_update_actions(params = {})
  params.select{ |key, _| key.to_s.include?('_action') }
end