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

_changed[RW]
_client[RW]
_definition[RW]
_embedded[RW]
_loaded[RW]

Public Class Methods

new(client, definition = {}, loaded = false) click to toggle source

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

exec!(reload = false) click to toggle source

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
get_href()
Alias for: href
get_self() click to toggle source

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
href() click to toggle source

Returns the self link href

@return [String] self link href

# File lib/desk_api/resource.rb, line 101
def href
  get_self['href']
end
Also aliased as: get_href
href=(value) click to toggle source

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
load() click to toggle source

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
load!() click to toggle source

Reloads the current resource

@return [DeskApi::Resource] self

# File lib/desk_api/resource.rb, line 153
def load!
  self.exec! true
end
Also aliased as: reload!
loaded?() click to toggle source

Is the current resource loaded?

@return [Boolean]

# File lib/desk_api/resource.rb, line 168
def loaded?
  !!@_loaded
end
next!() click to toggle source

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
reload!()
Alias for: load!
reset!() click to toggle source

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
resource_type() click to toggle source

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
respond_to?(method) click to toggle source

Checks if this resource responds to a specific method

@param method [String/Symbol] @return [Boolean]

Calls superclass method
# 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
to_hash() click to toggle source

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

get_embedded_resource(method) click to toggle source

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
get_field_value(method) click to toggle source

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
get_linked_resource(method) click to toggle source

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
is_embedded?(method) click to toggle source

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
is_field?(method) click to toggle source

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
method_missing(method, *args, &block) click to toggle source

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]

Calls superclass method
# 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
new_resource(definition, loaded = false, client = @_client) click to toggle source

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