Module: Restfully::MediaType

Defined in:
lib/restfully/media_type.rb,
lib/restfully/media_type/grid5000.rb,
lib/restfully/media_type/wildcard.rb,
lib/restfully/media_type/application_json.rb,
lib/restfully/media_type/abstract_media_type.rb,
lib/restfully/media_type/application_vnd_bonfire_xml.rb,
lib/restfully/media_type/application_x_www_form_urlencoded.rb

Defined Under Namespace

Classes: AbstractMediaType, ApplicationJson, ApplicationVndBonfireXml, ApplicationXWwwFormUrlencoded, Grid5000, Wildcard

Class Method Summary collapse

Class Method Details

.build_indexObject



57
58
59
60
61
62
# File 'lib/restfully/media_type.rb', line 57

def build_index
  # @index = []
  # catalog.each do |media_type|
  #
  # end
end

.catalogObject



6
7
8
# File 'lib/restfully/media_type.rb', line 6

def catalog
  @catalog ||= Set.new
end

.find(*types) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/restfully/media_type.rb', line 10

def find(*types)
  return nil if types.compact.empty?

  found = {}

  catalog.each{ |media_type|
    match = media_type.supports?(*types)
    found[match] = media_type unless match.nil?
  }
  
  if found.empty?
    nil
  else
    found.sort{|a, b| a[0].length <=> b[0].length }.last[1]
  end
end

.register(media_type) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/restfully/media_type.rb', line 34

def register(media_type)
  if media_type.signature.empty?
    raise ArgumentError, "The given MediaType (#{media_type}) has no signature"
  end
  if media_type.parser.nil?
    raise ArgumentError, "The given MediaType (#{media_type}) has no parser"
  end
  unregister(media_type).catalog.add(media_type)
end

.resetObject

Reset the catalog to the default list of media types



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/restfully/media_type.rb', line 45

def reset
  %w{
    wildcard
    application_json
    application_x_www_form_urlencoded
    grid5000
  }.each do |m|
    require "restfully/media_type/#{m}"
    register MediaType.const_get(m.camelize)
  end
end

.unregister(media_type) ⇒ Object



28
29
30
31
32
# File 'lib/restfully/media_type.rb', line 28

def unregister(media_type)
  catalog.delete(media_type)
  build_index
  self
end