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

Commit

Permalink
Added simple_oauth to runtime dependencies, fixed #6
Browse files Browse the repository at this point in the history
Added get_href to resource, fixed #7
Privatized methods not needed in outside scope, fixed #8
  • Loading branch information
Thomas Stachl committed Sep 1, 2013
1 parent 77b27b3 commit aa2a623
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions desk_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency('multi_json')
gem.add_runtime_dependency('faraday')
gem.add_runtime_dependency('faraday_middleware')
gem.add_runtime_dependency('simple_oauth')
gem.add_runtime_dependency('typhoeus')
gem.add_runtime_dependency('addressable')
gem.add_runtime_dependency('activesupport')
Expand Down
2 changes: 1 addition & 1 deletion lib/desk_api/action/embedded.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DeskApi
module Action
module Embedded
protected
private
def setup_embedded(entries)
@records = entries.map do |record|
resource(record._links.self['class']).new(client, record, true)
Expand Down
2 changes: 1 addition & 1 deletion lib/desk_api/action/field.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DeskApi
module Action
module Field
protected
private
# Handles the _links part of the HAL response and sets up the associations.
# It is used by the client to setup the initial resources like `users`, `cases` and
# so on, as well as by the resource object itself for sub resources (`cases.replies`).
Expand Down
4 changes: 2 additions & 2 deletions lib/desk_api/action/link.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DeskApi
module Action
module Link
protected
private
attr_accessor :_links
# Handles the _links part of the HAL response and sets up the associations.
# It is used by the client to setup the initial resources like `users`, `cases` and
Expand All @@ -18,7 +18,7 @@ def setup_links(links)
# this is a really ugly hack but necessary for sub resources which aren't declared consistently
definition['class'] = 'page' if method.pluralize == method
# get the client
client = self.instance_of?(DeskApi::Client) ? self : self.client
client = self.instance_of?(DeskApi::Client) ? self : @client
# create the new resource
@_links[method]['resource'] = resource(definition['class']).new client, Hashie::Mash.new({ _links: { self: definition } })
end
Expand Down
2 changes: 1 addition & 1 deletion lib/desk_api/action/resource.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DeskApi
module Action
module Resource
protected
private
def resource(name)
require "desk_api/resource/#{name}"
"DeskApi::Resource::#{name.classify}".constantize
Expand Down
20 changes: 14 additions & 6 deletions lib/desk_api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@ def get_self
@_links.self
end

def get_href
get_self['href']
end

protected

def exec!(reload = false)
return self if loaded and !reload
definition, @loaded = client.get(@_links.self.href).body, true
setup(definition)
end

private

attr_accessor :client, :loaded, :_changed

def setup(definition)
Expand All @@ -38,16 +51,11 @@ def setup(definition)
self
end

def exec!(reload = false)
return self if loaded and !reload
definition, @loaded = client.get(@_links.self.href).body, true
setup(definition)
end

def method_missing(method, *args, &block)
self.exec! if !loaded
raise DeskApi::Error::MethodNotSupported unless self.respond_to?(method.to_sym)
self.send(method, *args, &block)
end

end
end
15 changes: 15 additions & 0 deletions spec/desk_api/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@
end
end

context '#get_self' do
it 'returns the hash for self' do
subject.articles.get_self.should eq({
"href" => "/api/v2/articles",
"class" => "page"
})
end
end

context '#get_href' do
it 'returns the href for self' do
subject.articles.get_href.should eq('/api/v2/articles')
end
end

context '#resource', :vcr do
it 'requires the specified resource' do
subject.send(:resource, 'article').should equal(DeskApi::Resource::Article)
Expand Down

0 comments on commit aa2a623

Please sign in to comment.