Class: Atom::MediaType

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

Overview

MediaType

Class represents MediaType

Accessors

feed = Atom::MediaType.new 'application/atom+xml;type=feed'
puts feed.type               # application
puts feed.subtype            # atom+xml
puts feed.subtype_major      # xml
puts feed.without_parameters # application/atom+xml
puts feed.parameters         # type=feed
puts feed.to_s               # application/atom+xml;type=feed

Equivalence

feed2 = Atom::MediaType.new 'application/atom+xml;type=feed'
entry = Atom::MediaType.new 'application/atom+xml;type=entry'
feed == feed2 # -> true
feed == entry # -> false
feed == 'application/atom+xml;type=feed' # -> true

Constants

Major media types for atom syndication format are already prepared. Use following constants for them.

Atom::MediaType::SERVICE

application/atomsvc+xml

Atom::MediaType::CATEGORIES

application/atomcat+xml

Atom::MediaType::FEED

application/atom+xml;type=feed

Atom::MediaType::ENTRY

application/atom+xml;type=entry

Constant Summary collapse

SERVICE =
self.new 'application/atomsvc+xml'
CATEGORIES =
self.new 'application/atomcat+xml'
FEED =
self.new 'application/atom+xml;type=feed'
ENTRY =
self.new 'application/atom+xml;type=entry'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ MediaType

:nodoc:



186
187
188
189
190
191
# File 'lib/atomutil.rb', line 186

def initialize(type) #:nodoc:
  result = type.split(%r<[/;]>)
  @type       = result[0]
  @subtype    = result[1]
  @parameters = result[2]
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



185
186
187
# File 'lib/atomutil.rb', line 185

def parameters
  @parameters
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



185
186
187
# File 'lib/atomutil.rb', line 185

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



185
186
187
# File 'lib/atomutil.rb', line 185

def type
  @type
end

Instance Method Details

#==(value) ⇒ Object



205
206
207
208
209
210
211
# File 'lib/atomutil.rb', line 205

def ==(value)
  if value.is_a?(MediaType)
    to_s == value.to_s
  else
    to_s == value
  end
end

#is_a?(value) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
# File 'lib/atomutil.rb', line 213

def is_a?(value)
  value = self.class.new value unless value.instance_of?(self.class)
  return true  if     value.type == '*'
  return false unless value.type == @type
  return true  if     value.subtype == '*'
  return false unless value.subtype == @subtype
  return true  if     value.parameters.nil? || @parameters.nil? 
  return value.parameters == @parameters
end

#subtype_majorObject



193
194
195
# File 'lib/atomutil.rb', line 193

def subtype_major
  @subtype =~ /\+(.+)/ ? $1 : @subtype
end

#to_sObject



201
202
203
# File 'lib/atomutil.rb', line 201

def to_s
  [without_parameters, @parameters].select{ |p| !p.nil? }.join(";")
end

#without_parametersObject



197
198
199
# File 'lib/atomutil.rb', line 197

def without_parameters
  "#{@type}/#{@subtype}"
end