Class: Garage::HypermediaFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/garage/hypermedia_filter.rb

Constant Summary collapse

MIME_DICT =
%r[application/vnd\.cookpad\.dictionary\+(json|x-msgpack)]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ HypermediaFilter

Returns a new instance of HypermediaFilter.



16
17
18
# File 'lib/garage/hypermedia_filter.rb', line 16

def initialize(controller)
  @controller = controller
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



14
15
16
# File 'lib/garage/hypermedia_filter.rb', line 14

def controller
  @controller
end

Class Method Details

.before(controller) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/garage/hypermedia_filter.rb', line 5

def self.before(controller)
  helper = new(controller)
  controller.representation = helper.dictionary_representation if helper.has_dictionary_mime_type?
  controller.request.format = helper.dictionary_request_format if helper.has_dictionary_mime_type?
  controller.field_selector = helper.field_selector
rescue Garage::NestedFieldQuery::InvalidQuery
  raise Garage::BadRequest, "Invalid query in ?fields="
end

Instance Method Details

#dictionary_match_dataObject



40
41
42
# File 'lib/garage/hypermedia_filter.rb', line 40

def dictionary_match_data
  @dictionary_match_data ||= controller.request.format.to_s.match(MIME_DICT)
end

#dictionary_representationObject



28
29
30
# File 'lib/garage/hypermedia_filter.rb', line 28

def dictionary_representation
  :dictionary
end

#dictionary_request_formatObject



32
33
34
# File 'lib/garage/hypermedia_filter.rb', line 32

def dictionary_request_format
  dictionary_match_data[1].sub(/^x-/, "").to_sym
end

#field_selectorObject



20
21
22
# File 'lib/garage/hypermedia_filter.rb', line 20

def field_selector
  Garage::NestedFieldQuery::Selector.build(fields_param)
end

#fields_paramObject



24
25
26
# File 'lib/garage/hypermedia_filter.rb', line 24

def fields_param
  controller.params[:fields]
end

#has_dictionary_mime_type?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/garage/hypermedia_filter.rb', line 36

def has_dictionary_mime_type?
  dictionary_match_data
end