Module: Rackful::Resource::ClassMethods

Defined in:
gems/rackful-0.1.1/lib/rackful_resource.rb,
gems/rackful-0.1.1.orig/lib/rackful_resource.rb

Overview

Since:

Instance Method Summary (collapse)

Instance Method Details

- (self) add_media_type(media_type, method = :PUT)

Meta-programmer method.

Examples:

Have your resource accept XML and JSON in `PUT` requests

class MyResource
  add_parser XML2MyResource, :PUT
  add_parser JSON2MyResource, :PUT
end

Parameters:

  • parser (Parser)
  • method (#to_sym) (defaults to: :PUT)

Returns:

  • (self)

Since:

  • 0.0.1



84
85
86
87
88
89
# File 'gems/rackful-0.1.1/lib/rackful_resource.rb', line 84

def add_media_type media_type, method = :PUT
  method = method.to_sym
  self.media_types[method] ||= []
  self.media_types[method] << media_type
  self
end

- (self) add_serializer(serializer, quality = 1.0)

Meta-programmer method.

Examples:

Have your resource rendered in XML and JSON

class MyResource
  add_serializer MyResource2XML
  add_serializer MyResource2JSON, 0.5
end

Parameters:

  • serializer (Serializer)
  • quality (Float) (defaults to: 1.0)

Returns:

  • (self)

Since:

  • 0.0.1



43
44
45
46
47
48
49
50
51
52
53
54
# File 'gems/rackful-0.1.1/lib/rackful_resource.rb', line 43

def add_serializer serializer, quality = 1.0
  quality = quality.to_f
  quality = 1.0 if quality > 1.0
  quality = 0.0 if quality < 0.0
  # The single '@' on the following line is on purpose!
  s = [serializer, quality]
  serializer::CONTENT_TYPES.each {
    |content_type|
    self.serializers[content_type.to_s] = s
  }
  self
end

- (void) all_media_types

Since:

  • 0.0.1



97
98
99
100
101
102
103
104
105
106
107
# File 'gems/rackful-0.1.1/lib/rackful_resource.rb', line 97

def all_media_types
  @rackful_resource_all_media_types ||=
    if self.superclass.respond_to?(:all_media_types)
      self.superclass.all_media_types.merge( self.media_types ) do
        |key, oldval, newval|
        oldval + newval
      end
    else
      self.media_types
    end
end

- (void) all_serializers

Since:

  • 0.0.1



62
63
64
65
66
67
68
69
70
71
72
# File 'gems/rackful-0.1.1/lib/rackful_resource.rb', line 62

def all_serializers
  @rackful_resource_all_serializers ||=
    if self.superclass.respond_to?(:all_serializers)
      self.superclass.all_serializers.merge( self.serializers ) do
        |key, oldval, newval|
        newval[1] >= oldval[1] ? newval : oldval
      end
    else
      self.serializers
    end
end

- (String) best_content_type(accept, require_match = true)

The best media type for the response body, given the current HTTP request.

Parameters:

  • accept (Hash)
  • require_match (Boolean) (defaults to: true)

Returns:

Raises:

Since:

  • 0.1.0



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'gems/rackful-0.1.1/lib/rackful_resource.rb', line 118

def best_content_type accept, require_match = true
  if accept.empty?
    return self.all_serializers.values.sort_by(&:last).last[0]::CONTENT_TYPES[0]
  end
  matches = []
  accept.each_pair {
    |accept_media_type, accept_quality|
    self.all_serializers.each_pair {
      |content_type, v|
      quality = v[1]
      media_type = content_type.split(';').first.strip
      if File.fnmatch( accept_media_type, media_type )
        matches << [ content_type, accept_quality * quality ]
      end
    }
  }
  if matches.empty?
    if require_match
      raise( HTTP406NotAcceptable, self.all_serializers.keys() )
    else
      return self.all_serializers.values.sort_by(&:last).last[0]::CONTENT_TYPES[0]
    end
  end
  matches.sort_by(&:last).last[0]
end

- (void) media_types

Since:

  • 0.0.1



92
93
94
# File 'gems/rackful-0.1.1/lib/rackful_resource.rb', line 92

def media_types
  @rackful_resource_media_types ||= {}
end

- (void) serializers

Since:

  • 0.0.1



57
58
59
# File 'gems/rackful-0.1.1/lib/rackful_resource.rb', line 57

def serializers
  @rackful_resource_serializers ||= {}
end