]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/aviator/feature/faraday.rb
4b63ec8e9f99d40c2ca75d9f396d8efe573dcb29
[dsa-puppet.git] / 3rdparty / modules / aviator / feature / faraday.rb
1 require 'thread'
2 require 'cgi'
3 require 'set'
4 require 'forwardable'
5
6 # Public: This is the main namespace for Faraday.  You can either use it to
7 # create Faraday::Connection objects, or access it directly.
8 #
9 # Examples
10 #
11 #   Faraday.get "http://faraday.com"
12 #
13 #   conn = Faraday.new "http://faraday.com"
14 #   conn.get '/'
15 #
16 module Faraday
17   VERSION = "0.9.0"
18
19   class << self
20     # Public: Gets or sets the root path that Faraday is being loaded from.
21     # This is the root from where the libraries are auto-loaded from.
22     attr_accessor :root_path
23
24     # Public: Gets or sets the path that the Faraday libs are loaded from.
25     attr_accessor :lib_path
26
27     # Public: Gets or sets the Symbol key identifying a default Adapter to use
28     # for the default Faraday::Connection.
29     attr_reader :default_adapter
30
31     # Public: Sets the default Faraday::Connection for simple scripts that
32     # access the Faraday constant directly.
33     #
34     #     Faraday.get "https://faraday.com"
35     attr_writer :default_connection
36
37     # Public: Sets the default options used when calling Faraday#new.
38     attr_writer :default_connection_options
39
40     # Public: Initializes a new Faraday::Connection.
41     #
42     # url     - The optional String base URL to use as a prefix for all
43     #           requests.  Can also be the options Hash.
44     # options - The optional Hash used to configure this Faraday::Connection.
45     #           Any of these values will be set on every request made, unless
46     #           overridden for a specific request.
47     #           :url     - String base URL.
48     #           :params  - Hash of URI query unencoded key/value pairs.
49     #           :headers - Hash of unencoded HTTP header key/value pairs.
50     #           :request - Hash of request options.
51     #           :ssl     - Hash of SSL options.
52     #           :proxy   - Hash of Proxy options.
53     #
54     # Examples
55     #
56     #   Faraday.new 'http://faraday.com'
57     #
58     #   # http://faraday.com?page=1
59     #   Faraday.new 'http://faraday.com', :params => {:page => 1}
60     #
61     #   # same
62     #
63     #   Faraday.new :url => 'http://faraday.com',
64     #     :params => {:page => 1}
65     #
66     # Returns a Faraday::Connection.
67     def new(url = nil, options = nil)
68       block = block_given? ? Proc.new : nil
69       options = options ? default_connection_options.merge(options) : default_connection_options.dup
70       Faraday::Connection.new(url, options, &block)
71     end
72
73     # Internal: Requires internal Faraday libraries.
74     #
75     # *libs - One or more relative String names to Faraday classes.
76     #
77     # Returns nothing.
78     def require_libs(*libs)
79       libs.each do |lib|
80         require "#{lib_path}/#{lib}"
81       end
82     end
83
84     # Public: Updates default adapter while resetting
85     # #default_connection.
86     #
87     # Returns the new default_adapter.
88     def default_adapter=(adapter)
89       @default_connection = nil
90       @default_adapter = adapter
91     end
92
93     alias require_lib require_libs
94
95   private
96     # Internal: Proxies method calls on the Faraday constant to
97     # #default_connection.
98     def method_missing(name, *args, &block)
99       default_connection.send(name, *args, &block)
100     end
101   end
102
103   self.root_path = File.expand_path "..", __FILE__
104   self.lib_path = File.expand_path "../faraday", __FILE__
105   self.default_adapter = :net_http
106
107   # Gets the default connection used for simple scripts.
108   #
109   # Returns a Faraday::Connection, configured with the #default_adapter.
110   def self.default_connection
111     @default_connection ||= Connection.new
112   end
113
114   # Gets the default connection options used when calling Faraday#new.
115   #
116   # Returns a Faraday::ConnectionOptions.
117   def self.default_connection_options
118     @default_connection_options ||= ConnectionOptions.new
119   end
120
121   if (!defined?(RUBY_ENGINE) || "ruby" == RUBY_ENGINE) && RUBY_VERSION < '1.9'
122     begin
123       require 'system_timer'
124       Timer = SystemTimer
125     rescue LoadError
126       warn "Faraday: you may want to install system_timer for reliable timeouts"
127     end
128   end
129
130   unless const_defined? :Timer
131     require 'timeout'
132     Timer = Timeout
133   end
134
135   # Public: Adds the ability for other modules to register and lookup
136   # middleware classes.
137   module MiddlewareRegistry
138     # Public: Register middleware class(es) on the current module.
139     #
140     # mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
141     #           as fully qualified constant, or a Proc that will be lazily
142     #           called to return the former.
143     #
144     # Examples
145     #
146     #   module Faraday
147     #     class Whatever
148     #       # Middleware looked up by :foo returns Faraday::Whatever::Foo.
149     #       register_middleware :foo => Foo
150     #
151     #       # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
152     #       register_middleware :bar => :Bar
153     #
154     #       # Middleware looked up by :baz requires 'baz' and returns Faraday::Whatever.const_get(:Baz)
155     #       register_middleware :baz => [:Baz, 'baz']
156     #     end
157     #   end
158     #
159     # Returns nothing.
160     def register_middleware(autoload_path = nil, mapping = nil)
161       if mapping.nil?
162         mapping = autoload_path
163         autoload_path = nil
164       end
165       middleware_mutex do
166         @middleware_autoload_path = autoload_path if autoload_path
167         (@registered_middleware ||= {}).update(mapping)
168       end
169     end
170
171     # Public: Lookup middleware class with a registered Symbol shortcut.
172     #
173     # key - The Symbol key for the registered middleware.
174     #
175     # Examples
176     #
177     #   module Faraday
178     #     class Whatever
179     #       register_middleware :foo => Foo
180     #     end
181     #   end
182     #
183     #   Faraday::Whatever.lookup_middleware(:foo)
184     #   # => Faraday::Whatever::Foo
185     #
186     # Returns a middleware Class.
187     def lookup_middleware(key)
188       load_middleware(key) ||
189         raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
190     end
191
192     def middleware_mutex(&block)
193       @middleware_mutex ||= begin
194         require 'monitor'
195         Monitor.new
196       end
197       @middleware_mutex.synchronize(&block)
198     end
199
200     def fetch_middleware(key)
201       defined?(@registered_middleware) && @registered_middleware[key]
202     end
203
204     def load_middleware(key)
205       value = fetch_middleware(key)
206       case value
207       when Module
208         value
209       when Symbol, String
210         middleware_mutex do
211           @registered_middleware[key] = const_get(value)
212         end
213       when Proc
214         middleware_mutex do
215           @registered_middleware[key] = value.call
216         end
217       when Array
218         middleware_mutex do
219           const, path = value
220           if root = @middleware_autoload_path
221             path = "#{root}/#{path}"
222           end
223           require(path)
224           @registered_middleware[key] = const
225         end
226         load_middleware(key)
227       end
228     end
229   end
230
231   def self.const_missing(name)
232     if name.to_sym == :Builder
233       warn "Faraday::Builder is now Faraday::RackBuilder."
234       const_set name, RackBuilder
235     else
236       super
237     end
238   end
239
240   require_libs "utils", "options", "connection", "rack_builder", "parameters",
241     "middleware", "adapter", "request", "response", "upload_io", "error"
242
243   if !ENV["FARADAY_NO_AUTOLOAD"]
244     require_lib 'autoload'
245   end
246 end
247
248 # not pulling in active-support JUST for this method.  And I love this method.
249 class Object
250   # The primary purpose of this method is to "tap into" a method chain,
251   # in order to perform operations on intermediate results within the chain.
252   #
253   # Examples
254   #
255   #   (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
256   #     tap    { |x| puts "array: #{x.inspect}" }.
257   #     select { |x| x%2 == 0 }.
258   #     tap    { |x| puts "evens: #{x.inspect}" }.
259   #     map    { |x| x*x }.
260   #     tap    { |x| puts "squares: #{x.inspect}" }
261   #
262   # Yields self.
263   # Returns self.
264   def tap
265     yield(self)
266     self
267   end unless Object.respond_to?(:tap)
268 end