lib/excon/hypermedia/response.rb in excon-hypermedia-0.2.0 vs lib/excon/hypermedia/response.rb in excon-hypermedia-0.3.0
- old
+ new
@@ -8,58 +8,45 @@
#
# This HyperMedia::Response object helps determine valid subsequent
# requests and attribute values.
#
class Response
- attr_reader :response
-
def initialize(response)
@response = response
end
# handle
#
# Correctly handle the hypermedia request.
#
def handle(method_name, *params)
- return false if disabled?
+ return false unless enabled?
- case resource.type?(method_name)
- when :link then return handle_link(method_name, params)
- when :attribute then return handle_attribute(method_name)
+ if method_name == :rel
+ handle_link(params.shift, params)
+ elsif resource.type?(method_name) == :link
+ handle_link(method_name, params)
+ elsif resource.respond_to?(method_name, false)
+ resource.send(method_name, *params)
+ else
+ false
end
-
- respond_to?(method_name) ? send(method_name) : false
end
- def links
- resource.links
- end
+ private
- def attributes
- resource.attributes
- end
+ attr_reader :response
def resource
@resource ||= Resource.new(response.body)
end
def enabled?
response.data[:hypermedia] == true
end
- def disabled?
- !enabled?
- end
-
- private
-
def handle_link(name, params)
Excon.new(resource.link(name).href, params.first.to_h.merge(hypermedia: true))
- end
-
- def handle_attribute(name)
- attributes[name.to_s]
end
end
end
end