Class: Restfully::MediaType::AbstractMediaType

Inherits:
Object
  • Object
show all
Defined in:
lib/restfully/media_type/abstract_media_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, session) ⇒ AbstractMediaType

A MediaType instance takes the original io object and the current session object as input.



84
85
86
87
# File 'lib/restfully/media_type/abstract_media_type.rb', line 84

def initialize(io, session)
  @io = io
  @session = session
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



80
81
82
# File 'lib/restfully/media_type/abstract_media_type.rb', line 80

def io
  @io
end

#sessionObject (readonly)

Returns the value of attribute session.



80
81
82
# File 'lib/restfully/media_type/abstract_media_type.rb', line 80

def session
  @session
end

Class Method Details

.default_typeObject

Returns the media-type default signature.



43
44
45
# File 'lib/restfully/media_type/abstract_media_type.rb', line 43

def default_type
  signature.first
end

.defaultsHash

Returns the hash of default options set.

Returns:

  • (Hash)

    the hash of default options set.



22
23
24
# File 'lib/restfully/media_type/abstract_media_type.rb', line 22

def defaults
  @defaults ||= (parent(:defaults) || {}).dup
end

.parent(method) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/restfully/media_type/abstract_media_type.rb', line 13

def parent(method)
  if superclass.respond_to?(method)
    superclass.send(method)
  else
    nil
  end
end

.parserObject

Returns the media-type parser.



38
39
40
# File 'lib/restfully/media_type/abstract_media_type.rb', line 38

def parser
  defaults[:parser]
end

.serialize(object, *args) ⇒ Object

Serialize an object into a String. Calls parser#dump.



62
63
64
65
66
67
68
69
# File 'lib/restfully/media_type/abstract_media_type.rb', line 62

def serialize(object, *args)
  case object
  when String
    object
  else
    parser.dump(object, *args)
  end
end

.set(attribute, value) ⇒ Object

Sets a new value for a default attribute.



27
28
29
# File 'lib/restfully/media_type/abstract_media_type.rb', line 27

def set(attribute, value)
  defaults[attribute.to_sym] = value
end

.signatureObject

Returns the media-type signature, i.e. the list of metia-type it supports.



33
34
35
# File 'lib/restfully/media_type/abstract_media_type.rb', line 33

def signature
  [(defaults[:signature] || [])].flatten
end

.supports?(*types) ⇒ Boolean

Returns the first supported media-type of the signature that matches one of the given types.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
# File 'lib/restfully/media_type/abstract_media_type.rb', line 49

def supports?(*types)
  types.each do |type|
    type = type.to_s.downcase.split(";")[0]
    found = signature.find{ |s|
      type =~ Regexp.new(Regexp.escape(s.downcase).gsub('\*', ".*"))
    }
    return found if found
  end
  nil
end

.unserialize(io, *args) ⇒ Object

Unserialize an io object into a Hash. Calls parser#load.



73
74
75
# File 'lib/restfully/media_type/abstract_media_type.rb', line 73

def unserialize(io, *args)
  parser.load(io, *args)
end

Instance Method Details

Returns a string to display after the URI when pretty-printing objects.



109
110
111
# File 'lib/restfully/media_type/abstract_media_type.rb', line 109

def banner
  nil
end

#collection?Boolean

Returns true if the current io object must be handled as a Collection.

Should be overwritten.

Returns:

  • (Boolean)


138
139
140
# File 'lib/restfully/media_type/abstract_media_type.rb', line 138

def collection?
  false
end

#complete?Boolean

Returns true if the current io object is completely loaded. Overwrite and return false to force a reloading if your object is just

a URI reference.

Returns:

  • (Boolean)


145
146
147
# File 'lib/restfully/media_type/abstract_media_type.rb', line 145

def complete?
  true
end

#direct_fetch_uri(symbol) ⇒ Object

Returns a direct URI to a specific resource identified by symbol. Only called from collections, when doing collection. Either return a path if the media type supports direct fetching, or nil (default).



92
93
94
# File 'lib/restfully/media_type/abstract_media_type.rb', line 92

def direct_fetch_uri(symbol)
  nil
end

#each(*args, &block) ⇒ Object

How to iterate over a collection

Should be overwritten.

Raises:



159
160
161
# File 'lib/restfully/media_type/abstract_media_type.rb', line 159

def each(*args, &block)
  raise NotImplemented
end

Returns an array of Link objects. Do not overwrite directly. Overwrite #extract_links instead.



98
99
100
# File 'lib/restfully/media_type/abstract_media_type.rb', line 98

def links
  @links ||= extract_links.select{|l| l.valid?}
end

#metaObject

An object to display on Resource#inspect.

Should be overwritten.



152
153
154
# File 'lib/restfully/media_type/abstract_media_type.rb', line 152

def meta
  property
end

#property(key = nil) ⇒ Object

Without argument, returns the properties Hash obtained from calling #unserialized. With an argument, returns the value corresponding to that key.

Should be overwritten if required.



118
119
120
121
122
123
124
125
# File 'lib/restfully/media_type/abstract_media_type.rb', line 118

def property(key = nil)
  @properties ||= unserialized
  if key
    @properties[key]
  else
    @properties
  end
end

#represents?(id) ⇒ Boolean

Should return true if the current resource represented by this media-type can be designated with id.

Should be overwritten.

Returns:

  • (Boolean)


131
132
133
# File 'lib/restfully/media_type/abstract_media_type.rb', line 131

def represents?(id)
  false
end

#unserializedObject

Returns the unserialized version of the io object.



103
104
105
# File 'lib/restfully/media_type/abstract_media_type.rb', line 103

def unserialized
  @unserialized ||= self.class.unserialize(@io)
end