Fixed some YARD documentation issues and changed how the user-agent is built.
[arvados.git] / lib / google / api_client / parser.rb
1 # Copyright 2010 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 require 'json'
17
18
19 module Google
20   class APIClient
21     module Parser
22       def content_type(content_type)
23         @@content_type_mapping ||= {}
24         @@content_type_mapping[content_type] = self
25       end
26
27       def self.match_content_type(content_type)
28         # TODO(bobaman): Do this more efficiently.
29         mime_type_regexp = /^([^\/]+)(?:\/([^+]+\+)?([^;]+))?(?:;.*)?$/
30         if @@content_type_mapping[content_type]
31           # Exact match
32           return @@content_type_mapping[content_type]
33         else
34           media_type, extension, sub_type =
35             content_type.scan(mime_type_regexp)[0]
36           for pattern, parser in @@content_type_mapping
37             # We want to match on subtype first
38             pattern_media_type, pattern_extension, pattern_sub_type =
39               pattern.scan(mime_type_regexp)[0]
40             next if pattern_extension != nil
41             if media_type == pattern_media_type && sub_type == pattern_sub_type
42               return parser
43             end
44           end
45           for pattern, parser in @@content_type_mapping
46             # We failed to match on the subtype
47             # Try to match only on the media type
48             pattern_media_type, pattern_extension, pattern_sub_type =
49               pattern.scan(mime_type_regexp)[0]
50             next if pattern_extension != nil || pattern_sub_type != nil
51             if media_type == pattern_media_type
52               return parser
53             end
54           end
55         end
56         return nil
57       end
58     end
59   end
60 end