add parser configuration to http transport, will add tests next commit
[arvados.git] / lib / google / api_client / transport / http_transport.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 module Google #:nodoc:
16   class APIClient #:nodoc:
17       ##
18       # Factory for HTTP backed client requests.
19     class HTTPTransport
20
21       ##
22       # The default transport configuration values.  These may be overridden
23       # simply by passing in the same key to the constructor.
24       DEFAULTS = {
25         :parser => :json_parser
26       }
27
28       ##
29       # The default implementations of various parsers.  These may be overriden
30       # simply by passing the same key to the constructor.
31       PARSERS = {
32         :json_parser => JSONParser.new
33       }
34
35       ##
36       # Creates a new HTTP request factory.
37       #
38       # @param [Hash] options
39       # @return [Google::APIClient::Discovery] The HTTP request factory.
40       def initialize(options={})
41         @options = DEFAULTS.clone
42         @options.merge!(options)
43
44         # first check if user passed a parser then fallback on appropriate default
45         @parser = @options[@options[:parser]] || PARSERS[@options[:parser]]
46         unless @parser
47           raise ArgumentError,
48             'Invalid :parser configuration.'
49         end
50       end
51
52       ##
53       # Returns configuration of the transport.
54       #
55       # @return [Hash] The configuration options.
56       def options
57         return @options
58       end
59
60       ##
61       # Returns the parser used by the transport.
62       #
63       # @return The handle to the parser.
64       def parser
65        return @parser
66       end
67     end
68   end
69 end