rest-core 3.6.0 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitmodules +3 -0
- data/.travis.yml +0 -1
- data/CHANGES.md +28 -0
- data/Gemfile +0 -2
- data/README.md +14 -95
- data/Rakefile +16 -3
- data/example/simple.rb +1 -0
- data/example/use-cases.rb +15 -3
- data/lib/rest-core.rb +38 -75
- data/lib/rest-core/client/universal.rb +3 -1
- data/lib/rest-core/client_oauth1.rb +64 -59
- data/lib/rest-core/event.rb +9 -11
- data/lib/rest-core/middleware/auth_basic.rb +21 -21
- data/lib/rest-core/middleware/bypass.rb +8 -8
- data/lib/rest-core/middleware/cache.rb +94 -90
- data/lib/rest-core/middleware/clash_response.rb +15 -14
- data/lib/rest-core/middleware/common_logger.rb +27 -26
- data/lib/rest-core/middleware/default_headers.rb +8 -8
- data/lib/rest-core/middleware/default_payload.rb +8 -8
- data/lib/rest-core/middleware/default_query.rb +8 -8
- data/lib/rest-core/middleware/default_site.rb +12 -12
- data/lib/rest-core/middleware/defaults.rb +38 -38
- data/lib/rest-core/middleware/error_detector.rb +10 -10
- data/lib/rest-core/middleware/error_detector_http.rb +6 -4
- data/lib/rest-core/middleware/error_handler.rb +14 -14
- data/lib/rest-core/middleware/follow_redirect.rb +28 -27
- data/lib/rest-core/middleware/json_request.rb +13 -11
- data/lib/rest-core/middleware/json_response.rb +29 -28
- data/lib/rest-core/middleware/oauth1_header.rb +84 -83
- data/lib/rest-core/middleware/oauth2_header.rb +27 -25
- data/lib/rest-core/middleware/oauth2_query.rb +15 -15
- data/lib/rest-core/middleware/query_response.rb +14 -14
- data/lib/rest-core/middleware/retry.rb +25 -23
- data/lib/rest-core/middleware/smash_response.rb +15 -14
- data/lib/rest-core/middleware/timeout.rb +18 -19
- data/lib/rest-core/test.rb +1 -18
- data/lib/rest-core/util/clash.rb +38 -37
- data/lib/rest-core/util/config.rb +40 -39
- data/lib/rest-core/util/dalli_extension.rb +11 -10
- data/lib/rest-core/util/hmac.rb +9 -8
- data/lib/rest-core/util/json.rb +55 -54
- data/lib/rest-core/util/parse_link.rb +13 -12
- data/lib/rest-core/util/parse_query.rb +24 -22
- data/lib/rest-core/version.rb +1 -1
- data/rest-core.gemspec +121 -158
- data/test/test_cache.rb +2 -0
- data/test/test_default_payload.rb +1 -1
- data/test/test_error_handler.rb +5 -4
- data/test/test_timeout.rb +9 -8
- data/test/test_universal.rb +8 -0
- metadata +9 -73
- data/lib/rest-core/builder.rb +0 -162
- data/lib/rest-core/client.rb +0 -277
- data/lib/rest-core/client/simple.rb +0 -2
- data/lib/rest-core/engine.rb +0 -36
- data/lib/rest-core/engine/dry.rb +0 -9
- data/lib/rest-core/engine/http-client.rb +0 -41
- data/lib/rest-core/error.rb +0 -5
- data/lib/rest-core/event_source.rb +0 -137
- data/lib/rest-core/middleware.rb +0 -151
- data/lib/rest-core/promise.rb +0 -249
- data/lib/rest-core/thread_pool.rb +0 -131
- data/lib/rest-core/timer.rb +0 -58
- data/lib/rest-core/util/payload.rb +0 -173
- data/test/test_builder.rb +0 -40
- data/test/test_client.rb +0 -177
- data/test/test_event_source.rb +0 -159
- data/test/test_future.rb +0 -16
- data/test/test_httpclient.rb +0 -118
- data/test/test_payload.rb +0 -204
- data/test/test_promise.rb +0 -146
- data/test/test_simple.rb +0 -38
- data/test/test_thread_pool.rb +0 -10
@@ -1,21 +1,22 @@
|
|
1
1
|
|
2
|
-
require 'rest-core/middleware'
|
3
2
|
require 'rest-core/util/smash'
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
module RestCore
|
5
|
+
class SmashResponse
|
6
|
+
def self.members; [:smash_response]; end
|
7
|
+
include Middleware
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
def call env, &k
|
10
|
+
return app.call(env, &k) if env[DRY]
|
11
|
+
return app.call(env, &k) unless smash_response(env)
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
app.call(env){ |res|
|
14
|
+
if res[RESPONSE_BODY].kind_of?(Hash)
|
15
|
+
yield(res.merge(RESPONSE_BODY => Smash.new(res[RESPONSE_BODY])))
|
16
|
+
else
|
17
|
+
yield(res)
|
18
|
+
end
|
19
|
+
}
|
20
|
+
end
|
20
21
|
end
|
21
22
|
end
|
@@ -1,27 +1,26 @@
|
|
1
1
|
|
2
2
|
require 'timeout'
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
module RestCore
|
5
|
+
class Timeout
|
6
|
+
def self.members; [:timeout]; end
|
7
|
+
include Middleware
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
def call env, &k
|
10
|
+
return app.call(env, &k) if env[DRY] || timeout(env) == 0
|
11
|
+
process(env, &k)
|
12
|
+
end
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
app.call(env.merge(TIMER => timer), &k)
|
19
|
-
rescue Exception
|
20
|
-
timer.cancel
|
21
|
-
raise
|
22
|
-
end
|
14
|
+
def process env, &k
|
15
|
+
timer = PromisePool::Timer.new(timeout(env), timeout_error)
|
16
|
+
app.call(env.merge(TIMER => timer), &k)
|
17
|
+
rescue Exception
|
18
|
+
timer.cancel
|
19
|
+
raise
|
20
|
+
end
|
23
21
|
|
24
|
-
|
25
|
-
|
22
|
+
def timeout_error
|
23
|
+
::Timeout::Error.new('execution expired')
|
24
|
+
end
|
26
25
|
end
|
27
26
|
end
|
data/lib/rest-core/test.rb
CHANGED
@@ -9,21 +9,4 @@ require 'webmock'
|
|
9
9
|
require 'yaml'
|
10
10
|
|
11
11
|
WebMock.disable_net_connect!(:allow_localhost => true)
|
12
|
-
Pork::Executor.
|
13
|
-
|
14
|
-
class Pork::Executor
|
15
|
-
def with_img
|
16
|
-
f = Tempfile.new(['img', '.jpg'])
|
17
|
-
n = File.basename(f.path)
|
18
|
-
f.write('a'*10)
|
19
|
-
f.rewind
|
20
|
-
yield(f, n)
|
21
|
-
ensure
|
22
|
-
f.close!
|
23
|
-
end
|
24
|
-
|
25
|
-
def stub_select_for_stringio
|
26
|
-
stub(IO).select(where([is_a(StringIO)]), [], [],
|
27
|
-
RestCore::EventSource::READ_WAIT){ |rd, *| rd }
|
28
|
-
end
|
29
|
-
end
|
12
|
+
Pork::Executor.include(Muack::API, WebMock::API)
|
data/lib/rest-core/util/clash.rb
CHANGED
@@ -1,50 +1,51 @@
|
|
1
1
|
|
2
|
-
module RestCore
|
3
|
-
class
|
4
|
-
|
2
|
+
module RestCore
|
3
|
+
class Clash
|
4
|
+
Empty = Hash.new(&(l = lambda{|_,_|Hash.new(&l).freeze})).freeze
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
attr_accessor :data
|
7
|
+
def initialize data
|
8
|
+
self.data = data
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
def [] k
|
12
|
+
if data.key?(k)
|
13
|
+
convert(data[k])
|
14
|
+
else
|
15
|
+
Empty
|
16
|
+
end
|
16
17
|
end
|
17
|
-
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
def == rhs
|
20
|
+
if rhs.kind_of?(Clash)
|
21
|
+
data == rhs.data
|
22
|
+
else
|
23
|
+
data == rhs
|
24
|
+
end
|
24
25
|
end
|
25
|
-
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
private
|
28
|
+
def convert value
|
29
|
+
case value
|
30
|
+
when Hash
|
31
|
+
Clash.new(value)
|
32
|
+
when Array
|
33
|
+
value.map{ |ele| convert(ele) }
|
34
|
+
else
|
35
|
+
value
|
36
|
+
end
|
36
37
|
end
|
37
|
-
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
def respond_to_missing? msg, include_private=false
|
40
|
+
data.respond_to?(msg, include_private)
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
def method_missing msg, *args, &block
|
44
|
+
if data.respond_to?(msg)
|
45
|
+
data.public_send(msg, *args, &block)
|
46
|
+
else
|
47
|
+
super
|
48
|
+
end
|
48
49
|
end
|
49
50
|
end
|
50
51
|
end
|
@@ -2,49 +2,50 @@
|
|
2
2
|
require 'erb'
|
3
3
|
require 'yaml'
|
4
4
|
|
5
|
-
module RestCore
|
6
|
-
module
|
7
|
-
|
8
|
-
|
5
|
+
module RestCore
|
6
|
+
module Config
|
7
|
+
extend self
|
8
|
+
DefaultModuleName = 'DefaultAttributes'
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
10
|
+
def load klass, path, env, namespace=nil
|
11
|
+
config = YAML.load(ERB.new(File.read(path)).result(binding))
|
12
|
+
defaults = config[env]
|
13
|
+
return false unless defaults
|
14
|
+
return false unless defaults[namespace] if namespace
|
15
|
+
data = if namespace
|
16
|
+
defaults[namespace]
|
17
|
+
else
|
18
|
+
defaults
|
19
|
+
end
|
20
|
+
raise ArgumentError.new("#{data} is not a hash") unless
|
21
|
+
data.kind_of?(Hash)
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
default_attributes_module(klass).module_eval(
|
24
|
+
data.inject(["extend self\n"]){ |r, (k, v)|
|
25
|
+
# quote strings, leave others free (e.g. false, numbers, etc)
|
26
|
+
r << <<-RUBY
|
27
|
+
def default_#{k}
|
28
|
+
#{v.inspect}
|
29
|
+
end
|
30
|
+
RUBY
|
31
|
+
}.join, __FILE__, __LINE__)
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
def default_attributes_module klass
|
35
|
+
mod = if klass.const_defined?(DefaultModuleName)
|
36
|
+
klass.const_get(DefaultModuleName)
|
37
|
+
else
|
38
|
+
klass.send(:const_set, DefaultModuleName, Module.new)
|
39
|
+
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
singleton_class = if klass.respond_to?(:singleton_class)
|
42
|
+
klass.singleton_class
|
43
|
+
else
|
44
|
+
class << klass; self; end
|
45
|
+
end
|
46
46
|
|
47
|
-
|
48
|
-
|
47
|
+
klass.send(:extend, mod) unless singleton_class < mod
|
48
|
+
mod
|
49
|
+
end
|
49
50
|
end
|
50
51
|
end
|
@@ -1,15 +1,16 @@
|
|
1
1
|
|
2
|
-
module RestCore
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
module RestCore
|
3
|
+
module DalliExtension
|
4
|
+
def [] *args
|
5
|
+
get(*args)
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def []= *args
|
9
|
+
set(*args)
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
def store key, value, expires_in: nil
|
13
|
+
set(key, value, expires_in)
|
14
|
+
end
|
14
15
|
end
|
15
16
|
end
|
data/lib/rest-core/util/hmac.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
|
2
2
|
require 'openssl'
|
3
3
|
|
4
|
-
module RestCore
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
module RestCore
|
5
|
+
module Hmac
|
6
|
+
module_function
|
7
|
+
def sha256 key, data
|
8
|
+
OpenSSL::HMAC.digest('sha256', key, data)
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
def sha1 key, data
|
12
|
+
OpenSSL::HMAC.digest('sha1', key, data)
|
13
|
+
end
|
13
14
|
end
|
14
15
|
end
|
data/lib/rest-core/util/json.rb
CHANGED
@@ -1,64 +1,65 @@
|
|
1
1
|
|
2
|
-
module RestCore
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
2
|
+
module RestCore
|
3
|
+
module Json
|
4
|
+
module MultiJson
|
5
|
+
def self.extended mod
|
6
|
+
mod.const_set(:ParseError, ::MultiJson::DecodeError)
|
7
|
+
end
|
8
|
+
def encode hash
|
9
|
+
::MultiJson.dump(hash)
|
10
|
+
end
|
11
|
+
def decode json
|
12
|
+
::MultiJson.load(json)
|
13
|
+
end
|
13
14
|
end
|
14
|
-
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
module YajlRuby
|
17
|
+
def self.extended mod
|
18
|
+
mod.const_set(:ParseError, Yajl::ParseError)
|
19
|
+
end
|
20
|
+
def encode hash
|
21
|
+
Yajl::Encoder.encode(hash)
|
22
|
+
end
|
23
|
+
def decode json
|
24
|
+
Yajl::Parser.parse(json)
|
25
|
+
end
|
19
26
|
end
|
20
|
-
def encode hash
|
21
|
-
Yajl::Encoder.encode(hash)
|
22
|
-
end
|
23
|
-
def decode json
|
24
|
-
Yajl::Parser.parse(json)
|
25
|
-
end
|
26
|
-
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
module Json
|
29
|
+
def self.extended mod
|
30
|
+
mod.const_set(:ParseError, JSON::ParserError)
|
31
|
+
end
|
32
|
+
def encode hash
|
33
|
+
JSON.dump(hash)
|
34
|
+
end
|
35
|
+
def decode json
|
36
|
+
JSON.parse(json, :quirks_mode => true)
|
37
|
+
end
|
37
38
|
end
|
38
|
-
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
40
|
+
def self.select_json! mod, picked=false
|
41
|
+
if Object.const_defined?(:MultiJson)
|
42
|
+
mod.send(:extend, MultiJson)
|
43
|
+
elsif Object.const_defined?(:Yajl)
|
44
|
+
mod.send(:extend, YajlRuby)
|
45
|
+
elsif Object.const_defined?(:JSON)
|
46
|
+
mod.send(:extend, Json)
|
47
|
+
elsif picked
|
48
|
+
raise LoadError.new(
|
49
|
+
'No JSON library found. Tried: multi_json, yajl-ruby, json.')
|
50
|
+
else
|
51
|
+
# pick a json gem if available
|
52
|
+
%w[multi_json yajl json].each{ |json|
|
53
|
+
begin
|
54
|
+
require json
|
55
|
+
break
|
56
|
+
rescue LoadError
|
57
|
+
end
|
58
|
+
}
|
59
|
+
select_json!(mod, true)
|
60
|
+
end
|
60
61
|
end
|
61
|
-
end
|
62
62
|
|
63
|
-
|
63
|
+
select_json!(self)
|
64
|
+
end
|
64
65
|
end
|