emmy-extends 0.1.14 → 0.1.15
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/lib/emmy_extends/em_http_request/adapter.rb +52 -3
- data/lib/emmy_extends/thin/backend.rb +1 -1
- data/lib/emmy_extends/version.rb +1 -1
- data/spec/em_http_request_spec.rb +34 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7088fa3fafc3fdc98958ff1734a2522928fe3170
|
4
|
+
data.tar.gz: b7863454fe8471690716ab1d003741af1c5dcdd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e10fbf26fcbed70fa700c6ef74493481da16c6a122f5851a904b6cfca0261f796adcc89e7a1947fc684416a465cf3416301a39ea15f2d335e11f01f5769d8f28
|
7
|
+
data.tar.gz: 07bb371f777657cda35cdebc31bfcc90ff61eacac44dd571c02fdd9b39baed50db70d4a1ec597607c651235c06befacad09fe6e108e98fd92b42d4b1fd3dacf3
|
@@ -11,16 +11,65 @@ module EmmyExtends
|
|
11
11
|
attr_reader :operation
|
12
12
|
attr_reader :response
|
13
13
|
attr_reader :url
|
14
|
+
attr_reader :headers
|
15
|
+
attr_reader :body
|
14
16
|
|
15
17
|
# required for adapter
|
16
18
|
|
17
19
|
def delegate=(operation)
|
18
20
|
@operation = operation
|
19
|
-
|
21
|
+
prepare_url
|
22
|
+
@headers = operation.request.headers.clone
|
23
|
+
prepare_body
|
20
24
|
setup_http_request
|
21
25
|
setup_http_client
|
22
26
|
end
|
23
27
|
|
28
|
+
def prepare_url
|
29
|
+
@url = operation.request.real_url
|
30
|
+
raise 'relative url' if url.relative?
|
31
|
+
|
32
|
+
@url.normalize!
|
33
|
+
|
34
|
+
if path = operation.request.real_path
|
35
|
+
raise 'path is not relative' unless path.relative?
|
36
|
+
@url += path
|
37
|
+
end
|
38
|
+
@url.user = operation.request.user if operation.request.user
|
39
|
+
@url.password = operation.request.password if operation.request.password
|
40
|
+
@url.query = operation.request.query.is_a?(Hash) ? Encoders.query(operation.request.query) : operation.request.query.to_s if operation.request.query
|
41
|
+
end
|
42
|
+
|
43
|
+
def prepare_body
|
44
|
+
raise "attribute `file` unsupported" if operation.request.file
|
45
|
+
body, form, json, file = operation.request.body, operation.request.form, operation.request.json
|
46
|
+
|
47
|
+
@body = if body
|
48
|
+
raise "body cannot be hash" if body.is_a?(Hash)
|
49
|
+
body_text = body.is_a?(Array) ? body.join : body.to_s
|
50
|
+
headers['Content-Length'] = body_text.bytesize
|
51
|
+
body_text
|
52
|
+
|
53
|
+
elsif form
|
54
|
+
form_encoded = form.is_a?(String) ? form : Encoders.www_form(form)
|
55
|
+
body_text = Encoders.rfc3986(form_encoded)
|
56
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
57
|
+
headers['Content-Length'] = body_text.bytesize
|
58
|
+
body_text
|
59
|
+
|
60
|
+
elsif json
|
61
|
+
json_string = json.is_a?(String) ? json : (json.respond_to?(:to_json) ? json.to_json : JSON.dump(json))
|
62
|
+
headers['Content-Type'] = 'application/json'
|
63
|
+
headers['Content-Length'] = json_string.size
|
64
|
+
json_string
|
65
|
+
|
66
|
+
else
|
67
|
+
headers['Content-Length'] = 0 if %w('POST PUT').include? operation.request.type
|
68
|
+
'' # empty body
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
24
73
|
def to_a
|
25
74
|
["tcp://#{url.host}:#{url.port || url.default_port}", EmmyMachine::Connection, method(:initialize_connection), self]
|
26
75
|
end
|
@@ -76,8 +125,8 @@ module EmmyExtends
|
|
76
125
|
keepalive: false,
|
77
126
|
path: url.path,
|
78
127
|
query: url.query,
|
79
|
-
body: encode_body(
|
80
|
-
head:
|
128
|
+
body: encode_body(body),
|
129
|
+
head: headers
|
81
130
|
}
|
82
131
|
end
|
83
132
|
|
data/lib/emmy_extends/version.rb
CHANGED
@@ -35,4 +35,38 @@ describe EmmyExtends::EmHttpRequest do
|
|
35
35
|
expect(response.headers["Server"]).to eq("nginx")
|
36
36
|
expect(response.body.empty?).to be false
|
37
37
|
end
|
38
|
+
|
39
|
+
it "should send http get with params to httpbin.org" do
|
40
|
+
request = EmmyHttp::Request.new(
|
41
|
+
type: 'get',
|
42
|
+
url: 'http://httpbin.org',
|
43
|
+
path: '/bytes{/bytes}',
|
44
|
+
params: {
|
45
|
+
bytes: 1024
|
46
|
+
}
|
47
|
+
)
|
48
|
+
operation = EmmyHttp::Operation.new(request, EmmyExtends::EmHttpRequest::Adapter.new)
|
49
|
+
response = operation.sync
|
50
|
+
|
51
|
+
expect(response.status).to be 200
|
52
|
+
expect(response.headers).to include("Content-Type")
|
53
|
+
expect(response.headers["Server"]).to eq("nginx")
|
54
|
+
expect(response.body.empty?).to be false
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should send http get with params to httpbin.org" do
|
58
|
+
request = EmmyHttp::Request.new(
|
59
|
+
type: 'POST',
|
60
|
+
url: 'http://httpbin.org',
|
61
|
+
path: '/post',
|
62
|
+
json: {points: [{x:5, y:6}, {x:3, y:2}]}
|
63
|
+
|
64
|
+
)
|
65
|
+
operation = EmmyHttp::Operation.new(request, EmmyExtends::EmHttpRequest::Adapter.new)
|
66
|
+
response = operation.sync
|
67
|
+
|
68
|
+
expect(response.status).to be 200
|
69
|
+
expect(response.content_type).to eq("application/json")
|
70
|
+
expect(response.content["json"]).to include('points' => [{'x' => 5, 'y' => 6}, {'x' => 3, 'y' => 2}])
|
71
|
+
end
|
38
72
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emmy-extends
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- inre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05
|
11
|
+
date: 2015-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: emmy-machine
|