rest-client-adapters 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c0c1369c1493151a996a53f2469318d1a529285a94235c47b6bb6ef0cac9d72a
4
+ data.tar.gz: 1d0db1de1079149064d4d752c3ae747bb0da2d57ca9b711e098b02a6c698eb50
5
+ SHA512:
6
+ metadata.gz: e541a160ebe85b36d0641f28d274f819e330125aa0dd56a3c8d5dc7e7254727eca073372709cfefacb7b6013da146660839e44394eac7ee45b36a96ebfec9ac6
7
+ data.tar.gz: 414fab0835b57c0bbfa6fd66a6a81b0172f17fa8ab552c09b6bcf3bfc542ce6f2c7379f37682e2ad8c9667d172b888b9cac4b01f2b91aaafe67efb4d801bf800
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.6
4
+ - 2.6.3
5
+ cache: bundler
6
+ script:
7
+ - bundle exec rspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0
4
+
5
+ * Rename project from em-rest-client to rest-client-adapters
6
+ * Drop support for rest-client v1.x.x
7
+
8
+ ## 0.1.0
9
+
10
+ * Support RestClient v2.x.x
11
+
12
+ ## 0.0.1
13
+
14
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rest-client', '2.0.2'
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
+ # RestClient Adapters [![Build Status](https://travis-ci.org/railsware/rest-client-adapters.svg?branch=master)](https://travis-ci.org/railsware/rest-client-adapters)
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 'rest-client-adapters'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install rest-client-adapters
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ require 'rest-client-adapters/configuration'
2
+ require 'rest-client-adapters/request'
3
+ require 'rest-client-adapters/em_http_request'
@@ -0,0 +1,9 @@
1
+ require 'rest_client'
2
+
3
+ module RestClient
4
+ class << self
5
+ attr_accessor :adapter
6
+ end
7
+
8
+ self.adapter = :net_http
9
+ end
@@ -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 = open_timeout
67
+ options[:connect_timeout] = connect_timeout
68
+ end
69
+
70
+ if inactivity_timeout = read_timeout
71
+ options[:inactivity_timeout] = inactivity_timeout
72
+ end
73
+
74
+ if 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,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
@@ -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 = "rest-client-adapters"
7
+ spec.version = "0.2.0"
8
+ spec.authors = ["Andriy Yanko"]
9
+ spec.email = ["andriy.yanko@gmail.com"]
10
+
11
+ spec.summary = %q{RestClient Adapters}
12
+ spec.homepage = "https://github.com/railsware/rest-client-adapters"
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", ">= 2.0"
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", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest-client-adapters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Andriy Yanko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-22 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description:
98
+ email:
99
+ - andriy.yanko@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CHANGELOG.md
108
+ - Gemfile
109
+ - LICENSE
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/rest-client-adapters.rb
115
+ - lib/rest-client-adapters/configuration.rb
116
+ - lib/rest-client-adapters/em_http_request.rb
117
+ - lib/rest-client-adapters/request.rb
118
+ - rest-client-adapters.gemspec
119
+ homepage: https://github.com/railsware/rest-client-adapters
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.0.3
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: RestClient Adapters
142
+ test_files: []