C0 code coverage information
Generated on Fri Jul 11 15:55:34 -0700 2008 with rcov 0.7.0
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 module OpenID
2
3 module Yadis
4
5 # Generate an accept header value
6 #
7 # [str or (str, float)] -> str
8 def self.generate_accept_header(*elements)
9 parts = []
10 elements.each { |element|
11 if element.is_a?(String)
12 qs = "1.0"
13 mtype = element
14 else
15 mtype, q = element
16 q = q.to_f
17 if q > 1 or q <= 0
18 raise ArgumentError.new("Invalid preference factor: #{q}")
19 end
20 qs = sprintf("%0.1f", q)
21 end
22
23 parts << [qs, mtype]
24 }
25
26 parts.sort!
27 chunks = []
28 parts.each { |q, mtype|
29 if q == '1.0'
30 chunks << mtype
31 else
32 chunks << sprintf("%s; q=%s", mtype, q)
33 end
34 }
35
36 return chunks.join(', ')
37 end
38
39 def self.parse_accept_header(value)
40 # Parse an accept header, ignoring any accept-extensions
41 #
42 # returns a list of tuples containing main MIME type, MIME
43 # subtype, and quality markdown.
44 #
45 # str -> [(str, str, float)]
46 chunks = value.split(',', -1).collect { |v| v.strip }
47 accept = []
48 chunks.each { |chunk|
49 parts = chunk.split(";", -1).collect { |s| s.strip }
50
51 mtype = parts.shift
52 if mtype.index('/').nil?
53 # This is not a MIME type, so ignore the bad data
54 next
55 end
56
57 main, sub = mtype.split('/', 2)
58
59 q = nil
60 parts.each { |ext|
61 if !ext.index('=').nil?
62 k, v = ext.split('=', 2)
63 if k == 'q'
64 q = v.to_f
65 end
66 end
67 }
68
69 q = 1.0 if q.nil?
70
71 accept << [q, main, sub]
72 }
73
74 accept.sort!
75 accept.reverse!
76
77 return accept.collect { |q, main, sub| [main, sub, q] }
78 end
79
80 def self.match_types(accept_types, have_types)
81 # Given the result of parsing an Accept: header, and the
82 # available MIME types, return the acceptable types with their
83 # quality markdowns.
84 #
85 # For example:
86 #
87 # >>> acceptable = parse_accept_header('text/html, text/plain; q=0.5')
88 # >>> matchTypes(acceptable, ['text/plain', 'text/html', 'image/jpeg'])
89 # [('text/html', 1.0), ('text/plain', 0.5)]
90 #
91 # Type signature: ([(str, str, float)], [str]) -> [(str, float)]
92 if accept_types.nil? or accept_types == []
93 # Accept all of them
94 default = 1
95 else
96 default = 0
97 end
98
99 match_main = {}
100 match_sub = {}
101 accept_types.each { |main, sub, q|
102 if main == '*'
103 default = [default, q].max
104 next
105 elsif sub == '*'
106 match_main[main] = [match_main.fetch(main, 0), q].max
107 else
108 match_sub[[main, sub]] = [match_sub.fetch([main, sub], 0), q].max
109 end
110 }
111
112 accepted_list = []
113 order_maintainer = 0
114 have_types.each { |mtype|
115 main, sub = mtype.split('/', 2)
116 if match_sub.member?([main, sub])
117 q = match_sub[[main, sub]]
118 else
119 q = match_main.fetch(main, default)
120 end
121
122 if q != 0
123 accepted_list << [1 - q, order_maintainer, q, mtype]
124 order_maintainer += 1
125 end
126 }
127
128 accepted_list.sort!
129 return accepted_list.collect { |_, _, q, mtype| [mtype, q] }
130 end
131
132 def self.get_acceptable(accept_header, have_types)
133 # Parse the accept header and return a list of available types
134 # in preferred order. If a type is unacceptable, it will not be
135 # in the resulting list.
136 #
137 # This is a convenience wrapper around matchTypes and
138 # parse_accept_header
139 #
140 # (str, [str]) -> [str]
141 accepted = self.parse_accept_header(accept_header)
142 preferred = self.match_types(accepted, have_types)
143 return preferred.collect { |mtype, _| mtype }
144 end
145
146 end
147
148 end
Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.