Class: Hyperdrive::Resource

Inherits:
Object
  • Object
show all
Includes:
Values
Defined in:
lib/hyperdrive/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#callbacksObject (readonly)

Returns the value of attribute callbacks.



6
7
8
# File 'lib/hyperdrive/resource.rb', line 6

def callbacks
  @callbacks
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/hyperdrive/resource.rb', line 7

def description
  @description
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



6
7
8
# File 'lib/hyperdrive/resource.rb', line 6

def endpoint
  @endpoint
end

#filtersObject (readonly)

Returns the value of attribute filters.



6
7
8
# File 'lib/hyperdrive/resource.rb', line 6

def filters
  @filters
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/hyperdrive/resource.rb', line 6

def id
  @id
end

#media_type_namespaceObject (readonly)

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

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/hyperdrive/resource.rb', line 7

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/hyperdrive/resource.rb', line 6

def namespace
  @namespace
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/hyperdrive/resource.rb', line 6

def params
  @params
end

#request_handlersObject (readonly)

Returns the value of attribute request_handlers.



6
7
8
# File 'lib/hyperdrive/resource.rb', line 6

def request_handlers
  @request_handlers
end

#versionObject (readonly)

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_methodsObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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_hashObject



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