Class: Safrano::Request::AcceptEntry

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

Overview

borowed from Sinatra

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ AcceptEntry

Returns a new instance of AcceptEntry.



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/safrano/request.rb', line 72

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 = Hash[params]
  @q = @params.delete('q') { 1.0 }.to_f
end

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



70
71
72
# File 'lib/safrano/request.rb', line 70

def entry
  @entry
end

#paramsObject

Returns the value of attribute params.



69
70
71
# File 'lib/safrano/request.rb', line 69

def params
  @params
end

Instance Method Details

#<=>(other) ⇒ Object



85
86
87
# File 'lib/safrano/request.rb', line 85

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

#priorityObject



89
90
91
92
# File 'lib/safrano/request.rb', line 89

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

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/safrano/request.rb', line 94

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

#to_strObject



98
99
100
# File 'lib/safrano/request.rb', line 98

def to_str
  @type
end