httparty 0.13.0 → 0.13.1

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

Potentially problematic release.


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

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e2e263bc6000ec32578c99ef814398a9742be7a7
4
- data.tar.gz: 9f8eb0a8ddb0100669e3f08c86fbde96b7c538d7
3
+ metadata.gz: 3ce26387d700f484cf57204fb8257bee96a79694
4
+ data.tar.gz: 6ba64cf5e2c870f84860b29b17fa1699480754d1
5
5
  SHA512:
6
- metadata.gz: 4e02f9c9aab76af35b66bc0421d7322357a3a180f55cb50434259ce907d9cb536adaef681312fdee37648d8b558731c1c003d5bb57caa0000a187d24377449b3
7
- data.tar.gz: 39286a8c98a2375f4fa60c98fb0ec3a5dd8828760029fcdefa054acce0da38cc20eb113cfbb6759a6df3f7ffaf7a9d62021c17ddb599318cf557ab70fdfedfad
6
+ metadata.gz: 727c7ec279835747b7da0fc3173fd7d3b260677f8eadb1a80251bbb093eb92f9e3fb999af539a55467b55aa3d119b78182497289b03cabf43026f8cdc594d900
7
+ data.tar.gz: 015463cb6bb232327e126308257dccff62d08cde88eb7dd57730f9dad28626e685464dc030c0ca3b4048424316ae97676b0ea6738ca74541daf5777bf0aeff17
data/README.md CHANGED
@@ -18,37 +18,31 @@ gem install httparty
18
18
 
19
19
  ```ruby
20
20
  # Use the class methods to get down to business quickly
21
- response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
22
- puts response.body, response.code, response.message, response.headers.inspect
21
+ response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
23
22
 
24
- response.each do |item|
25
- puts item['user']['screen_name']
26
- end
23
+ puts response.body, response.code, response.message, response.headers.inspect
27
24
 
28
25
  # Or wrap things up in your own class
29
- class Twitter
26
+ class StackExchange
30
27
  include HTTParty
31
- base_uri 'twitter.com'
28
+ base_uri 'api.stackexchange.com'
32
29
 
33
- def initialize(u, p)
34
- @auth = {:username => u, :password => p}
30
+ def initialize(service, page)
31
+ @options = { :query => {:site => service, :page => page} }
35
32
  end
36
33
 
37
- # which can be :friends, :user or :public
38
- # options[:query] can be things like since, since_id, count, etc.
39
- def timeline(which=:friends, options={})
40
- options.merge!({:basic_auth => @auth})
41
- self.class.get("/statuses/#{which}_timeline.json", options)
34
+ def questions
35
+ self.class.get("/2.2/questions", @options)
42
36
  end
43
37
 
44
- def post(text)
45
- options = { :body => {:status => text}, :basic_auth => @auth }
46
- self.class.post('/statuses/update.json', options)
38
+ def users
39
+ self.class.get("/2.2/users", @options)
47
40
  end
48
41
  end
49
42
 
50
- twitter = Twitter.new(config['email'], config['password'])
51
- pp twitter.timeline
43
+ stack_exchange = StackExchange.new("stackoverflow", 1)
44
+ puts stack_exchange.questions
45
+ puts stack_exchange.users
52
46
  ```
53
47
 
54
48
  See the [examples directory](http://github.com/jnunemaker/httparty/tree/master/examples) for even more goodies.
@@ -63,7 +57,7 @@ formatted XML or JSON. Execute `httparty --help` for all the
63
57
  options. Below is an example of how easy it is.
64
58
 
65
59
  ```
66
- httparty "http://twitter.com/statuses/public_timeline.json"
60
+ httparty "https://api.stackexchange.com/2.2/questions?site=stackoverflow"
67
61
  ```
68
62
 
69
63
  ## Help and Docs
@@ -3,13 +3,9 @@ require File.join(dir, 'httparty')
3
3
  require 'pp'
4
4
 
5
5
  # You can also use post, put, delete, head, options in the same fashion
6
- response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
6
+ response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
7
7
  puts response.body, response.code, response.message, response.headers.inspect
8
8
 
9
- response.each do |item|
10
- puts item['user']['screen_name']
11
- end
12
-
13
9
  # An example post to a minimal rails app in the development environment
14
10
  # Note that "skip_before_filter :verify_authenticity_token" must be set in the
15
11
  # "pears" controller for this example
@@ -0,0 +1,24 @@
1
+ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require File.join(dir, 'httparty')
3
+ require 'pp'
4
+
5
+ class StackExchange
6
+ include HTTParty
7
+ base_uri 'api.stackexchange.com'
8
+
9
+ def initialize(service, page)
10
+ @options = { :query => {:site => service, :page => page} }
11
+ end
12
+
13
+ def questions
14
+ self.class.get("/2.2/questions", @options)
15
+ end
16
+
17
+ def users
18
+ self.class.get("/2.2/users", @options)
19
+ end
20
+ end
21
+
22
+ stack_exchange = StackExchange.new("stackoverflow", 1)
23
+ pp stack_exchange.questions
24
+ pp stack_exchange.users
@@ -2,6 +2,14 @@ When /^I set my HTTParty timeout option to (\d+)$/ do |timeout|
2
2
  @request_options[:timeout] = timeout.to_i
3
3
  end
4
4
 
5
+ When /^I set my HTTParty open_timeout option to (\d+)$/ do |timeout|
6
+ @request_options[:open_timeout] = timeout.to_i
7
+ end
8
+
9
+ When /^I set my HTTParty read_timeout option to (\d+)$/ do |timeout|
10
+ @request_options[:read_timeout] = timeout.to_i
11
+ end
12
+
5
13
  When /I call HTTParty#get with '(.*)'$/ do |url|
6
14
  begin
7
15
  @response_from_httparty = HTTParty.get("http://#{@host_and_port}#{url}", @request_options)
@@ -0,0 +1,13 @@
1
+ Feature: Supports the read timeout option
2
+ In order to handle inappropriately slow response times
3
+ As a developer
4
+ I want my request to raise an exception after my specified read_timeout as elapsed
5
+
6
+ Scenario: A long running response
7
+ Given a remote service that returns '<h1>Some HTML</h1>'
8
+ And that service is accessed at the path '/long_running_service.html'
9
+ And that service takes 2 seconds to generate a response
10
+ When I set my HTTParty read_timeout option to 1
11
+ And I call HTTParty#get with '/long_running_service.html'
12
+ Then it should raise a Timeout::Error exception
13
+ And I wait for the server to recover
@@ -176,6 +176,28 @@ module HTTParty
176
176
  default_options[:timeout] = t
177
177
  end
178
178
 
179
+ # Allows setting a default open_timeout for all HTTP calls in seconds
180
+ #
181
+ # class Foo
182
+ # include HTTParty
183
+ # open_timeout 10
184
+ # end
185
+ def open_timeout(t)
186
+ raise ArgumentError, 'open_timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
187
+ default_options[:open_timeout] = t
188
+ end
189
+
190
+ # Allows setting a default read_timeout for all HTTP calls in seconds
191
+ #
192
+ # class Foo
193
+ # include HTTParty
194
+ # read_timeout 10
195
+ # end
196
+ def read_timeout(t)
197
+ raise ArgumentError, 'read_timeout must be an integer or float' unless t && (t.is_a?(Integer) || t.is_a?(Float))
198
+ default_options[:read_timeout] = t
199
+ end
200
+
179
201
  # Set an output stream for debugging, defaults to $stderr.
180
202
  # The output stream is passed on to Net::HTTP#set_debug_output.
181
203
  #
@@ -38,13 +38,16 @@ module HTTParty
38
38
  # in the #options attribute. It is up to you to interpret them within your
39
39
  # connection adapter. Take a look at the implementation of
40
40
  # HTTParty::ConnectionAdapter#connection for examples of how they are used.
41
- # Something are probably interesting are as follows:
41
+ # Some things that are probably interesting are as follows:
42
42
  # * :+timeout+: timeout in seconds
43
+ # * :+open_timeout+: http connection open_timeout in seconds, overrides timeout if set
44
+ # * :+read_timeout+: http connection read_timeout in seconds, overrides timeout if set
43
45
  # * :+debug_output+: see HTTParty::ClassMethods.debug_output.
44
46
  # * :+pem+: contains pem data. see HTTParty::ClassMethods.pem.
47
+ # * :+verify+: verify the server’s certificate against the ca certificate.
45
48
  # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file.
46
49
  # * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path.
47
- # * :+connection_adapter_options+: contains the hash your passed to HTTParty.connection_adapter when you configured your connection adapter
50
+ # * :+connection_adapter_options+: contains the hash you passed to HTTParty.connection_adapter when you configured your connection adapter
48
51
  class ConnectionAdapter
49
52
 
50
53
  # Private: Regex used to strip brackets from IPv6 URIs.
@@ -81,6 +84,14 @@ module HTTParty
81
84
  http.read_timeout = options[:timeout]
82
85
  end
83
86
 
87
+ if options[:read_timeout] && (options[:read_timeout].is_a?(Integer) || options[:read_timeout].is_a?(Float))
88
+ http.read_timeout = options[:read_timeout]
89
+ end
90
+
91
+ if options[:open_timeout] && (options[:open_timeout].is_a?(Integer) || options[:open_timeout].is_a?(Float))
92
+ http.open_timeout = options[:open_timeout]
93
+ end
94
+
84
95
  if options[:debug_output]
85
96
  http.set_debug_output(options[:debug_output])
86
97
  end
@@ -43,7 +43,7 @@ module HTTParty
43
43
  end
44
44
 
45
45
  def path=(uri)
46
- @path = URI.parse(uri)
46
+ @path = URI(uri)
47
47
  end
48
48
 
49
49
  def request_uri(uri)
@@ -148,6 +148,7 @@ module HTTParty
148
148
  def setup_raw_request
149
149
  @raw_request = http_method.new(request_uri(uri))
150
150
  @raw_request.body = body if body
151
+ @raw_request.body_stream = options[:body_stream] if options[:body_stream]
151
152
  @raw_request.initialize_http_header(options[:headers])
152
153
  @raw_request.basic_auth(username, password) if options[:basic_auth]
153
154
  setup_digest_auth if options[:digest_auth]
@@ -1,3 +1,3 @@
1
1
  module HTTParty
2
- VERSION = "0.13.0"
2
+ VERSION = "0.13.1"
3
3
  end
@@ -153,6 +153,62 @@ describe HTTParty::ConnectionAdapter do
153
153
  end
154
154
  end
155
155
 
156
+ context "when timeout is not set and read_timeout is set to 6 seconds" do
157
+ let(:options) { {:read_timeout => 6} }
158
+
159
+ its(:read_timeout) { should == 6 }
160
+
161
+ it "should not set the open_timeout" do
162
+ http = mock("http", :null_object => true)
163
+ http.should_not_receive(:open_timeout=)
164
+ Net::HTTP.stub(:new => http)
165
+ adapter.connection
166
+ end
167
+ end
168
+
169
+ context "when timeout is set and read_timeout is set to 6 seconds" do
170
+ let(:options) { {:timeout => 5, :read_timeout => 6} }
171
+
172
+ its(:open_timeout) { should == 5 }
173
+ its(:read_timeout) { should == 6 }
174
+
175
+ it "should override the timeout option" do
176
+ http = mock("http", :null_object => true)
177
+ http.should_receive(:open_timeout=)
178
+ http.should_receive(:read_timeout=).twice
179
+ Net::HTTP.stub(:new => http)
180
+ adapter.connection
181
+ end
182
+ end
183
+
184
+ context "when timeout is not set and open_timeout is set to 7 seconds" do
185
+ let(:options) { {:open_timeout => 7} }
186
+
187
+ its(:open_timeout) { should == 7 }
188
+
189
+ it "should not set the read_timeout" do
190
+ http = mock("http", :null_object => true)
191
+ http.should_not_receive(:read_timeout=)
192
+ Net::HTTP.stub(:new => http)
193
+ adapter.connection
194
+ end
195
+ end
196
+
197
+ context "when timeout is set and open_timeout is set to 7 seconds" do
198
+ let(:options) { {:timeout => 5, :open_timeout => 7} }
199
+
200
+ its(:open_timeout) { should == 7 }
201
+ its(:read_timeout) { should == 5 }
202
+
203
+ it "should override the timeout option" do
204
+ http = mock("http", :null_object => true)
205
+ http.should_receive(:open_timeout=).twice
206
+ http.should_receive(:read_timeout=)
207
+ Net::HTTP.stub(:new => http)
208
+ adapter.connection
209
+ end
210
+ end
211
+
156
212
  context "when debug_output" do
157
213
  let(:http) { Net::HTTP.new(uri) }
158
214
  before do
@@ -117,6 +117,13 @@ describe HTTParty::Request do
117
117
  @post_request.options[:digest_auth] = {:username => 'foobar', :password => 'secret'}
118
118
  @post_request.send(:setup_raw_request)
119
119
  end
120
+
121
+ it 'should use body_stream when configured' do
122
+ stream = StringIO.new('foo')
123
+ request = HTTParty::Request.new(Net::HTTP::Post, 'http://api.foo.com/v1', body_stream: stream)
124
+ request.send(:setup_raw_request)
125
+ request.instance_variable_get(:@raw_request).body_stream.should == stream
126
+ end
120
127
  end
121
128
 
122
129
  describe "#uri" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httparty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-14 00:00:00.000000000 Z
12
+ date: 2014-04-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -66,6 +66,7 @@ files:
66
66
  - examples/headers_and_user_agents.rb
67
67
  - examples/nokogiri_html_parser.rb
68
68
  - examples/rubyurl.rb
69
+ - examples/stackexchange.rb
69
70
  - examples/tripit_sign_in.rb
70
71
  - examples/twitter.rb
71
72
  - examples/whoismyrep.rb
@@ -80,6 +81,7 @@ files:
80
81
  - features/steps/httparty_steps.rb
81
82
  - features/steps/mongrel_helper.rb
82
83
  - features/steps/remote_service_steps.rb
84
+ - features/supports_read_timeout_option.feature
83
85
  - features/supports_redirection.feature
84
86
  - features/supports_timeout_option.feature
85
87
  - httparty.gemspec
@@ -170,6 +172,7 @@ test_files:
170
172
  - features/steps/httparty_steps.rb
171
173
  - features/steps/mongrel_helper.rb
172
174
  - features/steps/remote_service_steps.rb
175
+ - features/supports_read_timeout_option.feature
173
176
  - features/supports_redirection.feature
174
177
  - features/supports_timeout_option.feature
175
178
  - spec/fixtures/delicious.xml