Class: Sinatra::Request::AcceptEntry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ AcceptEntry

Returns a new instance of AcceptEntry.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sinatra/base.rb', line 91

def initialize(entry)
  params = entry.scan(HEADER_PARAM).map! do |s|
    key, value = s.strip.split('=', 2)
    value = value[1..-2].gsub(/\\(.)/, '\1') if value.start_with?('"')
    [key, value]
  end

  @entry  = entry
  @type   = entry[/[^;]+/].delete(' ')
  @params = params.to_h
  @q      = @params.delete('q') { 1.0 }.to_f
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



125
126
127
# File 'lib/sinatra/base.rb', line 125

def method_missing(*args, &block)
  to_str.send(*args, &block)
end

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



89
90
91
# File 'lib/sinatra/base.rb', line 89

def entry
  @entry
end

#paramsObject

Returns the value of attribute params.



88
89
90
# File 'lib/sinatra/base.rb', line 88

def params
  @params
end

Instance Method Details

#<=>(other) ⇒ Object



104
105
106
# File 'lib/sinatra/base.rb', line 104

def <=>(other)
  other.priority <=> priority
end

#priorityObject



108
109
110
111
# File 'lib/sinatra/base.rb', line 108

def priority
  # We sort in descending order; better matches should be higher.
  [@q, -@type.count('*'), @params.size]
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/sinatra/base.rb', line 121

def respond_to?(*args)
  super || to_str.respond_to?(*args)
end

#to_s(full = false) ⇒ Object



117
118
119
# File 'lib/sinatra/base.rb', line 117

def to_s(full = false)
  full ? entry : to_str
end

#to_strObject



113
114
115
# File 'lib/sinatra/base.rb', line 113

def to_str
  @type
end