Class: Doze::Resource::Proxy

Inherits:
Object
  • Object
show all
Includes:
Doze::Resource
Defined in:
lib/doze/resource/proxy.rb

Direct Known Subclasses

Serialization::ResourceProxy

Instance Attribute Summary collapse

Attributes included from Doze::Resource

#uri

Instance Method Summary collapse

Methods included from Doze::Resource

#cache_expiry_period, #cacheable?, #delete_resource, #exists?, #get, #last_modified, #public_cache_expiry_period, #publicly_cacheable?, #put, #supports_get?, #uri_object

Constructor Details

#initialize(uri, target) ⇒ Proxy

Returns a new instance of Proxy.



14
15
16
17
# File 'lib/doze/resource/proxy.rb', line 14

def initialize(uri, target)
  @uri = uri
  @target = target
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



19
20
21
# File 'lib/doze/resource/proxy.rb', line 19

def target
  @target
end

Instance Method Details

#accepts_method_with_media_type?(resource_method, entity) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'lib/doze/resource/proxy.rb', line 48

def accepts_method_with_media_type?(resource_method, entity)
  method_name = "accepts_#{resource_method}_with_media_type?"
  if respond_to?(method_name)
    send(method_name, entity)
  else
    target && target.accepts_method_with_media_type?(resource_method, entity)
  end
end

#authorize(user, method) ⇒ Object

Some methods which should return something other than nil by default for an empty target:



71
72
73
# File 'lib/doze/resource/proxy.rb', line 71

def authorize(user, method)
  target ? target.authorize(user, method) : true
end

#other_method(method_name, entity = nil) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/doze/resource/proxy.rb', line 40

def other_method(method_name, entity=nil)
  if respond_to?(method_name)
    send(method_name, entity)
  else
    target && target.other_method(method_name, entity)
  end
end

#post(entity, session) ⇒ Object

proxying post is a bit fiddly due to the (in retrospect perhaps a bit unadvised) convenience support for different method arities for post. maybe move to post_with_session(entity, session) vs post(entity) if this causes any more pain.



60
61
62
63
64
65
66
67
# File 'lib/doze/resource/proxy.rb', line 60

def post(entity, session)
  t = target or return
  if t.method(:post).arity.abs > 1
    t.post(entity, session)
  else
    t.post(entity)
  end
end

#supports_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/doze/resource/proxy.rb', line 31

def supports_method?(method)
  supports_method = "supports_#{method}?"
  if respond_to?(supports_method)
    send(supports_method)
  else
    target && target.supports_method?(method)
  end
end

#try(method, *args, &block) ⇒ Object

Methods based around use try / respond_to? need special care when proxying:



23
24
25
26
27
28
29
# File 'lib/doze/resource/proxy.rb', line 23

def try(method, *args, &block)
  if respond_to?(method)
    send(method, *args, &block)
  elsif target.respond_to?(method)
    target && target.send(method, *args, &block)
  end
end