Class: Hyperdrive::Resource
- Inherits:
-
Object
- Object
- Hyperdrive::Resource
show all
- Includes:
- Values
- Defined in:
- lib/hyperdrive/resource.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#acceptable_content_types(http_request_method) ⇒ Object
-
#allowed_methods ⇒ Object
-
#available_versions(http_request_method) ⇒ Object
-
#callback(callback_type, http_request_method, version = nil) ⇒ Object
-
#has_callback?(callback_type, http_request_method, version = nil) ⇒ Boolean
-
#initialize(resource, options = {}) ⇒ Resource
constructor
A new instance of Resource.
-
#latest_version(http_request_method) ⇒ Object
-
#register_callback(callback_type, request_method, callback, version = 'v1') ⇒ Object
-
#register_filter(filter, description, options = {}) ⇒ Object
-
#register_param(param, description, options = {}) ⇒ Object
-
#register_request_handler(request_method, request_handler, version = 'v1') ⇒ Object
-
#request_handler(http_request_method, version = nil) ⇒ Object
-
#request_method_allowed?(http_request_method) ⇒ Boolean
-
#required?(param, http_request_method) ⇒ Boolean
-
#required_filter?(filter, http_request_method) ⇒ Boolean
-
#required_param?(param, http_request_method) ⇒ Boolean
-
#to_hash ⇒ Object
Methods included from Values
default_config, default_cors_options, default_headers, definable_request_methods, http_request_methods, request_methods, supported_request_methods
Constructor Details
#initialize(resource, options = {}) ⇒ Resource
Returns a new instance of Resource.
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/hyperdrive/resource.rb', line 9
def initialize(resource, options = {})
@resource = resource.to_s.split('_')
@resource[-1] = @resource[-1].en.plural
@namespace = options.fetch(:namespace) { @resource.join(':') }
@media_type_namespace = options.fetch(:media_type_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
|
Instance Attribute Details
#callbacks ⇒ Object
Returns the value of attribute callbacks.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def callbacks
@callbacks
end
|
#description ⇒ Object
Returns the value of attribute description.
7
8
9
|
# File 'lib/hyperdrive/resource.rb', line 7
def description
@description
end
|
#endpoint ⇒ Object
Returns the value of attribute endpoint.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def endpoint
@endpoint
end
|
#filters ⇒ Object
Returns the value of attribute filters.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def filters
@filters
end
|
#id ⇒ Object
Returns the value of attribute id.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def id
@id
end
|
Returns the value of attribute media_type_namespace.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def media_type_namespace
@media_type_namespace
end
|
#name ⇒ Object
Returns the value of attribute name.
7
8
9
|
# File 'lib/hyperdrive/resource.rb', line 7
def name
@name
end
|
#namespace ⇒ Object
Returns the value of attribute namespace.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def namespace
@namespace
end
|
#params ⇒ Object
Returns the value of attribute params.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def params
@params
end
|
#request_handlers ⇒ Object
Returns the value of attribute request_handlers.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def request_handlers
@request_handlers
end
|
#version ⇒ Object
Returns the value of attribute version.
6
7
8
|
# File 'lib/hyperdrive/resource.rb', line 6
def version
@version
end
|
Instance Method Details
#acceptable_content_types(http_request_method) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/hyperdrive/resource.rb', line 69
def acceptable_content_types(http_request_method)
content_types = []
@config[:media_types].each do |media_type|
available_versions(http_request_method).each do |version|
content_types << "application/vnd.#{@config[:vendor]}.#{media_type_namespace}.#{version}+#{media_type}"
end
content_types << "application/vnd.#{@config[:vendor]}.#{media_type_namespace}+#{media_type}"
content_types << "application/vnd.#{@config[:vendor]}+#{media_type}"
end
content_types
end
|
#allowed_methods ⇒ Object
94
95
96
|
# File 'lib/hyperdrive/resource.rb', line 94
def allowed_methods
request_methods.values_at(*request_handlers.keys)
end
|
#available_versions(http_request_method) ⇒ Object
81
82
83
84
|
# File 'lib/hyperdrive/resource.rb', line 81
def available_versions(http_request_method)
request_method = http_request_methods[http_request_method]
@request_handlers[request_method].keys.sort.reverse
end
|
#callback(callback_type, http_request_method, version = nil) ⇒ Object
41
42
43
44
45
|
# File 'lib/hyperdrive/resource.rb', line 41
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
|
#has_callback?(callback_type, http_request_method, version = nil) ⇒ Boolean
47
48
49
50
51
|
# File 'lib/hyperdrive/resource.rb', line 47
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
|
#latest_version(http_request_method) ⇒ Object
86
87
88
|
# File 'lib/hyperdrive/resource.rb', line 86
def latest_version(http_request_method)
available_versions(http_request_method).first
end
|
#register_callback(callback_type, request_method, callback, version = 'v1') ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/hyperdrive/resource.rb', line 31
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
|
#register_filter(filter, description, options = {}) ⇒ Object
27
28
29
|
# File 'lib/hyperdrive/resource.rb', line 27
def register_filter(filter, description, options = {})
@filters[filter] = Filter.new(filter, description, options)
end
|
#register_param(param, description, options = {}) ⇒ Object
23
24
25
|
# File 'lib/hyperdrive/resource.rb', line 23
def register_param(param, description, options = {})
@params[param] = Param.new(param, description, options)
end
|
#register_request_handler(request_method, request_handler, version = 'v1') ⇒ Object
54
55
56
57
58
59
60
61
|
# File 'lib/hyperdrive/resource.rb', line 54
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] ||= {}
@request_handlers[:head].merge!({ version => @request_handlers[:get][version] })
end
end
|
#request_handler(http_request_method, version = nil) ⇒ Object
63
64
65
66
67
|
# File 'lib/hyperdrive/resource.rb', line 63
def request_handler(http_request_method, version = nil)
version ||= latest_version(http_request_method)
request_method = http_request_methods[http_request_method]
request_handlers[request_method][version]
end
|
#request_method_allowed?(http_request_method) ⇒ Boolean
90
91
92
|
# File 'lib/hyperdrive/resource.rb', line 90
def request_method_allowed?(http_request_method)
allowed_methods.include?(http_request_method)
end
|
#required?(param, http_request_method) ⇒ Boolean
108
109
110
|
# File 'lib/hyperdrive/resource.rb', line 108
def required?(param, http_request_method)
required_param?(param, http_request_method) or required_filter?(param, http_request_method)
end
|
#required_filter?(filter, http_request_method) ⇒ Boolean
103
104
105
106
|
# File 'lib/hyperdrive/resource.rb', line 103
def required_filter?(filter, http_request_method)
return false unless %w(GET HEAD OPTIONS).include? http_request_method
filters.key?(filter) and filters[filter].required?(http_request_method)
end
|
#required_param?(param, http_request_method) ⇒ Boolean
98
99
100
101
|
# File 'lib/hyperdrive/resource.rb', line 98
def required_param?(param, http_request_method)
return false if %w(GET HEAD OPTIONS).include? http_request_method
params.key?(param) and params[param].required?(http_request_method)
end
|
#to_hash ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/hyperdrive/resource.rb', line 112
def to_hash
{
_links: { 'self' => { href: endpoint } },
id: id,
name: name,
description: description,
methods: allowed_methods,
params: params.map { |_,param| param.to_hash },
filters: filters.map { |_,filter| filter.to_hash },
media_types: allowed_methods.map { |method| acceptable_content_types(method) }.uniq
}
end
|