class DeskApi::Resource
{DeskApi::Resource} holds most of the magic of this wrapper. Basically everything that comes back from Desk.com's API is a Resource
, it keeps track of all the data, connects you to other resources through links and allows you access to embedded resources.
@author Thomas Stachl <tstachl@salesforce.com> @copyright Copyright © 2013-2016 Salesforce.com @license BSD 3-Clause License
@example get a cases {DeskApi::Resource}
cases_resource = DeskApi.cases
Attributes
Public Class Methods
Returns a {DeskApi::Resource} definition with a self link
@param link [String/Hash] the self href as string or hash @return [Hash]
# File lib/desk_api/resource.rb, line 61 def build_self_link(link, params = {}) link = { 'href' => link } if link.kind_of?(String) { '_links' => { 'self' => link } } end
Initializes a new {DeskApi::Resource} object
@param client [DeskApi::Client] the client to be used @param definition [Hash] a defintion for the resource @param loaded [Boolean] indicator of the loading state @return [DeskApi::Resource] the new resource
# File lib/desk_api/resource.rb, line 73 def initialize(client, definition = {}, loaded = false) reset! @_client, @_definition, @_loaded = client, definition, loaded end
Public Instance Methods
Executes the request to the Desk.com API if the resource is not loaded yet
@param reload [Boolean] should reload the resource @return [DeskApi::Resource] self
# File lib/desk_api/resource.rb, line 177 def exec!(reload = false) return self if loaded? and !reload @_definition, @_loaded = @_client.get(href).body, true self end
Returns the self link hash
@return [Hash] self link hash
# File lib/desk_api/resource.rb, line 94 def get_self @_definition['_links']['self'] end
Returns the self link href
@return [String] self link href
# File lib/desk_api/resource.rb, line 101 def href get_self['href'] end
Set the self link href
@return [DeskApi::Resource] self
# File lib/desk_api/resource.rb, line 109 def href=(value) @_definition['_links']['self']['href'] = value self end
Only loads the current resource if it isn't loaded yet
@return [DeskApi::Resource] self
# File lib/desk_api/resource.rb, line 161 def load self.exec! unless @_loaded end
Reloads the current resource
@return [DeskApi::Resource] self
# File lib/desk_api/resource.rb, line 153 def load! self.exec! true end
Is the current resource loaded?
@return [Boolean]
# File lib/desk_api/resource.rb, line 168 def loaded? !!@_loaded end
Change self to the next page
@return [Desk::Resource] self
# File lib/desk_api/resource.rb, line 81 def next! load next_page = @_definition['_links']['next'] if next_page @_definition = DeskApi::Resource.build_self_link(next_page) self.reset! end end
Resets a {DeskApi::Resource} to an empty state
@return [DeskApi::Resource] self
# File lib/desk_api/resource.rb, line 186 def reset! @_links, @_embedded, @_changed, @_loaded = {}, {}, {}, false self end
Returns the given resource type
@return [String] resource type/class
# File lib/desk_api/resource.rb, line 130 def resource_type get_self['class'] end
Checks if this resource responds to a specific method
@param method [String/Symbol] @return [Boolean]
# File lib/desk_api/resource.rb, line 138 def respond_to?(method) load meth = method.to_s return true if is_embedded?(meth) return true if is_link?(meth) return true if meth.end_with?('=') and is_field?(meth[0...-1]) return true if is_field?(meth) super end
Returns a hash based on the current definition of the resource
@return [Hash] definition hash
# File lib/desk_api/resource.rb, line 117 def to_hash load {}.tap do |hash| @_definition.each do |k, v| hash[k] = v end end end
Private Instance Methods
Returns the embedded resource
@param method [String/Symbol] @return [DeskApi::Resource]
# File lib/desk_api/resource.rb, line 233 def get_embedded_resource(method) return @_embedded[method] if @_embedded.key?(method) @_embedded[method] = @_definition['_embedded'][method] if @_embedded[method].kind_of?(Array) @_embedded[method].tap do |ary| ary.map!{ |definition| new_resource(definition, true) } unless ary.first.kind_of?(self.class) end else @_embedded[method] = new_resource(@_embedded[method], true) end end
Returns the field value from the changed or definition hash
@param method [String/Symbol] @return [Mixed]
# File lib/desk_api/resource.rb, line 225 def get_field_value(method) @_changed.key?(method) ? @_changed[method] : @_definition[method] end
Returns the linked resource
@param method [String/Symbol] @return [DeskApi::Resource]
# File lib/desk_api/resource.rb, line 250 def get_linked_resource(method) return @_links[method] if @_links.key?(method) @_links[method] = @_definition['_links'][method] if @_links[method] and not @_links[method].kind_of?(self.class) @_links[method] = new_resource(self.class.build_self_link(@_links[method])) end end
Checks if the given `method` is embedded in the current resource definition
@param method [String/Symbol] @return [Boolean]
# File lib/desk_api/resource.rb, line 217 def is_embedded?(method) @_definition.key?('_embedded') and @_definition['_embedded'].key?(method) end
Checks if the given `method` is a field on the current resource definition
@param method [String/Symbol] @return [Boolean]
# File lib/desk_api/resource.rb, line 199 def is_field?(method) @_definition.key?(method) end
Checks if the given `method` is a link on the current resource definition
@param method [String/Symbol] @return [Boolean]
# File lib/desk_api/resource.rb, line 208 def is_link?(method) @_definition.key?('_links') and @_definition['_links'].key?(method) end
Returns the requested embedded resource, linked resource or field value, also allows to set a new field value
@param method [String/Symbol] @param args [Mixed] @param block [Proc] @return [Mixed]
# File lib/desk_api/resource.rb, line 275 def method_missing(method, *args, &block) load meth = method.to_s return get_embedded_resource(meth) if is_embedded?(meth) return get_linked_resource(meth) if is_link?(meth) return @_changed[meth[0...-1]] = args.first if meth.end_with?('=') and is_field?(meth[0...-1]) return get_field_value(meth) if is_field?(meth) super(method, *args, &block) end
Creates a new resource
@param definition [Hash] @param loaded [Boolean] @param client [DeskApi::Client]
# File lib/desk_api/resource.rb, line 264 def new_resource(definition, loaded = false, client = @_client) self.class.new(client, definition, loaded) end