net-http2 0.7.2 → 0.8.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
  SHA1:
3
- metadata.gz: 4287aab78b7e305b8468f096cc87d1206881f07b
4
- data.tar.gz: dbd232be1b30b8fbe2e8cd8c12ff7442d5ed3a3f
3
+ metadata.gz: ba9816c229aab7bb320437a77c3c91ed60752609
4
+ data.tar.gz: 63836c93155c8d717f68542519b145f87e5e6bd1
5
5
  SHA512:
6
- metadata.gz: 90f467864f059da2242589f1bc50db6a520d69f9334a42d6d4aa5c6f6352789101a629712cd55a40efb6e97b3ae3df442399436e74742774106441728316aa77
7
- data.tar.gz: d973d443d3f3b9516f5c3d620f1393a86e87924e2888a643f37c82df4d86deb38264abe56adf03e23c5d25277c399bfd567b01e8e4aef71e90020446f91bb505
6
+ metadata.gz: bb589fa1c74d8fb02f79161660756667c3420baaa3810007a47506df85c5b996cfa46524d98bf6a11e2099dedc9fd2bba7922fd4ed171201dce5b59472e5012a
7
+ data.tar.gz: 749b07f13a864be5a935ee7fbeef1bc912e1a4e75170f9c1334de8799aeeb0f318e43a6cc209bcc8733b6687121bee756165a759e17273e47ddc50fbeaee63ba
data/README.md CHANGED
@@ -20,7 +20,6 @@ gem 'net-http2'
20
20
 
21
21
  ## Usage
22
22
 
23
- With a blocking call:
24
23
  ```ruby
25
24
  require 'net-http2'
26
25
 
@@ -40,30 +39,6 @@ response.body # => "A body"
40
39
  client.close
41
40
  ```
42
41
 
43
- With a non-blocking call:
44
- ```ruby
45
- require 'net-http2'
46
-
47
- # create a client
48
- client = NetHttp2::Client.new("http://106.186.112.116")
49
-
50
- # send request
51
- client.async_get('/') do |response|
52
-
53
- # read the response
54
- response.ok? # => true
55
- response.status # => '200'
56
- response.headers # => {":status"=>"200"}
57
- response.body # => "A body"
58
-
59
- # close the connection
60
- client.close
61
- end
62
-
63
- # quick & dirty fix to wait for the block to be called asynchronously
64
- sleep 5
65
- ```
66
-
67
42
 
68
43
  ## Objects
69
44
 
@@ -99,64 +74,24 @@ NetHttp2::Client.new(url)
99
74
  ##### Blocking calls
100
75
  These behave similarly to HTTP/1 calls.
101
76
 
102
- * **get(path, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
103
-
104
- Sends a GET request. Options can only specify a `:timeout` (defaults to 60).
105
- Returns `nil` in case a timeout occurs.
106
-
107
- For example:
108
-
109
- ```ruby
110
- response_1 = client.get('/path1')
111
- response_2 = client.get('/path2', { 'x-custom-header' => 'custom' })
112
- response_3 = client.get('/path3', { 'x-custom-header' => 'custom' }, timeout: 1)
113
- ```
114
-
115
- * **post(path, body, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
77
+ * **call(method, path, options={})** → **`NetHttp2::Response` or `nil`**
116
78
 
117
- Sends a POST request. Options can only specify a `:timeout` (defaults to 60).
118
- Returns `nil` in case a timeout occurs.
79
+ Sends a request. Returns `nil` in case a timeout occurs.
119
80
 
120
- * **put(path, body, headers={}, options={})** **`NetHttp2::Response` or `nil`**
121
-
122
- Sends a PUT request. Options can only specify a `:timeout` (defaults to 60).
123
- Returns `nil` in case a timeout occurs.
124
-
125
- * **delete(path, headers={}, options={})** → **`NetHttp2::Response` or `nil`**
126
-
127
- Sends a DELETE request. Options can only specify a `:timeout` (defaults to 60).
128
- Returns `nil` in case a timeout occurs.
129
-
130
- ##### Non-blocking calls
131
- > The API of these calls is still subject to change, as on of HTTP/2 benefits is to allow for the streaming of responses' bodies.
132
-
133
- * **async_get(path, headers={}, options={})** → block called with **`NetHttp2::Response` or `nil`**
134
-
135
- Sends a GET request. Options can only specify a `:timeout` (defaults to 60).
136
- Returns `nil` in case a timeout occurs.
81
+ `method` is a symbol that specifies the `:method` header (`:get`, `:post`, `:put`, `:patch`, `:delete`, `:options`). The body and the headers of the request can be specified in the options, together with the timeout.
137
82
 
138
83
  For example:
139
84
 
140
85
  ```ruby
141
- client.get('/path1') { |response_1| p response_2 }
142
- client.get('/path2', { 'x-custom-header' => 'custom' }) { |response_2| p response_2 }
143
- client.get('/path3', {}, timeout: 1) { |response_3| p response_3 }
86
+ response_1 = client.call(:get, '/path1')
87
+ response_2 = client.call(:get, '/path2', headers: { 'x-custom' => 'custom' })
88
+ response_3 = client.call(:post '/path3', body: "the request body", timeout: 1)
144
89
  ```
145
90
 
146
- * **async_post(path, body, headers={}, options={})** → block called with **`NetHttp2::Response` or `nil`**
147
-
148
- Sends a POST request. Options can only specify a `:timeout` (defaults to 60).
149
- Returns `nil` in case a timeout occurs.
150
91
 
151
- * **async_put(path,body, headers={}, options={})** → block called with **`NetHttp2::Response` or `nil`**
152
-
153
- Sends a PUT request. Options can only specify a `:timeout` (defaults to 60).
154
- Returns `nil` in case a timeout occurs.
155
-
156
- * **async_delete(path, headers={}, options={})** → block called with **`NetHttp2::Response` or `nil`**
92
+ ##### Non-blocking calls
157
93
 
158
- Sends a DELETE request. Options can only specify a `:timeout` (defaults to 60).
159
- Returns `nil` in case a timeout occurs.
94
+ > The real benefit of HTTP/2 is being able to receive body and header streams. The non-blocking API calls are currently being developed.
160
95
 
161
96
 
162
97
  ### `NetHttp2::Response`
data/lib/net-http2.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  require 'net-http2/client'
2
2
  require 'net-http2/response'
3
- require 'net-http2/request/base'
4
- require 'net-http2/request/delete'
5
- require 'net-http2/request/get'
6
- require 'net-http2/request/post'
7
- require 'net-http2/request/put'
3
+ require 'net-http2/request'
8
4
  require 'net-http2/stream'
9
5
  require 'net-http2/version'
10
6
 
@@ -21,44 +21,10 @@ module NetHttp2
21
21
  @mutex = Mutex.new
22
22
  end
23
23
 
24
- def get(path, headers={}, options={})
25
- request = NetHttp2::Request::Get.new(@uri, path, headers, options)
26
- call_with request
27
- end
28
-
29
- def post(path, body, headers={}, options={})
30
- request = NetHttp2::Request::Post.new(@uri, path, body, headers, options)
31
- call_with request
32
- end
33
-
34
- def put(path, body, headers={}, options={})
35
- request = NetHttp2::Request::Put.new(@uri, path, body, headers, options)
36
- call_with request
37
- end
38
-
39
- def delete(path, headers={}, options={})
40
- request = NetHttp2::Request::Delete.new(@uri, path, headers, options)
41
- call_with request
42
- end
43
-
44
- def async_get(path, headers={}, options={}, &block)
45
- request = NetHttp2::Request::Get.new(@uri, path, headers, options)
46
- async_call_with request, &block
47
- end
48
-
49
- def async_post(path, body, headers={}, options={}, &block)
50
- request = NetHttp2::Request::Post.new(@uri, path, body, headers, options)
51
- async_call_with request, &block
52
- end
53
-
54
- def async_put(path, body, headers={}, options={}, &block)
55
- request = NetHttp2::Request::Put.new(@uri, path, body, headers, options)
56
- async_call_with request, &block
57
- end
58
-
59
- def async_delete(path, headers={}, options={}, &block)
60
- request = NetHttp2::Request::Delete.new(@uri, path, headers, options)
61
- async_call_with request, &block
24
+ def call(method, path, options={})
25
+ request = NetHttp2::Request.new(method, @uri, path, options)
26
+ ensure_open
27
+ new_stream.call_with request
62
28
  end
63
29
 
64
30
  def ssl?
@@ -76,11 +42,6 @@ module NetHttp2
76
42
 
77
43
  private
78
44
 
79
- def call_with(request)
80
- ensure_open
81
- new_stream.call_with request
82
- end
83
-
84
45
  def async_call_with(request, &block)
85
46
  ensure_open
86
47
  new_stream.async_call_with request, &block
@@ -0,0 +1,37 @@
1
+ module NetHttp2
2
+
3
+ class Request
4
+
5
+ DEFAULT_TIMEOUT = 60
6
+
7
+ attr_reader :method, :uri, :path, :body, :timeout
8
+
9
+ def initialize(method, uri, path, options)
10
+ @method = method
11
+ @uri = uri
12
+ @path = path
13
+ @body = options[:body]
14
+ @headers = options[:headers] || {}
15
+ @timeout = options[:timeout] || DEFAULT_TIMEOUT
16
+ end
17
+
18
+ def headers
19
+ @headers.merge!({
20
+ ':scheme' => @uri.scheme,
21
+ ':method' => @method.to_s.upcase,
22
+ ':path' => @path,
23
+ })
24
+
25
+ @headers.merge!('host' => @uri.host) unless @headers['host']
26
+
27
+ if @body
28
+ @headers.merge!('content-length' => @body.bytesize.to_s)
29
+ else
30
+ @headers.delete('content-length')
31
+ end
32
+
33
+
34
+ @headers
35
+ end
36
+ end
37
+ end
@@ -15,7 +15,7 @@ module NetHttp2
15
15
  end
16
16
 
17
17
  @h2_stream.on(:data) { |d| @data << d }
18
- @h2_stream.on(:close) { mark_as_completed_and_async_respond }
18
+ @h2_stream.on(:close) { @completed = true }
19
19
  end
20
20
 
21
21
  def call_with(request)
@@ -23,16 +23,6 @@ module NetHttp2
23
23
  sync_respond(request.timeout)
24
24
  end
25
25
 
26
- def async_call_with(request, &block)
27
- @block = block
28
- send_data_of request
29
-
30
- Thread.new do
31
- wait(request.timeout)
32
- @block.call(nil) unless @completed
33
- end
34
- end
35
-
36
26
  private
37
27
 
38
28
  def send_data_of(request)
@@ -47,23 +37,11 @@ module NetHttp2
47
37
  end
48
38
  end
49
39
 
50
- def mark_as_completed_and_async_respond
51
- @completed = true
52
- @block.call(response) if @block
53
- end
54
-
55
40
  def sync_respond(timeout)
56
41
  wait(timeout)
57
42
  response if @completed
58
43
  end
59
44
 
60
- def response
61
- NetHttp2::Response.new(
62
- headers: @headers,
63
- body: @data
64
- )
65
- end
66
-
67
45
  def wait(timeout)
68
46
  cutoff_time = Time.now + timeout
69
47
 
@@ -71,5 +49,12 @@ module NetHttp2
71
49
  sleep 0.1
72
50
  end
73
51
  end
52
+
53
+ def response
54
+ NetHttp2::Response.new(
55
+ headers: @headers,
56
+ body: @data
57
+ )
58
+ end
74
59
  end
75
60
  end
@@ -1,3 +1,3 @@
1
1
  module NetHttp2
2
- VERSION = "0.7.2"
2
+ VERSION = "0.8.0"
3
3
  end
data/net-http2.gemspec CHANGED
@@ -4,13 +4,15 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'net-http2/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "net-http2"
8
- spec.version = NetHttp2::VERSION
9
- spec.licenses = ['MIT']
10
- spec.authors = ["Roberto Ostinelli"]
11
- spec.email = ["roberto@ostinelli.net"]
12
- spec.summary = %q{NetHttp2 is an HTTP2 client for Ruby.}
13
- spec.homepage = "http://github.com/ostinelli/net-http2"
7
+ spec.name = "net-http2"
8
+ spec.version = NetHttp2::VERSION
9
+ spec.licenses = ['MIT']
10
+ spec.authors = ["Roberto Ostinelli"]
11
+ spec.email = ["roberto@ostinelli.net"]
12
+ spec.summary = %q{NetHttp2 is an HTTP2 client for Ruby.}
13
+ spec.homepage = "http://github.com/ostinelli/net-http2"
14
+ spec.required_ruby_version = '>=2.1.0'
15
+
14
16
 
15
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
18
  spec.bindir = "exe"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-http2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Ostinelli
@@ -86,11 +86,7 @@ files:
86
86
  - bin/setup
87
87
  - lib/net-http2.rb
88
88
  - lib/net-http2/client.rb
89
- - lib/net-http2/request/base.rb
90
- - lib/net-http2/request/delete.rb
91
- - lib/net-http2/request/get.rb
92
- - lib/net-http2/request/post.rb
93
- - lib/net-http2/request/put.rb
89
+ - lib/net-http2/request.rb
94
90
  - lib/net-http2/response.rb
95
91
  - lib/net-http2/stream.rb
96
92
  - lib/net-http2/version.rb
@@ -107,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
103
  requirements:
108
104
  - - ">="
109
105
  - !ruby/object:Gem::Version
110
- version: '0'
106
+ version: 2.1.0
111
107
  required_rubygems_version: !ruby/object:Gem::Requirement
112
108
  requirements:
113
109
  - - ">="
@@ -1,33 +0,0 @@
1
- module NetHttp2
2
-
3
- module Request
4
-
5
- DEFAULT_TIMEOUT = 60
6
-
7
- class Base
8
- attr_reader :uri, :path, :body, :timeout
9
-
10
- def initialize(method, uri, path, body, headers, options={})
11
- @method = method
12
- @uri = uri
13
- @path = path
14
- @body = body
15
- @headers = headers
16
- @timeout = options[:timeout] || DEFAULT_TIMEOUT
17
- end
18
-
19
- def headers
20
- @headers.merge!({
21
- ':scheme' => @uri.scheme,
22
- ':method' => @method,
23
- ':path' => @path,
24
- })
25
-
26
- @headers.merge!('host' => @uri.host) unless @headers['host']
27
- @headers.merge!('content-length' => @body.bytesize.to_s) if @body
28
-
29
- @headers
30
- end
31
- end
32
- end
33
- end
@@ -1,12 +0,0 @@
1
- module NetHttp2
2
-
3
- module Request
4
-
5
- class Delete < Base
6
-
7
- def initialize(uri, path, headers, options)
8
- super('DELETE', uri, path, nil, headers, options)
9
- end
10
- end
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- module NetHttp2
2
-
3
- module Request
4
-
5
- class Get < Base
6
-
7
- def initialize(uri, path, headers, options)
8
- super('GET', uri, path, nil, headers, options)
9
- end
10
- end
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- module NetHttp2
2
-
3
- module Request
4
-
5
- class Post < Base
6
-
7
- def initialize(uri, path, body, headers, options)
8
- super('POST', uri, path, body, headers, options)
9
- end
10
- end
11
- end
12
- end
@@ -1,12 +0,0 @@
1
- module NetHttp2
2
-
3
- module Request
4
-
5
- class Put < Base
6
-
7
- def initialize(uri, path, body, headers, options)
8
- super('PUT', uri, path, body, headers, options)
9
- end
10
- end
11
- end
12
- end