lib/hyperdrive/resource.rb in hyperdrive-0.0.15 vs lib/hyperdrive/resource.rb in hyperdrive-0.0.16
- old
+ new
@@ -1,21 +1,22 @@
# encoding: utf-8
module Hyperdrive
class Resource
include Hyperdrive::Values
- attr_reader :id, :namespace, :endpoint, :params, :filters, :request_handlers, :version
+ attr_reader :id, :namespace, :endpoint, :params, :filters, :request_handlers, :callbacks, :version
attr_accessor :name, :description
def initialize(resource, options = {})
@resource = resource.to_s.split('_')
@resource[-1] = @resource[-1].en.plural
@namespace = @resource.join(':')
@endpoint = options.fetch(:endpoint) { "/#{@resource.join('/')}" }
@params = default_params
@filters = default_filters
@request_handlers = default_request_handlers
+ @callbacks = { before: {} }
@config = hyperdrive.config
@id = [@config[:vendor], @namespace].join(':')
end
def register_param(param, description, options = {})
@@ -24,10 +25,33 @@
def register_filter(filter, description, options = {})
@filters[filter] = Filter.new(filter, description, options)
end
+ def register_callback(callback_type, request_method, callback, version = 'v1')
+ @callbacks[callback_type] ||={}
+ @callbacks[callback_type][request_method] ||= {}
+ @callbacks[callback_type][request_method].merge!({ version => callback })
+ if request_method == :get
+ @callbacks[callback_type][:head] ||= {}
+ @callbacks[callback_type][:head].merge!({ version => @callbacks[callback_type][:get][version] })
+ end
+ end
+
+ def callback(callback_type, http_request_method, version = nil)
+ version ||= latest_version(http_request_method)
+ request_method = http_request_methods[http_request_method]
+ callbacks[callback_type][request_method][version]
+ end
+
+ def has_callback?(callback_type, http_request_method, version = nil)
+ version ||= latest_version(http_request_method)
+ request_method = http_request_methods[http_request_method]
+ callbacks.has_key?(callback_type) and callbacks[callback_type].has_key?(request_method) and callbacks[callback_type][request_method].has_key?(version)
+ end
+
+
def register_request_handler(request_method, request_handler, version = 'v1')
@request_handlers[request_method] ||= {}
@request_handlers[request_method].merge!({ version => request_handler })
if request_method == :get
@request_handlers[:head] ||= {}
@@ -41,15 +65,15 @@
request_handlers[request_method][version]
end
def acceptable_content_types(http_request_method)
content_types = []
- media_type_namepace = @resource.join('.')
+ media_type_namespace = @resource.join('-')
@config[:media_types].each do |media_type|
available_versions(http_request_method).each do |version|
- content_types << "application/vnd.#{@config[:vendor]}.#{media_type_namepace}.#{version}+#{media_type}"
+ content_types << "application/vnd.#{@config[:vendor]}.#{media_type_namespace}.#{version}+#{media_type}"
end
- content_types << "application/vnd.#{@config[:vendor]}.#{media_type_namepace}+#{media_type}"
+ content_types << "application/vnd.#{@config[:vendor]}.#{media_type_namespace}+#{media_type}"
content_types << "application/vnd.#{@config[:vendor]}+#{media_type}"
end
content_types
end