~ubuntu-branches/ubuntu/wily/ruby-excon/wily

« back to all changes in this revision

Viewing changes to lib/excon.rb

  • Committer: Package Import Robot
  • Author(s): Praveen Arimbrathodiyil
  • Date: 2013-05-16 20:49:12 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130516204912-bngjq57uhy40lzzq
Tags: 0.21.0-1
* Team upload
* New upstream release
* std version bunped to 3.9.4
* Update build dependencies (gem2deb (>= 0.3.0~), rake, ruby-rspec, 
  ruby-chronic, ruby-shindo, ruby-sinatra, ruby-open4, ruby-activesupport,
   ruby-delorean, ruby-eventmachine)
* debian/patches/02_remove_gemfile_lock.patch: Remove Gemfile.lock
* debian/patches/03_remove_rubygems_bundler_add_requires_from_gemspec.patch:
  Remove rubygems and bundler, add require lines instead of Bundler.require

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
require 'openssl'
7
7
require 'rbconfig'
8
8
require 'socket'
 
9
require 'timeout'
9
10
require 'uri'
10
11
 
11
 
require 'excon/constants'
12
 
require 'excon/connection'
13
 
require 'excon/errors'
14
 
require 'excon/response'
15
 
require 'excon/socket'
16
 
require 'excon/ssl_socket'
17
 
 
 
12
# Define defaults first so they will be available to other files
18
13
module Excon
19
14
  class << self
20
15
 
21
16
    # @return [Hash] defaults for Excon connections
22
17
    def defaults
23
18
      @defaults ||= {
 
19
        :chunk_size         => CHUNK_SIZE || DEFAULT_CHUNK_SIZE,
24
20
        :connect_timeout    => 60,
25
21
        :headers            => {},
 
22
        :idempotent         => false,
26
23
        :instrumentor_name  => 'excon',
 
24
        :middlewares        => [
 
25
          Excon::Middleware::Expects,
 
26
          Excon::Middleware::Idempotent,
 
27
          Excon::Middleware::Instrumentor,
 
28
          Excon::Middleware::Mock
 
29
        ],
27
30
        :mock               => false,
 
31
        :nonblock           => DEFAULT_NONBLOCK,
28
32
        :read_timeout       => 60,
29
33
        :retry_limit        => DEFAULT_RETRY_LIMIT,
30
34
        :ssl_ca_file        => DEFAULT_CA_FILE,
31
35
        :ssl_verify_peer    => RbConfig::CONFIG['host_os'] !~ /mswin|win32|dos|cygwin|mingw/i,
 
36
        :uri_parser         => URI,
32
37
        :write_timeout      => 60
33
38
      }
34
39
    end
39
44
      @defaults = new_defaults
40
45
    end
41
46
 
 
47
  end
 
48
end
 
49
 
 
50
require 'excon/constants'
 
51
require 'excon/connection'
 
52
require 'excon/errors'
 
53
require 'excon/middlewares/base'
 
54
require 'excon/middlewares/expects'
 
55
require 'excon/middlewares/idempotent'
 
56
require 'excon/middlewares/instrumentor'
 
57
require 'excon/middlewares/mock'
 
58
require 'excon/response'
 
59
require 'excon/socket'
 
60
require 'excon/ssl_socket'
 
61
require 'excon/standard_instrumentor'
 
62
 
 
63
module Excon
 
64
  class << self
 
65
 
42
66
    # Status of mocking
43
67
    def mock
44
 
      puts("Excon#mock is deprecated, pass Excon.defaults[:mock] instead (#{caller.first})")
 
68
      $stderr.puts("Excon#mock is deprecated, pass Excon.defaults[:mock] instead (#{caller.first})")
45
69
      self.defaults[:mock]
46
70
    end
47
71
 
49
73
    # false is the default and works as expected
50
74
    # true returns a value from stubs or raises
51
75
    def mock=(new_mock)
52
 
      puts("Excon#mock is deprecated, pass Excon.defaults[:mock]= instead (#{caller.first})")
 
76
      $stderr.puts("Excon#mock is deprecated, pass Excon.defaults[:mock]= instead (#{caller.first})")
53
77
      self.defaults[:mock] = new_mock
54
78
    end
55
79
 
56
80
    # @return [String] The filesystem path to the SSL Certificate Authority
57
81
    def ssl_ca_path
58
 
      puts("Excon#ssl_ca_path is deprecated, use Excon.defaults[:ssl_ca_path] instead (#{caller.first})")
 
82
      $stderr.puts("Excon#ssl_ca_path is deprecated, use Excon.defaults[:ssl_ca_path] instead (#{caller.first})")
59
83
      self.defaults[:ssl_ca_path]
60
84
    end
61
85
 
62
86
    # Change path to the SSL Certificate Authority
63
87
    # @return [String] The filesystem path to the SSL Certificate Authority
64
88
    def ssl_ca_path=(new_ssl_ca_path)
65
 
      puts("Excon#ssl_ca_path= is deprecated, use Excon.defaults[:ssl_ca_path]= instead (#{caller.first})")
 
89
      $stderr.puts("Excon#ssl_ca_path= is deprecated, use Excon.defaults[:ssl_ca_path]= instead (#{caller.first})")
66
90
      self.defaults[:ssl_ca_path] = new_ssl_ca_path
67
91
    end
68
92
 
69
93
    # @return [true, false] Whether or not to verify the peer's SSL certificate / chain
70
94
    def ssl_verify_peer
71
 
      puts("Excon#ssl_verify_peer= is deprecated, use Excon.defaults[:ssl_verify_peer]= instead (#{caller.first})")
 
95
      $stderr.puts("Excon#ssl_verify_peer= is deprecated, use Excon.defaults[:ssl_verify_peer]= instead (#{caller.first})")
72
96
      self.defaults[:ssl_verify_peer]
73
97
    end
74
98
 
75
99
    # Change the status of ssl peer verification
76
100
    # @see Excon#ssl_verify_peer (attr_reader)
77
101
    def ssl_verify_peer=(new_ssl_verify_peer)
78
 
      puts("Excon#ssl_verify_peer is deprecated, use Excon.defaults[:ssl_verify_peer] instead (#{caller.first})")
 
102
      $stderr.puts("Excon#ssl_verify_peer is deprecated, use Excon.defaults[:ssl_verify_peer] instead (#{caller.first})")
79
103
      self.defaults[:ssl_verify_peer] = new_ssl_verify_peer
80
104
    end
81
105
 
85
109
    #   @param [Hash<Symbol, >] params One or more option params to set on the Connection instance
86
110
    #   @return [Connection] A new Excon::Connection instance
87
111
    def new(url, params = {})
88
 
      Excon::Connection.new(url, params)
 
112
      uri_parser = params[:uri_parser] || Excon.defaults[:uri_parser]
 
113
      uri = uri_parser.parse(url)
 
114
      params = {
 
115
        :host       => uri.host,
 
116
        :path       => uri.path,
 
117
        :port       => uri.port.to_s,
 
118
        :query      => uri.query,
 
119
        :scheme     => uri.scheme,
 
120
        :user       => (URI.decode(uri.user) if uri.user),
 
121
        :password   => (URI.decode(uri.password) if uri.password),
 
122
      }.merge!(params)
 
123
      Excon::Connection.new(params)
89
124
    end
90
125
 
91
126
    # push an additional stub onto the list to check for mock requests
92
127
    #   @param [Hash<Symbol, >] request params to match against, omitted params match all
93
128
    #   @param [Hash<Symbol, >] response params to return from matched request or block to call with params
94
 
    def stub(request_params, response_params = nil)
 
129
    def stub(request_params = {}, response_params = nil)
 
130
      if url = request_params.delete(:url)
 
131
        uri = URI.parse(url)
 
132
        request_params.update(
 
133
          :host              => uri.host,
 
134
          :path              => uri.path,
 
135
          :port              => uri.port.to_s,
 
136
          :query             => uri.query,
 
137
          :scheme            => uri.scheme
 
138
        )
 
139
        if uri.user || uri.password
 
140
          request_params[:headers] ||= {}
 
141
          user, pass = URI.decode_www_form_component(uri.user.to_s), URI.decode_www_form_component(uri.password.to_s)
 
142
          request_params[:headers]['Authorization'] ||= 'Basic ' << ['' << user << ':' << pass].pack('m').delete(Excon::CR_NL)
 
143
        end
 
144
      end
95
145
      if block_given?
96
146
        if response_params
97
147
          raise(ArgumentError.new("stub requires either response_params OR a block"))
103
153
      else
104
154
        raise(ArgumentError.new("stub requires either response_params OR a block"))
105
155
      end
106
 
      stubs << stub
 
156
      stubs.unshift(stub)
107
157
      stub
108
158
    end
109
159
 
 
160
    # get a stub matching params or nil
 
161
    def stub_for(request_params={})
 
162
      Excon.stubs.each do |stub, response|
 
163
        captures = { :headers => {} }
 
164
        headers_match = !stub.has_key?(:headers) || stub[:headers].keys.all? do |key|
 
165
          case value = stub[:headers][key]
 
166
          when Regexp
 
167
            if match = value.match(request_params[:headers][key])
 
168
              captures[:headers][key] = match.captures
 
169
            end
 
170
            match
 
171
          else
 
172
            value == request_params[:headers][key]
 
173
          end
 
174
        end
 
175
        non_headers_match = (stub.keys - [:headers]).all? do |key|
 
176
          case value = stub[key]
 
177
          when Regexp
 
178
            if match = value.match(request_params[key])
 
179
              captures[key] = match.captures
 
180
            end
 
181
            match
 
182
          else
 
183
            value == request_params[key]
 
184
          end
 
185
        end
 
186
        if headers_match && non_headers_match
 
187
          request_params[:captures] = captures
 
188
          return response
 
189
        end
 
190
      end
 
191
      nil
 
192
    end
 
193
 
110
194
    # get a list of defined stubs
111
195
    def stubs
112
196
      @stubs ||= []
114
198
 
115
199
    # Generic non-persistent HTTP methods
116
200
    HTTP_VERBS.each do |method|
117
 
      eval <<-DEF
 
201
      module_eval <<-DEF, __FILE__, __LINE__ + 1
118
202
        def #{method}(url, params = {}, &block)
119
 
          new(url).request(params.merge!(:method => :#{method}), &block)
 
203
          new(url, params).request(:method => :#{method}, &block)
120
204
        end
121
205
      DEF
122
206
    end