emmy-extends 0.1.14 → 0.1.15

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: 8cd5850d2cf87a79c735d52200c3d96f86399c2c
4
- data.tar.gz: 4393a9ae336742bd9df485b3700123e5cd016bc3
3
+ metadata.gz: 7088fa3fafc3fdc98958ff1734a2522928fe3170
4
+ data.tar.gz: b7863454fe8471690716ab1d003741af1c5dcdd4
5
5
  SHA512:
6
- metadata.gz: 807638b36d9f9d3e474037781266fe791bb7e2e2478aacdd383c447af80859d3030eb8a87ab93f557131502eba257d50fd34805e455e553c43e515e21bc91fd3
7
- data.tar.gz: f14d2c27a8087b01e346e06e49c652f7c360c153a1d8191d25e477fc66f4abbcde73af719970979651587a8b315a1d8e6cfe31de388b7a759738459018fcfcfa
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
- @url = operation.request.real_url
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(operation.request.body),
80
- head: operation.request.headers
128
+ body: encode_body(body),
129
+ head: headers
81
130
  }
82
131
  end
83
132
 
@@ -16,7 +16,7 @@ module EmmyExtends
16
16
 
17
17
  # Stops the server
18
18
  def disconnect
19
- puts "disconnect"
19
+ #puts "disconnect"
20
20
  #EventMachine.stop_server(@signature)
21
21
  end
22
22
 
@@ -1,3 +1,3 @@
1
1
  module EmmyExtends
2
- VERSION = "0.1.14"
2
+ VERSION = "0.1.15"
3
3
  end
@@ -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.14
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-25 00:00:00.000000000 Z
11
+ date: 2015-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: emmy-machine