em-rest-client 0.0.1 → 0.1.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: 33ad6f9a9a457e88f3f1a141ded4d63f9922c99d
4
- data.tar.gz: e865d4f6b1b46d7fbb9d990e1c37a19e34c8d0bf
3
+ metadata.gz: '06104576379630975ec5aaab0e871ed05e0e2548'
4
+ data.tar.gz: d864b33a587822a8e583ad01376f3fc4385af69e
5
5
  SHA512:
6
- metadata.gz: 567fed64ed7e316a6876107affdd6fb227786bbc3e641ba3b62615cf2dea53df0c6ade0a459b828567c0b1b06cec5e0c53bef13351db967db2ad99356ead3a4d
7
- data.tar.gz: d423bc5560975a5600213be2620ec208791095646121a0b319bf283d1f34f8a4039b1810d63d36aeac4c97ab5cea306c0aa5cd4b7a2bd21ce6bc0a9fa61c5f67
6
+ metadata.gz: 8e1a3d562a9fa9a5021ec1c7c9efdf8883e70a9385f1f744f7dfd7a36a592d72cd089f72f4c4d016fbe6d1c5ef1e4ecf8c47a8de510e9c1dcbf7ebd333025ab5
7
+ data.tar.gz: a777f0815fb079d4b0b3096327eea9651cbbbc795d0dc692c3e7fc584ae83fcd2cfee38991f9439028267135eb0b490c36a3b6d9674a84f8ffe7371ce8c2b861
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
+ /Gemfile
3
4
  /Gemfile.lock
4
5
  /_yardoc/
5
6
  /coverage/
@@ -1,3 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.5
3
+ - 2.2.7
4
+ - 2.3.4
5
+ - 2.4.1
6
+ gemfile:
7
+ - Gemfile1
8
+ - Gemfile2
9
+ cache: bundler
10
+ script:
11
+ - bundle exec rspec
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ * Support RestClient v2.x.x
6
+
7
+ ## 0.0.1
8
+
9
+ * Initial release
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ gem 'rest-client', '1.8.0'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rest-client', '2.0.2'
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "em-rest-client"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.1.0"
8
8
  spec.authors = ["Andriy Yanko"]
9
9
  spec.email = ["andriy.yanko@railsware.com"]
10
10
 
@@ -17,11 +17,11 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_development_dependency "rest-client", "~> 1.8"
20
+ spec.add_development_dependency "rest-client", ">= 1.8"
21
21
  spec.add_development_dependency "em-http-request", "~> 1.1"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.9"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.4.0"
26
- spec.add_development_dependency "webmock", "~> 2.0"
26
+ spec.add_development_dependency "webmock", "~> 3.0"
27
27
  end
@@ -1,2 +1,3 @@
1
- require 'em-rest-client/rest_client'
1
+ require 'em-rest-client/configuration'
2
+ require 'em-rest-client/request'
2
3
  require 'em-rest-client/em_http_request'
@@ -0,0 +1,11 @@
1
+ require 'rest_client'
2
+
3
+ module RestClient
4
+ class << self
5
+ attr_reader :major_version
6
+ attr_accessor :adapter
7
+ end
8
+
9
+ self.instance_variable_set :@major_version, VERSION.to_i
10
+ self.adapter = :net_http
11
+ end
@@ -1,107 +1,2 @@
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
1
+ require 'em-rest-client/em_http_request/base'
2
+ require "em-rest-client/em_http_request/v#{RestClient::VERSION.to_i}"
@@ -0,0 +1,114 @@
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
+
66
+ if connect_timeout = build_connect_timeout
67
+ options[:connect_timeout] = connect_timeout
68
+ end
69
+
70
+ if inactivity_timeout = build_inactivity_timeout
71
+ options[:inactivity_timeout] = inactivity_timeout
72
+ end
73
+
74
+ if proxy_uri = build_proxy_uri
75
+ options[:proxy] = {
76
+ host: proxy_uri.host,
77
+ port: proxy_uri.port,
78
+ authorization: [proxy_uri.user, proxy_uri.password]
79
+ }
80
+ end
81
+
82
+ options
83
+ end
84
+
85
+ def build_request_options
86
+ options = {}
87
+ options[:decoding] = false
88
+ options[:head] = processed_headers
89
+ if user
90
+ options[:head]['authorization'] = [user, password]
91
+ end
92
+ options[:body] = payload.to_s
93
+ options
94
+ end
95
+
96
+ def build_net_http_response(client)
97
+ klass = Net::HTTPResponse.send(
98
+ :response_class,
99
+ client.response_header.status.to_s
100
+ )
101
+ response = klass.new(
102
+ client.response_header.http_version,
103
+ client.response_header.status.to_s,
104
+ client.response_header.http_reason
105
+ )
106
+ client.response_header.raw.each do |k, v|
107
+ response.add_field(k, v)
108
+ end
109
+ response.body = client.response
110
+ response.instance_variable_set :@read, true
111
+ response
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,18 @@
1
+ RestClient::EmHttpRequest.class_eval do
2
+
3
+ private
4
+
5
+ def build_connect_timeout
6
+ open_timeout
7
+ end
8
+
9
+ def build_inactivity_timeout
10
+ timeout
11
+ end
12
+
13
+ def build_proxy_uri
14
+ return unless RestClient.proxy
15
+ URI.parse(RestClient.proxy)
16
+ end
17
+
18
+ end
@@ -0,0 +1,19 @@
1
+ RestClient::EmHttpRequest.class_eval do
2
+
3
+ private
4
+
5
+ alias_method :parse_url_with_auth, :parse_url_with_auth!
6
+
7
+ def build_connect_timeout
8
+ open_timeout
9
+ end
10
+
11
+ def build_inactivity_timeout
12
+ read_timeout
13
+ end
14
+
15
+ def build_proxy_uri
16
+ proxy_uri
17
+ end
18
+
19
+ end
@@ -0,0 +1,22 @@
1
+ module RestClient
2
+ class Request
3
+ class << self
4
+ def execute(args, &block)
5
+ adapter_name = args.fetch(:adapter, RestClient.adapter)
6
+ klass = get_adapter_klass(adapter_name)
7
+ klass.new(args).execute(&block)
8
+ end
9
+
10
+ def get_adapter_klass(name)
11
+ case name
12
+ when :net_http
13
+ Request
14
+ when :em_http
15
+ EmHttpRequest
16
+ else
17
+ raise NotImplementedError, "Unsupported adapter: #{adapter.inspect}"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-rest-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andriy Yanko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-31 00:00:00.000000000 Z
11
+ date: 2017-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.8'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.8'
27
27
  - !ruby/object:Gem::Dependency
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '2.0'
89
+ version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '2.0'
96
+ version: '3.0'
97
97
  description:
98
98
  email:
99
99
  - andriy.yanko@railsware.com
@@ -104,7 +104,9 @@ files:
104
104
  - ".gitignore"
105
105
  - ".rspec"
106
106
  - ".travis.yml"
107
- - Gemfile
107
+ - CHANGELOG.md
108
+ - Gemfile1
109
+ - Gemfile2
108
110
  - LICENSE
109
111
  - README.md
110
112
  - Rakefile
@@ -112,8 +114,12 @@ files:
112
114
  - bin/setup
113
115
  - em-rest-client.gemspec
114
116
  - lib/em-rest-client.rb
117
+ - lib/em-rest-client/configuration.rb
115
118
  - lib/em-rest-client/em_http_request.rb
116
- - lib/em-rest-client/rest_client.rb
119
+ - lib/em-rest-client/em_http_request/base.rb
120
+ - lib/em-rest-client/em_http_request/v1.rb
121
+ - lib/em-rest-client/em_http_request/v2.rb
122
+ - lib/em-rest-client/request.rb
117
123
  homepage: https://github.com/railsware/em-rest-client
118
124
  licenses:
119
125
  - MIT
@@ -134,8 +140,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
140
  version: '0'
135
141
  requirements: []
136
142
  rubyforge_project:
137
- rubygems_version: 2.4.5.1
143
+ rubygems_version: 2.6.11
138
144
  signing_key:
139
145
  specification_version: 4
140
146
  summary: EventMachine::HttpRequest adapter for HTTP REST client
141
147
  test_files: []
148
+ has_rdoc:
@@ -1,27 +0,0 @@
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