dagger 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -4
- data/lib/dagger.rb +10 -6
- data/lib/dagger/version.rb +1 -1
- data/test/arguments_spec.rb +48 -0
- data/test/persistent_spec.rb +19 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d105ac895b507dfe26dff70cb114f8aa3df1070
|
4
|
+
data.tar.gz: 9e406e38bd220df5bef5c18fb5245a29dad6c605
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9da12347a5186a0ef71a66b63bb78c97413ecdee9a2411baf3f9a0e9120c938cf1a51ecd08f7b2b39fc986e636102ebc1490368a35f6f6d4bf1d8ee07004fd8a
|
7
|
+
data.tar.gz: 3f3a9136f4a7d26cef68fb311aadd10b6f513e59590ecd0a0cef82e92072312dd7db18a1e91c0ad2d920748cc425f9032c9ce4efeb381149967f29afef09f9a4
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ In your Gemfile:
|
|
12
12
|
|
13
13
|
# Usage
|
14
14
|
|
15
|
-
## `get(url, [
|
15
|
+
## `get(url, [options])`
|
16
16
|
|
17
17
|
```rb
|
18
18
|
require 'dagger'
|
@@ -20,15 +20,15 @@ resp = Dagger.get('http://google.com')
|
|
20
20
|
|
21
21
|
puts resp.body # => "<!doctype html...>"
|
22
22
|
|
23
|
-
#
|
24
|
-
Dagger.get('google.com/search', { q: 'dagger' }) # => requests '/search?q=dagger'
|
23
|
+
# you can also pass a query via the options hash, in which case is appended as a query string.
|
24
|
+
Dagger.get('google.com/search', { query: { q: 'dagger' } }) # => requests '/search?q=dagger'
|
25
25
|
```
|
26
26
|
|
27
27
|
## `post(url, params, [options])`
|
28
28
|
|
29
29
|
```rb
|
30
30
|
resp = Dagger.post('http://api.server.com', { foo: 'bar' })
|
31
|
-
puts resp.
|
31
|
+
puts resp.code # => 200
|
32
32
|
|
33
33
|
# if you want to send JSON to the server, you can pass the { json: true } option,
|
34
34
|
# which converts your params object to JSON, and also sets Content-Type to 'application/json'
|
@@ -47,6 +47,22 @@ Same syntax applies for `put`, `patch` and `delete` requests.
|
|
47
47
|
resp = Dagger.request(:put, 'https://api.server.com', { foo: 'bar' }, { follow: 10 })
|
48
48
|
puts resp.headers # { 'Content-Type' => 'application/json', ... }
|
49
49
|
```
|
50
|
+
In this case, if you want to include a query in your get request, simply pass it as
|
51
|
+
the `params` argument.
|
52
|
+
|
53
|
+
## `open(url, [options]) # => &block`
|
54
|
+
|
55
|
+
Oh yes. Dagger can open and hold a persistent connection so you can perform various
|
56
|
+
requests without the overhead of establishing new TCP sessions.
|
57
|
+
|
58
|
+
```rb
|
59
|
+
Dagger.open('https://api.server.com', { verify_ssl: 'false' }) do
|
60
|
+
if post('/login', { email: 'foo@bar.com', pass: 'secret' }).success?
|
61
|
+
resp = get('/something', { query: { items: 20 }, follow: 5 }) # follow 5 redirects max.
|
62
|
+
File.open('something', 'wb') { |f| f.write(resp.body) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
50
66
|
|
51
67
|
# Options
|
52
68
|
|
data/lib/dagger.rb
CHANGED
@@ -24,13 +24,17 @@ module Dagger
|
|
24
24
|
uri
|
25
25
|
end
|
26
26
|
|
27
|
-
def self.encode(
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
def self.encode(obj, key = nil)
|
28
|
+
if key.nil? && obj.is_a?(String) # && obj['=']
|
29
|
+
return obj
|
30
|
+
end
|
31
|
+
|
32
|
+
case obj
|
33
|
+
when Hash then obj.map { |k, v| encode(v, append_key(key,k)) }.join('&')
|
34
|
+
when Array then obj.map { |v| encode(v, "#{key}[]") }.join('&')
|
31
35
|
when nil then ''
|
32
36
|
else
|
33
|
-
"#{key}=#{URI.escape(
|
37
|
+
"#{key}=#{URI.escape(obj.to_s)}"
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
@@ -65,7 +69,7 @@ module Dagger
|
|
65
69
|
def get(uri, opts = {})
|
66
70
|
opts[:follow] = 10 if opts[:follow] == true
|
67
71
|
|
68
|
-
path = uri[0] == '/' ? uri : Utils.parse_uri(uri)
|
72
|
+
path = uri[0] == '/' ? uri : Utils.parse_uri(uri).request_uri
|
69
73
|
path.sub!(/\?.*|$/, '?' + Utils.encode(opts[:query])) if opts[:query]
|
70
74
|
|
71
75
|
request = Net::HTTP::Get.new(path, DEFAULT_HEADERS.merge(opts[:headers] || {}))
|
data/lib/dagger/version.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require './lib/dagger'
|
2
|
+
|
3
|
+
require 'rspec/mocks'
|
4
|
+
require 'rspec/expectations'
|
5
|
+
|
6
|
+
describe 'arguments' do
|
7
|
+
|
8
|
+
describe 'URL' do
|
9
|
+
|
10
|
+
def send_request(url)
|
11
|
+
Dagger.get(url)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'empty url' do
|
15
|
+
|
16
|
+
it 'raises error' do
|
17
|
+
expect { send_request('') }.to raise_error(URI::InvalidURIError)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'invalid URL' do
|
23
|
+
|
24
|
+
it 'raises error' do
|
25
|
+
expect { send_request('asd123.rewqw') }.to raise_error(SocketError)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'nonexisting host' do
|
31
|
+
|
32
|
+
it 'raises error' do
|
33
|
+
expect { send_request('http://www.foobar1234567890foobar.com/hello') }.to raise_error(SocketError)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'valid host' do
|
39
|
+
|
40
|
+
it 'works' do
|
41
|
+
expect(send_request('http://www.google.com')).to be_a(Net::HTTPResponse)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require './lib/dagger'
|
2
|
+
|
3
|
+
require 'rspec/mocks'
|
4
|
+
require 'rspec/expectations'
|
5
|
+
|
6
|
+
describe 'Persitent mode' do
|
7
|
+
|
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(:get).twice #.and_return(fake_resp)
|
12
|
+
|
13
|
+
obj = Dagger.open('https://www.google.com') do
|
14
|
+
get('/search?q=dagger+http+client')
|
15
|
+
get('google.com/search?q=thank+you+ruby')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
@@ -113,7 +113,9 @@ files:
|
|
113
113
|
- lib/dagger/parsers.rb
|
114
114
|
- lib/dagger/response.rb
|
115
115
|
- lib/dagger/version.rb
|
116
|
+
- test/arguments_spec.rb
|
116
117
|
- test/parsers_spec.rb
|
118
|
+
- test/persistent_spec.rb
|
117
119
|
homepage: https://github.com/tomas/dagger
|
118
120
|
licenses: []
|
119
121
|
metadata: {}
|