Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop' of github.com:tstachl/desk into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Stachl committed May 21, 2014
2 parents 3a4c6f6 + dd1fce5 commit 791e54b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/desk_api/resource.rb
Expand Up @@ -17,7 +17,7 @@ def initialize(client, definition = {}, loaded = false)
end

def create(params = {})
self.class.new(@_client, @_client.post(clean_base_url, params).body, true)
new_resource(@_client.post(clean_base_url, params).body, true)
end

def update(params = {})
Expand All @@ -36,11 +36,11 @@ def search(params = {})
params = { q: params } if params.kind_of?(String)
url = Addressable::URI.parse(clean_base_url + '/search')
url.query_values = params
self.class.new(@_client, self.class.build_self_link(url.to_s))
new_resource(self.class.build_self_link(url.to_s))
end

def find(id, options = {})
res = self.class.new(@_client, self.class.build_self_link("#{clean_base_url}/#{id}"))
res = new_resource(self.class.build_self_link("#{clean_base_url}/#{id}"))
res.embed(*(options[:embed].kind_of?(Array) ? options[:embed] : [options[:embed]])) if options[:embed]
res.exec!
end
Expand Down Expand Up @@ -155,6 +155,14 @@ def reload!
end
alias_method :load!, :reload!

def load
self.exec! unless @_loaded
end

def loaded?
@_loaded
end

protected

def clean_base_url
Expand All @@ -172,14 +180,6 @@ def reset!
self
end

def load
self.exec! unless @_loaded
end

def loaded?
@_loaded
end

private
attr_accessor :_client, :_loaded, :_changed, :_embedded, :_links, :_definition

Expand Down Expand Up @@ -209,23 +209,26 @@ def get_embedded_resource(method)

if @_embedded[method].kind_of?(Array)
@_embedded[method].tap do |ary|
ary.map!{ |definition| self.class.new(@_client, definition, true) } unless ary.first.kind_of?(self.class)
ary.map!{ |definition| new_resource(definition, true) } unless ary.first.kind_of?(self.class)
end
else
@_embedded[method] = self.class.new(@_client, @_embedded[method], true)
@_embedded[method] = new_resource(@_embedded[method], true)
end
end

def get_linked_resource(method)
return @_links[method] if @_links.key?(method)
@_links[method] = @_definition['_links'][method]

# NOTE: create method for self.class.new
if @_links[method] and not @_links[method].kind_of?(self.class)
@_links[method] = self.class.new(@_client, self.class.build_self_link(@_links[method]))
@_links[method] = new_resource(self.class.build_self_link(@_links[method]))
end
end

def new_resource(definition, loaded=false, client=@_client)
self.class.new(client, definition, loaded)
end

def method_missing(method, *args, &block)
self.load

Expand Down
9 changes: 9 additions & 0 deletions spec/desk_api/resource_spec.rb
Expand Up @@ -516,6 +516,15 @@
end
end

context '#new_resource' do
it 'returns a new desk resource from a hash definition' do
subject.
cases.
send(:new_resource, DeskApi::Resource.build_self_link('/api/v2/customers')).
should be_an_instance_of(DeskApi::Resource)
end
end

describe 'prioritize links and embeds' do
before do
@company = subject.customers.entries.first.company
Expand Down

0 comments on commit 791e54b

Please sign in to comment.