em-rest-client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/em-rest-client.gemspec +27 -0
- data/lib/em-rest-client.rb +2 -0
- data/lib/em-rest-client/em_http_request.rb +107 -0
- data/lib/em-rest-client/rest_client.rb +27 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 33ad6f9a9a457e88f3f1a141ded4d63f9922c99d
|
4
|
+
data.tar.gz: e865d4f6b1b46d7fbb9d990e1c37a19e34c8d0bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 567fed64ed7e316a6876107affdd6fb227786bbc3e641ba3b62615cf2dea53df0c6ade0a459b828567c0b1b06cec5e0c53bef13351db967db2ad99356ead3a4d
|
7
|
+
data.tar.gz: d423bc5560975a5600213be2620ec208791095646121a0b319bf283d1f34f8a4039b1810d63d36aeac4c97ab5cea306c0aa5cd4b7a2bd21ce6bc0a9fa61c5f67
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Railsware (www.railsware.com)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# EM-REST-Client
|
2
|
+
|
3
|
+
|
4
|
+
This gem allows to use different HTTP implementation in RestClient with next adapters:
|
5
|
+
|
6
|
+
* `:net_http` - Net::HTTP (default, blocking I/O)
|
7
|
+
* `:em_http` - EventMachine::HttpRequest (non-blocking I/0)
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'em-rest-client'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install em-rest-client
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
|
29
|
+
You may specify adapter for each request:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
RestClient.get('https://www.google.com')
|
33
|
+
RestClient.get('https://www.google.com', adapter: :em_http)
|
34
|
+
```
|
35
|
+
|
36
|
+
Or you may specify adapter globaly:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
RestClient.adapter = :em_http
|
40
|
+
RestClient.get('https://www.google.com')
|
41
|
+
```
|
42
|
+
|
43
|
+
## Notes
|
44
|
+
|
45
|
+
When EventMachine is already running we assume that you are responsible for Fiber allocation.
|
46
|
+
You can add `Rack::FiberPool` to your application middleware and it automatically provides a Fiber for each incoming HTTP request. It creates pool of Fibers and re-use fiber for each incoming HTTP request. Also you can control the connection pool size.
|
47
|
+
|
48
|
+
## Authors
|
49
|
+
|
50
|
+
* [Andriy Yanko](http://ayanko.github.io)
|
51
|
+
|
52
|
+
## References
|
53
|
+
|
54
|
+
* https://github.com/rest-client/rest-client
|
55
|
+
* https://github.com/igrigorik/em-http-request
|
56
|
+
* https://github.com/alebsack/rack-fiber_pool
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "em/rest/client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "em-rest-client"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Andriy Yanko"]
|
9
|
+
spec.email = ["andriy.yanko@railsware.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{EventMachine::HttpRequest adapter for HTTP REST client}
|
12
|
+
spec.homepage = "https://github.com/railsware/em-rest-client"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "rest-client", "~> 1.8"
|
21
|
+
spec.add_development_dependency "em-http-request", "~> 1.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.4.0"
|
26
|
+
spec.add_development_dependency "webmock", "~> 2.0"
|
27
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'em-http'
|
2
|
+
require 'fiber'
|
3
|
+
|
4
|
+
module RestClient
|
5
|
+
class EmHttpRequest < Request
|
6
|
+
|
7
|
+
def initialize(args)
|
8
|
+
raise ArgumentError, ':block_response option is not supported' if args[:block_response]
|
9
|
+
super(args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute(&block)
|
13
|
+
uri = parse_url_with_auth(url)
|
14
|
+
connection = build_connection(uri)
|
15
|
+
client = transmit(connection, &block)
|
16
|
+
raise client.error if client.error
|
17
|
+
net_http_response = build_net_http_response(client)
|
18
|
+
process_result(net_http_response, &block)
|
19
|
+
ensure
|
20
|
+
payload.close if payload
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def transmit(connection, &block)
|
26
|
+
client = nil
|
27
|
+
if EM.reactor_running?
|
28
|
+
client = sync_transmit(connection, &block)
|
29
|
+
else
|
30
|
+
EM.run do
|
31
|
+
Fiber.new do
|
32
|
+
client = sync_transmit(connection, &block)
|
33
|
+
EM.stop
|
34
|
+
end.resume
|
35
|
+
end
|
36
|
+
end
|
37
|
+
client
|
38
|
+
end
|
39
|
+
|
40
|
+
def sync_transmit(connection, &block)
|
41
|
+
client = build_client(connection)
|
42
|
+
if client.error
|
43
|
+
client
|
44
|
+
else
|
45
|
+
fiber = Fiber.current
|
46
|
+
client.callback { fiber.resume(client) }
|
47
|
+
client.errback { fiber.resume(client) }
|
48
|
+
Fiber.yield
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_connection(uri)
|
53
|
+
uri = uri.dup
|
54
|
+
uri.user = nil
|
55
|
+
uri.password = nil
|
56
|
+
EM::HttpRequest.new(uri, build_connection_options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_client(connection)
|
60
|
+
connection.setup_request(method, build_request_options)
|
61
|
+
end
|
62
|
+
|
63
|
+
def build_connection_options
|
64
|
+
options = {}
|
65
|
+
options[:connect_timeout] = open_timeout if open_timeout
|
66
|
+
options[:inactivity_timeout] = timeout if timeout
|
67
|
+
if RestClient.proxy
|
68
|
+
proxy_uri = URI.parse(RestClient.proxy)
|
69
|
+
options[:proxy] = {
|
70
|
+
host: proxy_uri.host,
|
71
|
+
port: proxy_uri.port,
|
72
|
+
authorization: [proxy_uri.user, proxy_uri.password]
|
73
|
+
}
|
74
|
+
end
|
75
|
+
options
|
76
|
+
end
|
77
|
+
|
78
|
+
def build_request_options
|
79
|
+
options = {}
|
80
|
+
options[:decoding] = false
|
81
|
+
options[:head] = processed_headers
|
82
|
+
if user
|
83
|
+
options[:head]['authorization'] = [user, password]
|
84
|
+
end
|
85
|
+
options[:body] = payload.to_s
|
86
|
+
options
|
87
|
+
end
|
88
|
+
|
89
|
+
def build_net_http_response(client)
|
90
|
+
klass = Net::HTTPResponse.send(
|
91
|
+
:response_class,
|
92
|
+
client.response_header.status.to_s
|
93
|
+
)
|
94
|
+
response = klass.new(
|
95
|
+
client.response_header.http_version,
|
96
|
+
client.response_header.status.to_s,
|
97
|
+
client.response_header.http_reason
|
98
|
+
)
|
99
|
+
client.response_header.raw.each do |k, v|
|
100
|
+
response.add_field(k, v)
|
101
|
+
end
|
102
|
+
response.body = client.response
|
103
|
+
response.instance_variable_set :@read, true
|
104
|
+
response
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
|
3
|
+
module RestClient
|
4
|
+
class << self
|
5
|
+
attr_accessor :adapter
|
6
|
+
end
|
7
|
+
|
8
|
+
class Request
|
9
|
+
class << self
|
10
|
+
def execute(args, &block)
|
11
|
+
adapter = args.fetch(:adapter, RestClient.adapter)
|
12
|
+
klass =
|
13
|
+
case adapter
|
14
|
+
when :net_http
|
15
|
+
Request
|
16
|
+
when :em_http
|
17
|
+
EmHttpRequest
|
18
|
+
else
|
19
|
+
raise NotImplementedError, "Unsupported adapter: #{adapter.inspect}"
|
20
|
+
end
|
21
|
+
klass.new(args).execute(&block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
RestClient.adapter = :net_http
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-rest-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andriy Yanko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: em-http-request
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.4.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.4.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- andriy.yanko@railsware.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- em-rest-client.gemspec
|
114
|
+
- lib/em-rest-client.rb
|
115
|
+
- lib/em-rest-client/em_http_request.rb
|
116
|
+
- lib/em-rest-client/rest_client.rb
|
117
|
+
homepage: https://github.com/railsware/em-rest-client
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.4.5.1
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: EventMachine::HttpRequest adapter for HTTP REST client
|
141
|
+
test_files: []
|