Method: JSONRPC2::HTTPUtils.parse_accept
- Defined in:
- lib/jsonrpc2/accept.rb
.parse_accept(field, regex = false) ⇒ Object
Parses the HTTP Accept field and returns a sorted list of prefered types
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jsonrpc2/accept.rb', line 23 def parse_accept field, regex = false index = -1 list = field.split(/,\s*/).map do |media| index += 1 case media when /;/ media, param_str = *media.split(/\s*;\s*(?=q\s*=)/,2) params = param_str.to_s.split(/\s*;\s*/).inject({}) { |hash, str| k,v = *str.strip.split(/=/).map(&:strip) hash.merge(k => v) } { :q => (params['q'] || 1.0).to_f, :media => media, :index => index } else { :q => 1.0, :media => media, :index => index } end end.sort_by { |option| [-1 * option[:q], option[:media].scan(/[*]/).size, option[:index]] } final = {} list.each do |item| q = item[:q] final[q] ||= [] final[q].push(regex ? type_to_regex(item[:media]) : item[:media]) end final.sort_by { |k,v| -1 * k } end |