paypal-sdk-http-adapters 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/paypal-sdk-http-adapters.rb +3 -0
- data/lib/paypal-sdk-http-adapters/configuration.rb +26 -0
- data/lib/paypal-sdk-http-adapters/http_helper.rb +7 -0
- data/lib/paypal-sdk/http_adapters/em_http.rb +171 -0
- data/lib/paypal-sdk/http_adapters/net_http.rb +29 -0
- data/paypal-sdk-http-adapters.gemspec +28 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 25019fe9055eb91fca29636b8505d0cedbb48ac0
|
4
|
+
data.tar.gz: e324d6c00ab00d42237c54b29d4aff3083ea1ad3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a9d7915cd4896b76001480c941d3608a929b83c0471485957a1a1c0afbe0c381650b8b22ed19010ba58477975016ff2570d687892d62e23237c92c65cd379682
|
7
|
+
data.tar.gz: d2ee25bc4daac1f1e195e9f5b3c563e6be59fa73b6fdb172d7033d12c8c197057ad0fbea6843e6a0e3aa77d87c4b2aff16a9f9c48d817c269187401198524162
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# HTTP Adapters for PayPal SDK[![Build Status](https://travis-ci.org/railsware/paypal-sdk-http-adapters.svg?branch=master)](https://travis-ci.org/railsware/paypal-sdk-http-adapters)
|
2
|
+
|
3
|
+
This gem extends PayPal SDK and allows you to set specific http library.
|
4
|
+
|
5
|
+
Currently it supports two adapters:
|
6
|
+
|
7
|
+
* `:net_http` - Net::HTTP (default, blocking I/O)
|
8
|
+
* `:em_http` - EM::Http (non-blocking I/0)
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'paypal-sdk-http-adapters'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install paypal-sdk-http-adapters
|
26
|
+
|
27
|
+
## NetHttp Adapter
|
28
|
+
|
29
|
+
```
|
30
|
+
PayPal::SDK.http_adapter_name = :net_http
|
31
|
+
```
|
32
|
+
|
33
|
+
This is default adapter that actually is very simple wrapper for standard `Net::HTTP`.
|
34
|
+
|
35
|
+
## EmHttp Adapter
|
36
|
+
|
37
|
+
```
|
38
|
+
PayPal::SDK.http_adapter_name = :em_http
|
39
|
+
```
|
40
|
+
|
41
|
+
This is adapter for `EM::Http` library.
|
42
|
+
|
43
|
+
When EventMachine is already running we assume that you are responsible for Fiber allocation.
|
44
|
+
You can add `Rack::FiberPool` to your application middleware and it automatically provides a Fiber for each incoming HTTP request.
|
45
|
+
It creates pool of Fibers and re-use fiber for each incoming HTTP request.
|
46
|
+
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/paypal/PayPal-Ruby-SDK
|
55
|
+
* https://github.com/paypal/sdk-core-ruby
|
56
|
+
* https://github.com/igrigorik/em-http-request
|
57
|
+
* https://github.com/alebsack/rack-fiber_pool
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "paypal-sdk-http-adapters"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module PayPal::SDK
|
2
|
+
class << self
|
3
|
+
attr_accessor :http_adapter_name
|
4
|
+
|
5
|
+
def current_http_adapter
|
6
|
+
load_http_adapter(http_adapter_name) unless http_adapters.key?(http_adapter_name)
|
7
|
+
http_adapters.fetch(http_adapter_name)
|
8
|
+
end
|
9
|
+
|
10
|
+
def load_http_adapter(name)
|
11
|
+
require "paypal-sdk/http_adapters/#{name}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def register_http_adapter(name, klass)
|
15
|
+
http_adapters[name] = klass
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def http_adapters
|
21
|
+
@http_adapters ||= {}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
self.http_adapter_name = :net_http
|
26
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require 'em-http'
|
2
|
+
require 'fiber'
|
3
|
+
|
4
|
+
module PayPal::SDK
|
5
|
+
module HttpAdapters
|
6
|
+
class EmHttp
|
7
|
+
attr_reader :connection_options
|
8
|
+
|
9
|
+
def initialize(uri, proxy)
|
10
|
+
@uri = uri
|
11
|
+
@connection_options = {
|
12
|
+
tls: {}
|
13
|
+
}
|
14
|
+
@connection_options[:proxy] = {
|
15
|
+
host: proxy.host,
|
16
|
+
port: proxy.port,
|
17
|
+
authorization: [proxy.user, proxy.password]
|
18
|
+
} if proxy
|
19
|
+
end
|
20
|
+
|
21
|
+
def open_timeout=(value)
|
22
|
+
@connection_options[:connect_timeout] = value
|
23
|
+
end
|
24
|
+
|
25
|
+
def read_timeout=(value)
|
26
|
+
@connection_options[:inactivity_timeout] = value
|
27
|
+
end
|
28
|
+
|
29
|
+
def use_ssl=(value)
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def ca_file=(value)
|
34
|
+
@connection_options[:tls][:cert_chain_file] = value
|
35
|
+
end
|
36
|
+
|
37
|
+
def verify_mode=(value)
|
38
|
+
@connection_options[:tls][:verify_peer] = (value > 0)
|
39
|
+
end
|
40
|
+
|
41
|
+
def ssl_version=(value)
|
42
|
+
@connection_options[:tls][:ssl_version] = value
|
43
|
+
end
|
44
|
+
|
45
|
+
def cert=(value)
|
46
|
+
raise NotImplementedError, "cert=#{value}" if value
|
47
|
+
end
|
48
|
+
|
49
|
+
def key=(value)
|
50
|
+
raise NotImplementedError, "key=#{value}" if value
|
51
|
+
end
|
52
|
+
|
53
|
+
#########################################################################
|
54
|
+
|
55
|
+
%i[get head delete].each do |method|
|
56
|
+
define_method method do |path, header|
|
57
|
+
request(method, path, header, nil)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
%i[put post patch options].each do |method|
|
62
|
+
define_method method do |path, body, header|
|
63
|
+
request(method, path, header, body)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def request(method, path, header, body)
|
68
|
+
within_session do
|
69
|
+
uri = URI.parse(path)
|
70
|
+
|
71
|
+
client = run_request(@connection, method,
|
72
|
+
path: uri.path,
|
73
|
+
query: uri.query,
|
74
|
+
head: header,
|
75
|
+
body: body
|
76
|
+
)
|
77
|
+
|
78
|
+
if client.error
|
79
|
+
raise client.error
|
80
|
+
else
|
81
|
+
build_response(client)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def start
|
87
|
+
raise 'session is already started' if @started
|
88
|
+
|
89
|
+
begin
|
90
|
+
@started = true
|
91
|
+
@connection = EM::HttpRequest.new(@uri, @connection_options)
|
92
|
+
|
93
|
+
within_reactor do
|
94
|
+
yield(self)
|
95
|
+
end
|
96
|
+
ensure
|
97
|
+
@connection = nil
|
98
|
+
@started = nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def started?
|
103
|
+
@started
|
104
|
+
end
|
105
|
+
|
106
|
+
protected
|
107
|
+
|
108
|
+
def within_session
|
109
|
+
if started?
|
110
|
+
yield
|
111
|
+
else
|
112
|
+
start do
|
113
|
+
yield
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def within_reactor
|
119
|
+
result = nil
|
120
|
+
|
121
|
+
if EM.reactor_running?
|
122
|
+
result = yield
|
123
|
+
else
|
124
|
+
EM.run do
|
125
|
+
Fiber.new do
|
126
|
+
result = yield
|
127
|
+
EM.stop
|
128
|
+
end.resume
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
result
|
133
|
+
end
|
134
|
+
|
135
|
+
def run_request(connection, method, options)
|
136
|
+
connection.setup_request(method, options).tap do |client|
|
137
|
+
unless client.error
|
138
|
+
fiber = Fiber.current
|
139
|
+
client.callback { fiber.resume(client) }
|
140
|
+
client.errback { fiber.resume(client) }
|
141
|
+
Fiber.yield
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def build_response(client)
|
147
|
+
klass = Net::HTTPResponse.send(:response_class,
|
148
|
+
client.response_header.status.to_s
|
149
|
+
)
|
150
|
+
|
151
|
+
response = klass.new(
|
152
|
+
client.response_header.http_version,
|
153
|
+
client.response_header.status.to_s,
|
154
|
+
client.response_header.http_reason
|
155
|
+
)
|
156
|
+
|
157
|
+
client.response_header.raw.each do |k, v|
|
158
|
+
response.add_field(k, v)
|
159
|
+
end
|
160
|
+
|
161
|
+
response.body = client.response
|
162
|
+
|
163
|
+
response.instance_variable_set :@read, true
|
164
|
+
|
165
|
+
response
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
self.register_http_adapter :em_http, HttpAdapters::EmHttp
|
171
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module PayPal::SDK
|
4
|
+
module HttpAdapters
|
5
|
+
class NetHttp
|
6
|
+
class << self
|
7
|
+
def new(uri, proxy)
|
8
|
+
if proxy
|
9
|
+
Net::HTTP.new(
|
10
|
+
uri.host,
|
11
|
+
uri.port,
|
12
|
+
proxy.host,
|
13
|
+
proxy.port,
|
14
|
+
proxy.user,
|
15
|
+
proxy.password
|
16
|
+
)
|
17
|
+
else
|
18
|
+
Net::HTTP.new(
|
19
|
+
uri.host,
|
20
|
+
uri.port
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
self.register_http_adapter :net_http, HttpAdapters::NetHttp
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
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 = "paypal-sdk-http-adapters"
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["Andriy Yanko"]
|
9
|
+
spec.email = ["andriy.yanko@railsware.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{HTTP Adapters for PayPal SDK}
|
12
|
+
spec.homepage = "https://github.com/railsware/paypal-sdk-http-adapters"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "paypal-sdk-core", ">= 0.3"
|
23
|
+
spec.add_development_dependency "em-http-request", "~> 1.1"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "webmock", "~> 3.1"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paypal-sdk-http-adapters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andriy Yanko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: paypal-sdk-core
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.3'
|
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.15'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.15'
|
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.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.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.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.1'
|
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
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/paypal-sdk-http-adapters.rb
|
113
|
+
- lib/paypal-sdk-http-adapters/configuration.rb
|
114
|
+
- lib/paypal-sdk-http-adapters/http_helper.rb
|
115
|
+
- lib/paypal-sdk/http_adapters/em_http.rb
|
116
|
+
- lib/paypal-sdk/http_adapters/net_http.rb
|
117
|
+
- paypal-sdk-http-adapters.gemspec
|
118
|
+
homepage: https://github.com/railsware/paypal-sdk-http-adapters
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.6.11
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: HTTP Adapters for PayPal SDK
|
142
|
+
test_files: []
|
143
|
+
has_rdoc:
|