excon 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of excon might be problematic. Click here for more details.

@@ -4,35 +4,58 @@ Http(s) EXtended CONnections
4
4
 
5
5
  == Getting Started
6
6
 
7
- sudo gem install excon
7
+ Install the gem.
8
8
 
9
- Now you are ready to get started using one off requests.
9
+ $ sudo gem install excon
10
+
11
+ Require with rubygems.
10
12
 
11
13
  require 'rubygems'
12
14
  require 'excon'
13
- Excon.get('http://geemus.com')
14
15
 
15
- The returned response object has #body, #headers and #status attributes. Supported one off requests are #connect, #delete, #get, #head, #options, #post, #put, and #trace
16
+ The simplest way to use excon is with one-off requests:
17
+
18
+ response = Excon.get('http://geemus.com')
19
+
20
+ Supported one off request methods are #connect, #delete, #get, #head, #options, #post, #put, and #trace.
16
21
 
17
- You can also create a connection to reuse across multiple requests (more performant!).
22
+ The returned response object has #body, #headers and #status attributes.
23
+
24
+ Alternately you can create a connection object which is reusable across multiple requests (more performant!).
18
25
 
19
26
  connection = Excon.new('http://geemus.com')
20
- connection.request(:method => 'GET')
27
+ response_one = connection.request(:method => 'GET')
28
+ response_two = connection.request(:method => 'POST', :path => '/foo')
29
+ response_three = connection.request(:method => 'DELETE', :path => '/bar')
21
30
 
22
- Both one off and persistent connections also support many other options, for example:
31
+ Both one-off and persistent connections support many other options. Here are a few common examples:
23
32
 
33
+ # Custom headers
24
34
  Excon.get('http://geemus.com', :headers => {'Authorization' => 'Basic 0123456789ABCDEF'})
25
- # or
26
35
  connection.request(:method => 'GET', :headers => {'Authorization' => 'Basic 0123456789ABCDEF'})
36
+
37
+ # Changing query strings
38
+ connection = Excon.new('http://geemus.com/')
39
+ connection.request(:method => 'GET', :query => {:foo => 'bar'})
40
+
41
+ # POST body
42
+ Excon.post('http://geemus.com', :body => 'language=ruby&class=fog')
43
+
44
+ # Symbols can also be used for HTTP methods
45
+ connection.request(:method => :get)
27
46
 
28
- You can also stream responses by passing a block that will receive each chunk.
47
+ These options can be combined to make pretty much any request you might need.
48
+
49
+ == Streaming Responses
50
+
51
+ You can stream responses by passing a block that will receive each chunk.
29
52
 
30
53
  Excon.get('http://geemus.com') do |chunk, remaining_bytes, total_bytes|
31
54
  puts chunk
32
55
  puts "Remaining: #{remaining_bytes.to_f / total_bytes}%"
33
56
  end
34
-
35
- These options can be combined to make pretty much any request you might need.
57
+
58
+ Iterating over each chunk will allow you to do work on the response incrementally without buffering the entire response first. For very large responses this can lead to significant memory savings.
36
59
 
37
60
  == Proxy Support
38
61
 
@@ -43,29 +66,44 @@ You can specify a proxy URL that Excon will use with both HTTP and HTTPS connect
43
66
 
44
67
  The proxy URL must be fully specified, including scheme (e.g. "http://") and port.
45
68
 
46
- Excon will also use the environment variables "http_proxy" and "https_proxy" if they are present. "http_proxy" will be used for HTTPS connections if "https_proxy" is not set.
69
+ Excon will also use the environment variables "http_proxy" and "https_proxy" if they are present.
70
+
71
+ If "https_proxy" is not set, the value of "http_proxy" will be used for both HTTP and HTTPS connections.
72
+
73
+ NOTE: Environment variables will take precedence over a :proxy option that has been specified in code.
74
+
75
+ == Stubs
76
+
77
+ You can stub out requests for testing purposes by enabling mock mode on a connection.
78
+
79
+ Excon.mock = true
80
+
81
+ Then you can add stubs, for instance:
82
+
83
+ # Excon.stub(request_attributes, response_attributes)
84
+ Excon.stub({:method => :get}, {:body => 'body', :status => 200})
47
85
 
48
- Environment variables will take precedence over a :proxy option that has been specified in code.
86
+ Omitted attributes are assumed to match, so this stub will match any get request and return an Excon::Response with a body of 'body' and status of 200. You can add whatever stubs you might like this way and they will be checked against in the order they were added, if none of them match then excon will raise an error to let you know.
49
87
 
50
88
  == HTTPS/SSL Issues
51
89
 
52
- By default excon will try to verify peer certificates when using ssl for https, unfortunately on some operating systems the defaults will not work. This will likely manifest itself as something like "Excon::Errors::SocketError: SSL_connect returned=1 ..."
90
+ By default excon will try to verify peer certificates when using SSL for HTTPS. Unfortunately on some operating systems the defaults will not work. This will likely manifest itself as something like "Excon::Errors::SocketError: SSL_connect returned=1 ..."
53
91
 
54
- If you have the misfortune of running into this problem you have a couple options. First, if you have certificates in a location that defaults fail to locate you can set a different path to certificates:
92
+ If you have the misfortune of running into this problem you have a couple options. If you have certificates but they aren't being auto-discovered, you can specify the path to your certificates:
55
93
 
56
94
  Excon.ssl_ca_path = '/path/to/certs'
57
95
 
58
- or failing that, you can turn off peer verification (less secure):
96
+ Failing that, you can turn off peer verification (less secure):
59
97
 
60
98
  Excon.ssl_verify_peer = false
61
99
 
62
- Either of these should allow you to work around the socket error and continue with getting your work done.
100
+ Either of these should allow you to work around the socket error and continue with your work.
63
101
 
64
102
  == Copyright
65
103
 
66
104
  (The MIT License)
67
105
 
68
- Copyright (c) 2010 {geemus (Wesley Beary)}[http://github.com/geemus]
106
+ Copyright (c) 2010-2011 {geemus (Wesley Beary)}[http://github.com/geemus]
69
107
 
70
108
  Permission is hereby granted, free of charge, to any person obtaining
71
109
  a copy of this software and associated documentation files (the
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'excon'
16
- s.version = '0.6.4'
17
- s.date = '2011-07-05'
16
+ s.version = '0.6.5'
17
+ s.date = '2011-07-13'
18
18
  s.rubyforge_project = 'excon'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -13,7 +13,7 @@ require 'excon/response'
13
13
 
14
14
  module Excon
15
15
  unless const_defined?(:VERSION)
16
- VERSION = '0.6.4'
16
+ VERSION = '0.6.5'
17
17
  end
18
18
 
19
19
  unless const_defined?(:CHUNK_SIZE)
@@ -78,20 +78,23 @@ module Excon
78
78
  # all specified non-headers params match and no headers were specified or all specified headers match
79
79
  if (stub.keys - [:headers]).all? {|key| stub[key] == params[key] } &&
80
80
  (!stub.has_key?(:headers) || stub[:headers].keys.all? {|key| stub[:headers][key] == params[:headers][key]})
81
- if block_given?
82
- body = response.delete(:body)
81
+ response_attributes = case response
82
+ when Proc
83
+ response.call(params)
84
+ else
85
+ response
86
+ end
87
+ if block_given? && response_attributes.has_key?(:body)
88
+ body = response_attributes.delete(:body)
89
+ content_length = remaining = body.bytesize
83
90
  i = 0
84
91
  while i < body.length
85
- yield body[i, CHUNK_SIZE]
92
+ yield(body[i, CHUNK_SIZE], [remaining - CHUNK_SIZE, 0].max, content_length)
93
+ remaining -= CHUNK_SIZE
86
94
  i += CHUNK_SIZE
87
95
  end
88
96
  end
89
- case response
90
- when Proc
91
- return Excon::Response.new(response.call(params))
92
- else
93
- return Excon::Response.new(response)
94
- end
97
+ return Excon::Response.new(response_attributes)
95
98
  end
96
99
  end
97
100
  # if we reach here no stubs matched
@@ -33,6 +33,14 @@ Shindo.tests('Excon stubs') do
33
33
  response.status
34
34
  end
35
35
 
36
+ tests('request body with block given').returns('body') do
37
+ body = ''
38
+ connection.request(:method => :get, :path => '/content-length/100') do |chunk, remaining_bytes, total_bytes|
39
+ body << chunk
40
+ end
41
+ body
42
+ end
43
+
36
44
  Excon.stubs.pop
37
45
 
38
46
  end
@@ -56,6 +64,14 @@ Shindo.tests('Excon stubs') do
56
64
  response.status
57
65
  end
58
66
 
67
+ tests('request body with block given').returns('body') do
68
+ body = ''
69
+ connection.request(:body => 'body', :method => :get, :path => '/content-length/100') do |chunk, remaining_bytes, total_bytes|
70
+ body << chunk
71
+ end
72
+ body
73
+ end
74
+
59
75
  Excon.stubs.pop
60
76
 
61
77
  end
@@ -71,7 +87,7 @@ Shindo.tests('Excon stubs') do
71
87
 
72
88
  test("with block") do
73
89
  chunks = []
74
- response = connection.request(:method => :get, :path => '/content-length/100') do |chunk|
90
+ response = connection.request(:method => :get, :path => '/content-length/100') do |chunk, remaining_bytes, total_bytes|
75
91
  chunks << chunk
76
92
  end
77
93
  chunks == ['x' * Excon::CHUNK_SIZE, 'x']
@@ -40,6 +40,14 @@ def basic_tests
40
40
  tests("response.body").returns('x' * 100) do
41
41
  response.body
42
42
  end
43
+
44
+ tests("block usage").returns(['x' * 100, 0, 100]) do
45
+ data = []
46
+ connection.request(:method => :get, :path => '/content-length/100') do |chunk, remaining_length, total_length|
47
+ data = [chunk, remaining_length, total_length]
48
+ end
49
+ data
50
+ end
43
51
  end
44
52
  end
45
53
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 4
10
- version: 0.6.4
9
+ - 5
10
+ version: 0.6.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - geemus (Wesley Beary)
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-05 00:00:00 -05:00
19
- default_executable:
18
+ date: 2011-07-13 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: open4
@@ -124,7 +123,6 @@ files:
124
123
  - tests/stub_tests.rb
125
124
  - tests/test_helper.rb
126
125
  - tests/thread_safety_tests.rb
127
- has_rdoc: true
128
126
  homepage: https://github.com/geemus/excon
129
127
  licenses: []
130
128
 
@@ -154,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
152
  requirements: []
155
153
 
156
154
  rubyforge_project: excon
157
- rubygems_version: 1.4.1
155
+ rubygems_version: 1.8.5
158
156
  signing_key:
159
157
  specification_version: 2
160
158
  summary: speed, persistence, http(s)