lib/excon/hypermedia/response.rb in excon-hypermedia-0.3.0 vs lib/excon/hypermedia/response.rb in excon-hypermedia-0.4.0
- old
+ new
@@ -19,34 +19,41 @@
# Correctly handle the hypermedia request.
#
def handle(method_name, *params)
return false unless enabled?
- 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
+ case method_name
+ when :resource then resource
+ when :rel then rel(params.shift, params)
+ else false
end
end
private
attr_reader :response
def resource
- @resource ||= Resource.new(response.body)
+ @resource ||= ResourceObject.new(body_to_hash)
end
+ def body_to_hash
+ content_type.include?('application/hal+json') ? JSON.parse(response.body) : {}
+ end
+
+ def content_type
+ response.headers['Content-Type'].to_s
+ end
+
def enabled?
response.data[:hypermedia] == true
end
- def handle_link(name, params)
- Excon.new(resource.link(name).href, params.first.to_h.merge(hypermedia: true))
+ def rel(name, params)
+ link = resource._links.send(name)
+ options = params.first.to_h.merge(hypermedia: true)
+
+ link.respond_to?(:to_ary) ? link.map { |l| l.rel(options) } : link.rel(options)
end
end
end
end