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
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
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
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
Using this method allows you to hit the search endpoint
@param params [Hash] the search params @return [DeskApi::Resource] the search page resource
# File lib/desk_api/resource/scrud.rb, line 80 def search(params = {}) params = { q: params } if params.kind_of?(String) url = Addressable::URI.parse(clean_base_url + '/search') url.query_values = params new_resource(self.class.build_self_link(url.to_s)) end
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
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
Filters the links
@param params [Hash] @return [Hash]
# File lib/desk_api/resource/scrud.rb, line 132 def filter_links(params = {}) params.select{ |key, _| key.to_s == '_links' } end
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
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