Added some tests for determining the discovery document URI.
[arvados.git] / lib / google / api_client / discovery / discovery.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 require "json"
16 require "addressable/template"
17
18 module Google #:nodoc:
19   class APIClient #:nodoc:
20     ##
21     # A discovery document handler.
22     class Discovery
23       ##
24       # The default discovery configuration values.  These may be overrided
25       # simply by passing in the same key to the constructor.
26       DEFAULTS = {
27       }
28       
29       ##
30       # A set of default configuration values specific to each service.  These
31       # may be overrided simply by passing in the same key to the constructor.
32       SERVICE_DEFAULTS = {
33       }
34
35       ##
36       # Creates a new API discovery handler.
37       #
38       # @param [Hash] options
39       #   <code>:service</code>::
40       #     The name of the service.
41       #   <code>:service_version</code>::
42       #     The version of the service.
43       #   <code>:discovery_uri</code>::
44       #     The URI of the discovery document.
45       #
46       # @return [Google::APIClient::Discovery] The API discovery handler.
47       def initialize(options={})
48         if options[:service] && SERVICE_DEFAULTS[options[:service]]
49           @options = DEFAULTS.merge(SERVICE_DEFAULTS[options[:service]])
50         else
51           @options = DEFAULTS.clone
52         end
53         @options.merge!(options)
54         if @options[:service] && !@options[:discovery_uri]
55           service_id = @options[:service]
56           service_version = @options[:service_version] || "1.0"
57           @options[:discovery_uri] =
58             "http://www.googleapis.com/discovery/0.1/describe" +
59             "?api=#{service_id}&apiVersion=#{service_version}"
60         end
61         unless @options[:discovery_uri]
62           raise ArgumentError,
63             "Missing required configuration value, :discovery_uri."
64         end
65         # Handle any remaining configuration here
66       end
67
68       ##
69       # Returns the configuration of the handler.  Configuration options that
70       # are not recognized by the handler are ignored.
71       #
72       # @return [Hash] The configuration options.
73       def options
74         return @options
75       end
76       
77       ##
78       # Returns the URI of the discovery document.
79       #
80       # @return [String] The URI of the discovery document.
81       def discovery_uri
82         return @options[:discovery_uri]
83       end
84     end
85   end
86 end