Class: Negotiator
- Inherits:
-
Object
- Object
- Negotiator
- Defined in:
- lib/negotiator.rb
Constant Summary collapse
- VERSION =
"0.1"
Class Method Summary collapse
Class Method Details
.parse(h) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/negotiator.rb', line 35 def self.parse(h) if !h h = "*/*" end qual = [{}, {}, 0.0] p = h.split(/,\s*/) for s in p t, st, q = parse_media_range(s) case true when t == "*" qual[2] = q when st == "*" qual[1][t] = q else qual[0]["#{t}/#{st}"] = q end end qual end |
.parse_media_range(s) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/negotiator.rb', line 57 def self.parse_media_range(s) q = 1.0 if s[";"] s, qs = s.split(/;\s*/) if qs.index("q=") == 0 q = Float(qs[2,qs.length]) end end t, st = s.split(/\//) return t, st, q end |
.pick(h, o) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/negotiator.rb', line 4 def self.pick(h, o) r = parse(h) cand = [] for s, q in o st = s.split(/\//)[0] case true when r[0].include?(s) q *= r[0][s] when r[1].include?(st) q *= r[1][st] else q *= r[2] end if q > 0 cand << [s, q] end end if cand.empty? return nil end best = cand[0] for s, q in cand if q > best[1] best = [s, q] end end return best[0] end |