dagger 1.9.0 → 2.0.0

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
  SHA256:
3
- metadata.gz: 692b25152c7fd3229ad3dc13bd25250b613e55a211e489b8835258b18405db19
4
- data.tar.gz: 5560f64e580e4f72f9e243eaf1c5eed1b389e439074607dc24bdd8fbe9c532f0
3
+ metadata.gz: 447d77e66ec69445f5040231d19c038279d4ec7de624461deda078c544423ca9
4
+ data.tar.gz: c5eb814e71fd6a37b3b4272f6f747654eeb157655dec65ae6ec6f0cf86d4e5ff
5
5
  SHA512:
6
- metadata.gz: 85d160f0130c12fde8b2723cd54bcaf4a75c4f48319cb040c4725a93e6cc09d4f8a68a7977135f33c38922583cc8de481bc078ea6d3ff5636c42c3154a5dca74
7
- data.tar.gz: 9bd29716a9e84b75e898f71b00e8fc145550fcccd15b2c0006172e56e3e5454b62668bc4536dacde371a33b429f437a942354368de4cce0394ff73296cefa8e8
6
+ metadata.gz: 31108057fbc3cd1d2e0c319d4c64244c596cbbb90d8ae6067945caedb2f8f7ff5c0fd144b16be55ac24cd36bbd466f08659b995b47ecd67425d26224bd505ede
7
+ data.tar.gz: d996eaefa87c30682eb127d9f2f28cbb28363fcf2a2f758e32610d553937c05a35750316f043a6f4d2a9eb6727aad8f0351ca761ee62b326313b46e902764fd2
@@ -1,6 +1,6 @@
1
1
  module Dagger
2
- MAJOR = 1
3
- MINOR = 9
2
+ MAJOR = 2
3
+ MINOR = 0
4
4
  PATCH = 0
5
5
 
6
6
  VERSION = [MAJOR, MINOR, PATCH].join('.')
data/lib/dagger.rb CHANGED
@@ -42,18 +42,28 @@ module Dagger
42
42
  def self.resolve_uri(uri, host = nil, query = nil)
43
43
  uri = host + uri if uri[0] == '/' && host
44
44
  uri = parse_uri(uri.to_s)
45
- uri.path.sub!(/\?.*|$/, '?' + Utils.encode(query)) if query and query.any?
45
+ uri.path.sub!(/\?.*|$/, '?' + to_query_string(query)) if query and query.any?
46
46
  uri
47
47
  end
48
48
 
49
- def self.encode(obj, key = nil)
49
+ def self.encode_body(obj, opts = {})
50
+ if obj.is_a?(String)
51
+ obj
52
+ elsif opts[:json]
53
+ Oj.dump(obj, mode: :compat) # compat ensures symbols are converted to strings
54
+ else
55
+ to_query_string(obj)
56
+ end
57
+ end
58
+
59
+ def self.to_query_string(obj, key = nil)
50
60
  if key.nil? && obj.is_a?(String) # && obj['=']
51
61
  return obj
52
62
  end
53
63
 
54
64
  case obj
55
- when Hash then obj.map { |k, v| encode(v, append_key(key,k)) }.join('&')
56
- when Array then obj.map { |v| encode(v, "#{key}[]") }.join('&')
65
+ when Hash then obj.map { |k, v| to_query_string(v, append_key(key, k)) }.join('&')
66
+ when Array then obj.map { |v| to_query_string(v, "#{key}[]") }.join('&')
57
67
  when nil then ''
58
68
  else
59
69
  "#{key}=#{ERB::Util.url_encode(obj.to_s)}"
@@ -119,6 +129,7 @@ module Dagger
119
129
  opts[:follow] = 10 if opts[:follow] == true
120
130
  headers = opts[:headers] || {}
121
131
  headers['Accept'] = 'application/json' if opts[:json] && headers['Accept'].nil?
132
+ headers['Content-Type'] = 'application/json' if opts[:json] && opts[:body]
122
133
 
123
134
  if opts[:ip]
124
135
  headers['Host'] = uri.host
@@ -127,6 +138,7 @@ module Dagger
127
138
 
128
139
  request = Net::HTTP::Get.new(uri, DEFAULT_HEADERS.merge(headers))
129
140
  request.basic_auth(opts.delete(:username), opts.delete(:password)) if opts[:username]
141
+ request.body = Utils.encode_body(opts[:body], opts) if opts[:body]
130
142
 
131
143
  if @http.respond_to?(:started?) # regular Net::HTTP
132
144
  @http.start unless @http.started?
@@ -137,7 +149,7 @@ module Dagger
137
149
 
138
150
  if REDIRECT_CODES.include?(resp.code.to_i) && resp['Location'] && (opts[:follow] && opts[:follow] > 0)
139
151
  opts[:follow] -= 1
140
- debug "Following redirect to #{resp['Location']}"
152
+ debug { "Following redirect to #{resp['Location']}" }
141
153
  return get(resp['Location'], opts)
142
154
  end
143
155
 
@@ -148,7 +160,7 @@ module Dagger
148
160
  SocketError, EOFError, OpenSSL::SSL::SSLError => e
149
161
 
150
162
  if retries = opts[:retries] and retries.to_i > 0
151
- debug "Got #{e.class}! Retrying in a sec (#{retries} retries left)"
163
+ debug { "Got #{e.class}! Retrying in a sec (#{retries} retries left)" }
152
164
  sleep (opts[:retry_wait] || DEFAULT_RETRY_WAIT)
153
165
  get(uri, opts.merge(retries: retries - 1))
154
166
  else
@@ -174,45 +186,44 @@ module Dagger
174
186
 
175
187
  def request(method, uri, data, opts = {})
176
188
  if method.to_s.downcase == 'get'
177
- query = (opts[:query] || {}).merge(data || {})
178
- return get(uri, opts.merge(query: query))
189
+ data ||= opts[:body]
190
+ return get(uri, opts.merge(body: data))
179
191
  end
180
192
 
181
- uri = Utils.resolve_uri(uri, @host)
193
+ uri = Utils.resolve_uri(uri, @host, opts[:query])
182
194
  if @host != uri.scheme_and_host
183
195
  raise ArgumentError.new("#{uri.scheme_and_host} does not match #{@host}")
184
196
  end
185
197
 
186
198
  headers = DEFAULT_HEADERS.merge(opts[:headers] || {})
187
-
188
- query = if data.is_a?(String)
189
- data
190
- elsif opts[:json]
191
- headers['Content-Type'] = 'application/json'
192
- headers['Accept'] = 'application/json' if headers['Accept'].nil?
193
- Oj.dump(data, mode: :compat) # compat ensures symbols are converted to strings
194
- else # querystring, then
195
- Utils.encode(data)
196
- end
199
+ body = Utils.encode_body(data, opts)
197
200
 
198
201
  if opts[:username] # opts[:password] is optional
199
202
  str = [opts[:username], opts[:password]].compact.join(':')
200
203
  headers['Authorization'] = 'Basic ' + Base64.encode64(str)
201
204
  end
202
205
 
206
+ if opts[:json]
207
+ headers['Content-Type'] = 'application/json'
208
+ headers['Accept'] = 'application/json' if headers['Accept'].nil?
209
+ end
210
+
211
+ start = Time.now
212
+ debug { "Sending #{method} request to #{uri.request_uri} with headers #{headers.inspect} -- #{query}" }
213
+
203
214
  if @http.respond_to?(:started?) # regular Net::HTTP
204
- args = [method.to_s.downcase, uri.path, query, headers]
215
+ args = [method.to_s.downcase, uri.request_uri, body, headers]
205
216
  args.delete_at(2) if args[0] == 'delete' # Net::HTTP's delete does not accept data
206
217
 
207
218
  @http.start unless @http.started?
208
219
  resp, data = @http.send(*args)
209
220
  else # Net::HTTP::Persistent
210
- req = Kernel.const_get("Net::HTTP::#{method.capitalize}").new(uri.path, headers)
211
- # req.set_form_data(query)
212
- req.body = query
221
+ req = Kernel.const_get("Net::HTTP::#{method.capitalize}").new(uri.request_uri, headers)
222
+ req.body = body
213
223
  resp, data = @http.send_request(uri, req)
214
224
  end
215
225
 
226
+ debug { "Got response #{resp.code} in #{(Time.now - start).round(2)}s: #{data || resp.body}" }
216
227
  @response = build_response(resp, data || resp.body)
217
228
 
218
229
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EINVAL, Timeout::Error, \
@@ -220,7 +231,7 @@ module Dagger
220
231
  SocketError, EOFError, OpenSSL::SSL::SSLError => e
221
232
 
222
233
  if method.to_s.downcase != 'get' && retries = opts[:retries] and retries.to_i > 0
223
- debug "[#{DAGGER_NAME}] Got #{e.class}! Retrying in a sec (#{retries} retries left)"
234
+ debug { "Got #{e.class}! Retrying in a sec (#{retries} retries left)" }
224
235
  sleep (opts[:retry_wait] || DEFAULT_RETRY_WAIT)
225
236
  request(method, uri, data, opts.merge(retries: retries - 1))
226
237
  else
@@ -252,8 +263,16 @@ module Dagger
252
263
 
253
264
  private
254
265
 
255
- def debug(str)
256
- puts str if ENV['DEBUGGING']
266
+ def debug(&block)
267
+ if ENV['DEBUGGING'] || ENV['DEBUG']
268
+ str = yield
269
+ logger.info "[#{DAGGER_NAME}] #{str}"
270
+ end
271
+ end
272
+
273
+ def logger
274
+ require 'logger'
275
+ @logger ||= Logger.new(@logfile || STDOUT)
257
276
  end
258
277
 
259
278
  def build_response(resp, body)
@@ -6,25 +6,28 @@ require 'rspec/expectations'
6
6
  describe 'Persistent mode' do
7
7
 
8
8
  it 'works' do
9
- fake_client = double('Client')
10
- expect(Dagger::Client).to receive(:new).once.and_return(fake_client)
11
- expect(fake_client).to receive(:open).once #.and_return(fake_resp)
12
- expect(fake_client).to receive(:close).once #.and_return(fake_resp)
9
+ # fake_client = double('Client')
10
+ # expect(Dagger::Client).to receive(:new).once.and_return(fake_client)
11
+ # expect(fake_client).to receive(:open).once #.and_return(fake_resp)
12
+ # expect(fake_client).to receive(:close).once #.and_return(fake_resp)
13
13
 
14
+ res1, res2 = nil, nil
14
15
  obj = Dagger.open('https://www.google.com') do
15
- get('/search?q=dagger+http+client')
16
- get('google.com/search?q=thank+you+ruby')
16
+ res1 = get('/search?q=dagger+http+client', { body: 'foo' })
17
+ res2 = get('https://www.google.com/search?q=thank+you+ruby')
18
+ res3 = post('https://www.google.com/search?q=foobar', { foo: 'bar' })
17
19
  end
20
+
21
+ expect(res1.code).to eq(400)
22
+ expect(res2.code).to eq(200)
23
+ expect(res2.code).to eq(200)
24
+ expect(obj).to be_a(Dagger::Client)
18
25
  end
19
26
 
20
27
  end
21
28
 
22
29
  describe 'using threads' do
23
30
 
24
- def get(url)
25
- @http.get(url)
26
- end
27
-
28
31
  def connect(host)
29
32
  raise if @http
30
33
  @http = Dagger.open(host)
@@ -49,7 +52,7 @@ describe 'using threads' do
49
52
  # mutex.synchronize { Dagger.open(host) }
50
53
  http = Dagger.open(host)
51
54
  while url = mutex.synchronize { urls.pop }
52
- puts "Fetching #{url}"
55
+ # puts "Fetching #{url}"
53
56
  resp = http.get(url)
54
57
  mutex.synchronize do
55
58
  result.push(resp.code)
@@ -0,0 +1,19 @@
1
+ require './lib/dagger'
2
+
3
+ require 'rspec/mocks'
4
+ require 'rspec/expectations'
5
+
6
+ describe 'sending data' do
7
+
8
+ it 'works with get if using .request' do
9
+ resp = Dagger.request('get', 'https://httpbingo.org/get?x=123', { foo: 'bar', testing: 1 }, { json: true })
10
+ expect(resp.ok?).to eq(true)
11
+ end
12
+
13
+ it 'works with get if passing body as option' do
14
+ resp = Dagger.get('https://httpbingo.org/get?x=123', { body: { foo: 'bar', testing: 1 }, json: true })
15
+ expect(resp.ok?).to eq(true)
16
+ end
17
+
18
+
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomás Pollak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-11 00:00:00.000000000 Z
11
+ date: 2021-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -120,6 +120,7 @@ files:
120
120
  - spec/ip_connect_spec.rb
121
121
  - spec/parsers_spec.rb
122
122
  - spec/persistent_spec.rb
123
+ - spec/sending_data_spec.rb
123
124
  homepage: https://github.com/tomas/dagger
124
125
  licenses: []
125
126
  metadata: {}