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

Commit

Permalink
Merge pull request #22 from veader/develop
Browse files Browse the repository at this point in the history
add respond_to support for the resource @veader this is awesome, thanks for sending the pull request.
  • Loading branch information
tstachl committed Feb 27, 2014
2 parents 9140f73 + be513e9 commit c283ff8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
21 changes: 18 additions & 3 deletions lib/desk_api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def query_params=(params = {})
protected

def clean_base_url
Addressable::URI.parse(href).path.gsub(/\/(search|\d+)$/, '')
Addressable::URI.parse(href).path.gsub(/\/(search|\d+)$/, '')
end

def exec!(reload = false)
Expand Down Expand Up @@ -146,7 +146,7 @@ def get_linked_resource(method)

return nil if links[method].nil?
return links[method] if links[method].kind_of?(self.class)

links[method] = self.class.new(@_client, self.class.build_self_link(links[method]))
end

Expand All @@ -162,4 +162,19 @@ def method_missing(method, *args, &block)

super(method, *args, &block)
end
end

public
def respond_to?(method, include_private = false)
self.exec! unless @_loaded

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

end
25 changes: 24 additions & 1 deletion spec/desk_api/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@
end
end

context '#respond_to', :vcr do
before do
@company = DeskApi::Resource.new(subject, {
'_links' => {'self'=>{'href'=>'/api/v2/cases','class'=>'page'}},
'name' => 'foo'
}, true)
end

it 'loads the resource to find a suitable method' do
@company.instance_variable_set(:@_loaded, false)
@company.should_receive(:exec!)
@company.respond_to?(:name)
end

it 'returns true if method found in definition' do
@company.respond_to?(:name).should be_true
end

it 'returns false if method does not exist' do
@company.respond_to?(:no_method_here).should be_false
end
end

context '#by_url', :vcr do
it 'finds resources by url' do
subject.articles.by_url('/api/v2/articles/1295677').should be_an_instance_of(DeskApi::Resource)
Expand Down Expand Up @@ -321,4 +344,4 @@
@company.name.should eq('Desk.com')
end
end
end
end

0 comments on commit c283ff8

Please sign in to comment.