ease_http 0.1.0 → 0.2.0
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/ease_http/connection.rb +28 -3
- data/lib/ease_http/utils.rb +10 -0
- data/lib/ease_http/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0b372fed20c73fc13df8ff8a423ec2e3024cd96
|
4
|
+
data.tar.gz: ff68545a52cf6bbc8c633fd5c64d7772d3439f43
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6027b52315d492268ca239df2012680b420950e91ef1acb54ed91800c1d42da3682b0501bbf2a206bb0330f251362cfb43d21e1f660b99982264edcfdcbc4492
|
7
|
+
data.tar.gz: fff1d07c52b760b4418545bf4cff867d1f729429a5b3e2fc94558eb972e021f83a85189ce7bb75648851f54538f80c80475fd33d0377781ae1dc4c7acb390e6a
|
data/lib/ease_http/connection.rb
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require 'uri'
|
3
3
|
require 'net/http'
|
4
|
+
require 'ease_http/utils'
|
4
5
|
|
5
6
|
module EaseHTTP
|
6
7
|
class Connection
|
7
|
-
def initialize(endpoint)
|
8
|
+
def initialize(endpoint, options={})
|
8
9
|
@endpoint = endpoint
|
9
10
|
uri = URI(endpoint)
|
11
|
+
# timeout = options.delete(:timeout) || 0.2
|
10
12
|
|
11
13
|
@http = Net::HTTP.new(uri.host, uri.port)
|
12
14
|
@http.use_ssl = uri.scheme == 'https'
|
15
|
+
# @http.read_timeout = timeout
|
13
16
|
end
|
14
17
|
|
15
18
|
# path - path to request
|
@@ -25,7 +28,8 @@ module EaseHTTP
|
|
25
28
|
uri.query = URI.encode_www_form(query)
|
26
29
|
end
|
27
30
|
|
28
|
-
|
31
|
+
request = Net::HTTP::Get.new uri.request_uri, headers
|
32
|
+
@http.request request, &block
|
29
33
|
end
|
30
34
|
|
31
35
|
# path - path to request
|
@@ -35,8 +39,29 @@ module EaseHTTP
|
|
35
39
|
def post(path, options={}, &block)
|
36
40
|
headers = options.delete :headers
|
37
41
|
data = options.delete :data
|
42
|
+
content_type = (headers && headers['Content-Type']) || content_type(data)
|
38
43
|
|
39
|
-
|
44
|
+
request = Net::HTTP::Post.new path, headers
|
45
|
+
|
46
|
+
# convert hash data to array data
|
47
|
+
data = data.inject([]) do |memo, (key, value)|
|
48
|
+
if value.is_a?(File)
|
49
|
+
memo << [key.to_s, value, { content_type: Utils.mime_type(value.path) }]
|
50
|
+
else
|
51
|
+
memo << [key.to_s, value]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
request.set_form data, content_type
|
56
|
+
|
57
|
+
@http.request request, &block
|
40
58
|
end
|
59
|
+
|
60
|
+
private
|
61
|
+
def content_type(data)
|
62
|
+
data.any? { |key, value| value.is_a?(File) } ?
|
63
|
+
'multipart/form-data' :
|
64
|
+
'application/x-www-form-urlencoded'
|
65
|
+
end
|
41
66
|
end
|
42
67
|
end
|
data/lib/ease_http/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ease_http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- reyesyang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- ease_http.gemspec
|
87
87
|
- lib/ease_http.rb
|
88
88
|
- lib/ease_http/connection.rb
|
89
|
+
- lib/ease_http/utils.rb
|
89
90
|
- lib/ease_http/version.rb
|
90
91
|
homepage: https://github.com/reyesyang/ease_http
|
91
92
|
licenses:
|