pebblebed 0.3.25 → 0.3.26

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54866fbc0a14546c0debe6bf4543d6a0adea572b
4
- data.tar.gz: ba9444ba7b2d6855afdde6b2e6c9d4780910cabb
3
+ metadata.gz: 14d1b62f07bf96068be143f86a02af3d0e9d3f86
4
+ data.tar.gz: 52004701d254aa4afd4855e4c0028ac9bf17bb29
5
5
  SHA512:
6
- metadata.gz: 16a35634df1b2d1a1f5b5c6c2e735310d5f688636d7cd595ee13e9a391b248a8700b807301042d4c902c753c1d8f7075b9742f9374b523c1f55112a9411ccb15
7
- data.tar.gz: 565c4b3095752c9154dc3416e2ac8d3de9a336587afe24b640f0e9d8d824146d41b13b6d3909d0160c6b6b62383db52d3e4b73e109a1ea2cc8e023bebbbba84b
6
+ metadata.gz: 24d3974b44099d97ef4dad54bfc50a7c13a4dabdd5b693a22da982e8653920c31c780644f7d2f6d9a06e3346c0794e748c15628d61566020a2d25b96ceee9a5b
7
+ data.tar.gz: 93b67975bc2cffb0f473f1a79a7e18e30d3dd745af5283e13b5220bafafcd9cfc68dbd21ce09cf407ea099c03c60d1467e7440f7aa1a44a4f08067190ceaab48
@@ -41,15 +41,27 @@ module Pebblebed
41
41
  DEFAULT_READ_TIMEOUT = 30
42
42
 
43
43
  class << self
44
- attr_accessor :connect_timeout, :request_timeout, :read_timeout
44
+ attr_reader :connect_timeout, :request_timeout, :read_timeout
45
+ def connect_timeout=(value)
46
+ @connect_timeout = value
47
+ self.current_easy = nil
48
+ end
49
+ def request_timeout=(value)
50
+ @request_timeout = value
51
+ self.current_easy = nil
52
+ end
53
+ def read_timeout=(value)
54
+ @read_timeout = value
55
+ self.current_easy = nil
56
+ end
45
57
  end
46
58
 
47
59
  class Response
48
- def initialize(easy)
49
- @body = easy.body_str
60
+ def initialize(url, header, body)
61
+ @body = body
50
62
  # We parse it ourselves because Curl::Easy fails when there's no text message
51
- @status = easy.header_str.scan(/HTTP\/\d\.\d\s(\d+)\s/).map(&:first).last.to_i
52
- @url = easy.url
63
+ @status = header.scan(/HTTP\/\d\.\d\s(\d+)\s/).map(&:first).last.to_i
64
+ @url = url
53
65
  end
54
66
 
55
67
  attr_reader :body, :status, :url
@@ -94,7 +106,7 @@ module Pebblebed
94
106
  end
95
107
 
96
108
  def self.stream_get(url = nil, params = nil, options = {})
97
- return do_easy { |easy|
109
+ return do_easy(cache: false) { |easy|
98
110
  on_data = options[:on_data] or raise "Option :on_data must be specified"
99
111
 
100
112
  url, params = url_and_params_from_args(url, params)
@@ -109,7 +121,7 @@ module Pebblebed
109
121
  end
110
122
 
111
123
  def self.stream_post(url, params, options = {})
112
- return do_easy { |easy|
124
+ return do_easy(cache: false) { |easy|
113
125
  on_data = options[:on_data] or raise "Option :on_data must be specified"
114
126
 
115
127
  url, params = url_and_params_from_args(url, params)
@@ -127,7 +139,7 @@ module Pebblebed
127
139
  end
128
140
 
129
141
  def self.stream_put(url, params, options = {})
130
- return do_easy { |easy|
142
+ return do_easy(cache: false) { |easy|
131
143
  on_data = options[:on_data] or raise "Option :on_data must be specified"
132
144
 
133
145
  url, params = url_and_params_from_args(url, params)
@@ -161,28 +173,54 @@ module Pebblebed
161
173
  def self.handle_http_errors(response)
162
174
  if response.status == 404
163
175
  errmsg = "Resource not found: '#{response.url}'"
164
- errmsg << extract_error_summary(response.body) if response.body
176
+ if (summary = extract_error_summary(response.body))
177
+ errmsg << ": #{summary}"
178
+ end
165
179
  # ActiveSupport::SafeBuffer.new is the same as errmsg.html_safe in rails
166
180
  raise HttpNotFoundError.new(ActiveSupport::SafeBuffer.new(errmsg), response.status)
167
- elsif response.status >= 400
168
- errmsg = "Service request to '#{response.url}' failed (#{response.status}):"
169
- errmsg << extract_error_summary(response.body) if response.body
181
+ end
182
+
183
+ if response.status >= 400
184
+ errmsg = "Service request to '#{response.url}' failed (#{response.status})"
185
+ if (summary = extract_error_summary(response.body))
186
+ errmsg << ": #{summary}"
187
+ end
170
188
  raise HttpError.new(ActiveSupport::SafeBuffer.new(errmsg), response.status, response)
171
189
  end
190
+
172
191
  response
173
192
  end
174
193
 
175
- def self.do_easy(&block)
176
- with_easy do |easy|
194
+ def self.do_easy(cache: true, &block)
195
+ with_easy(cache: cache) do |easy|
177
196
  yield easy
178
- return handle_http_errors(Response.new(easy))
197
+ response = Response.new(easy.url, easy.header_str, easy.body_str)
198
+ return handle_http_errors(response)
199
+ end
200
+ end
201
+
202
+ def self.with_easy(cache: true, &block)
203
+ if cache
204
+ easy = self.current_easy ||= new_easy
205
+ else
206
+ easy = new_easy
179
207
  end
208
+ yield easy
180
209
  end
181
210
 
182
- def self.with_easy(&block)
183
- yield new_easy
211
+ def self.current_easy
212
+ Thread.current[:pebblebed_curb_easy]
184
213
  end
185
214
 
215
+ def self.current_easy=(value)
216
+ if (current = Thread.current[:pebblebed_curb_easy])
217
+ # Reset old instance
218
+ current.reset
219
+ end
220
+ Thread.current[:pebblebed_curb_easy] = value
221
+ end
222
+
223
+ # Returns new Easy instance from current configuration.
186
224
  def self.new_easy
187
225
  easy = Curl::Easy.new
188
226
  easy.connect_timeout = connect_timeout || DEFAULT_CONNECT_TIMEOUT
@@ -210,11 +248,15 @@ module Pebblebed
210
248
  end
211
249
 
212
250
  def self.extract_error_summary(body)
213
- # Supports Sinatra error pages
214
- extract = Nokogiri::HTML(body).css('#summary').text.gsub(/\s+/, ' ').strip
215
- # TODO: Rails?
216
- return body if extract == ''
217
- extract
251
+ return nil unless body
252
+
253
+ # Hack to support Sinatra error pages
254
+ summary = Nokogiri::HTML(body).css('#summary').text.gsub(/\s+/, ' ').strip
255
+ return summary if summary.length > 0
256
+
257
+ summary = body
258
+ summary = summary[0, 500] + "..." if summary.length > 500
259
+ summary
218
260
  end
219
261
 
220
262
  end
@@ -1,3 +1,3 @@
1
1
  module Pebblebed
2
- VERSION = "0.3.25"
2
+ VERSION = "0.3.26"
3
3
  end
data/spec/http_spec.rb CHANGED
@@ -103,6 +103,19 @@ describe Pebblebed::Http do
103
103
  expect(result["QUERY_STRING"]).to eq "hello=world"
104
104
  expect(response.body).to eq nil
105
105
  end
106
+
107
+ it "supports multiple sequential streaming request" do
108
+ 10.times do
109
+ buf = ""
110
+ response = Pebblebed::Http.stream_get(pebble_url, {hello: 'world'},
111
+ on_data: ->(data) {
112
+ buf << data
113
+ })
114
+ result = JSON.parse(buf)
115
+ expect(result["QUERY_STRING"]).to eq "hello=world"
116
+ expect(response.body).to eq nil
117
+ end
118
+ end
106
119
  end
107
120
  end
108
121
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pebblebed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.25
4
+ version: 0.3.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katrina Owen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-12-21 00:00:00.000000000 Z
12
+ date: 2018-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -304,7 +304,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
304
304
  version: '0'
305
305
  requirements: []
306
306
  rubyforge_project: pebblebed
307
- rubygems_version: 2.5.1
307
+ rubygems_version: 2.5.2
308
308
  signing_key:
309
309
  specification_version: 4
310
310
  summary: Development tools for working with Pebblebed