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.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +3 -0
  3. data/.travis.yml +0 -1
  4. data/CHANGES.md +28 -0
  5. data/Gemfile +0 -2
  6. data/README.md +14 -95
  7. data/Rakefile +16 -3
  8. data/example/simple.rb +1 -0
  9. data/example/use-cases.rb +15 -3
  10. data/lib/rest-core.rb +38 -75
  11. data/lib/rest-core/client/universal.rb +3 -1
  12. data/lib/rest-core/client_oauth1.rb +64 -59
  13. data/lib/rest-core/event.rb +9 -11
  14. data/lib/rest-core/middleware/auth_basic.rb +21 -21
  15. data/lib/rest-core/middleware/bypass.rb +8 -8
  16. data/lib/rest-core/middleware/cache.rb +94 -90
  17. data/lib/rest-core/middleware/clash_response.rb +15 -14
  18. data/lib/rest-core/middleware/common_logger.rb +27 -26
  19. data/lib/rest-core/middleware/default_headers.rb +8 -8
  20. data/lib/rest-core/middleware/default_payload.rb +8 -8
  21. data/lib/rest-core/middleware/default_query.rb +8 -8
  22. data/lib/rest-core/middleware/default_site.rb +12 -12
  23. data/lib/rest-core/middleware/defaults.rb +38 -38
  24. data/lib/rest-core/middleware/error_detector.rb +10 -10
  25. data/lib/rest-core/middleware/error_detector_http.rb +6 -4
  26. data/lib/rest-core/middleware/error_handler.rb +14 -14
  27. data/lib/rest-core/middleware/follow_redirect.rb +28 -27
  28. data/lib/rest-core/middleware/json_request.rb +13 -11
  29. data/lib/rest-core/middleware/json_response.rb +29 -28
  30. data/lib/rest-core/middleware/oauth1_header.rb +84 -83
  31. data/lib/rest-core/middleware/oauth2_header.rb +27 -25
  32. data/lib/rest-core/middleware/oauth2_query.rb +15 -15
  33. data/lib/rest-core/middleware/query_response.rb +14 -14
  34. data/lib/rest-core/middleware/retry.rb +25 -23
  35. data/lib/rest-core/middleware/smash_response.rb +15 -14
  36. data/lib/rest-core/middleware/timeout.rb +18 -19
  37. data/lib/rest-core/test.rb +1 -18
  38. data/lib/rest-core/util/clash.rb +38 -37
  39. data/lib/rest-core/util/config.rb +40 -39
  40. data/lib/rest-core/util/dalli_extension.rb +11 -10
  41. data/lib/rest-core/util/hmac.rb +9 -8
  42. data/lib/rest-core/util/json.rb +55 -54
  43. data/lib/rest-core/util/parse_link.rb +13 -12
  44. data/lib/rest-core/util/parse_query.rb +24 -22
  45. data/lib/rest-core/version.rb +1 -1
  46. data/rest-core.gemspec +121 -158
  47. data/test/test_cache.rb +2 -0
  48. data/test/test_default_payload.rb +1 -1
  49. data/test/test_error_handler.rb +5 -4
  50. data/test/test_timeout.rb +9 -8
  51. data/test/test_universal.rb +8 -0
  52. metadata +9 -73
  53. data/lib/rest-core/builder.rb +0 -162
  54. data/lib/rest-core/client.rb +0 -277
  55. data/lib/rest-core/client/simple.rb +0 -2
  56. data/lib/rest-core/engine.rb +0 -36
  57. data/lib/rest-core/engine/dry.rb +0 -9
  58. data/lib/rest-core/engine/http-client.rb +0 -41
  59. data/lib/rest-core/error.rb +0 -5
  60. data/lib/rest-core/event_source.rb +0 -137
  61. data/lib/rest-core/middleware.rb +0 -151
  62. data/lib/rest-core/promise.rb +0 -249
  63. data/lib/rest-core/thread_pool.rb +0 -131
  64. data/lib/rest-core/timer.rb +0 -58
  65. data/lib/rest-core/util/payload.rb +0 -173
  66. data/test/test_builder.rb +0 -40
  67. data/test/test_client.rb +0 -177
  68. data/test/test_event_source.rb +0 -159
  69. data/test/test_future.rb +0 -16
  70. data/test/test_httpclient.rb +0 -118
  71. data/test/test_payload.rb +0 -204
  72. data/test/test_promise.rb +0 -146
  73. data/test/test_simple.rb +0 -38
  74. 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
- class RestCore::SmashResponse
6
- def self.members; [:smash_response]; end
7
- include RestCore::Middleware
4
+ module RestCore
5
+ class SmashResponse
6
+ def self.members; [:smash_response]; end
7
+ include Middleware
8
8
 
9
- def call env, &k
10
- return app.call(env, &k) if env[DRY]
11
- return app.call(env, &k) unless smash_response(env)
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
- 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
- }
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
- require 'rest-core/middleware'
5
- require 'rest-core/timer'
4
+ module RestCore
5
+ class Timeout
6
+ def self.members; [:timeout]; end
7
+ include Middleware
6
8
 
7
- class RestCore::Timeout
8
- def self.members; [:timeout]; end
9
- include RestCore::Middleware
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
- def call env, &k
12
- return app.call(env, &k) if env[DRY] || timeout(env) == 0
13
- process(env, &k)
14
- end
15
-
16
- def process env, &k
17
- timer = Timer.new(timeout(env), timeout_error)
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
- def timeout_error
25
- ::Timeout::Error.new('execution expired')
22
+ def timeout_error
23
+ ::Timeout::Error.new('execution expired')
24
+ end
26
25
  end
27
26
  end
@@ -9,21 +9,4 @@ require 'webmock'
9
9
  require 'yaml'
10
10
 
11
11
  WebMock.disable_net_connect!(:allow_localhost => true)
12
- Pork::Executor.__send__(:include, Muack::API, WebMock::API)
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)
@@ -1,50 +1,51 @@
1
1
 
2
- module RestCore; end
3
- class RestCore::Clash
4
- Empty = Hash.new(&(l = lambda{|_,_|Hash.new(&l).freeze})).freeze
2
+ module RestCore
3
+ class Clash
4
+ Empty = Hash.new(&(l = lambda{|_,_|Hash.new(&l).freeze})).freeze
5
5
 
6
- attr_accessor :data
7
- def initialize data
8
- self.data = data
9
- end
6
+ attr_accessor :data
7
+ def initialize data
8
+ self.data = data
9
+ end
10
10
 
11
- def [] k
12
- if data.key?(k)
13
- convert(data[k])
14
- else
15
- Empty
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
- def == rhs
20
- if rhs.kind_of?(RestCore::Clash)
21
- data == rhs.data
22
- else
23
- data == rhs
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
- private
28
- def convert value
29
- case value
30
- when Hash
31
- RestCore::Clash.new(value)
32
- when Array
33
- value.map{ |ele| convert(ele) }
34
- else
35
- value
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
- def respond_to_missing? msg, include_private=false
40
- data.respond_to?(msg, include_private)
41
- end
39
+ def respond_to_missing? msg, include_private=false
40
+ data.respond_to?(msg, include_private)
41
+ end
42
42
 
43
- def method_missing msg, *args, &block
44
- if data.respond_to?(msg)
45
- data.public_send(msg, *args, &block)
46
- else
47
- super
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; end
6
- module RestCore::Config
7
- extend self
8
- DefaultModuleName = 'DefaultAttributes'
5
+ module RestCore
6
+ module Config
7
+ extend self
8
+ DefaultModuleName = 'DefaultAttributes'
9
9
 
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)
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
- 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
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
- 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
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
- singleton_class = if klass.respond_to?(:singleton_class)
42
- klass.singleton_class
43
- else
44
- class << klass; self; end
45
- end
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
- klass.send(:extend, mod) unless singleton_class < mod
48
- mod
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; end
3
- module RestCore::DalliExtension
4
- def [] *args
5
- get(*args)
6
- end
2
+ module RestCore
3
+ module DalliExtension
4
+ def [] *args
5
+ get(*args)
6
+ end
7
7
 
8
- def []= *args
9
- set(*args)
10
- end
8
+ def []= *args
9
+ set(*args)
10
+ end
11
11
 
12
- def store key, value, expires_in: nil
13
- set(key, value, expires_in)
12
+ def store key, value, expires_in: nil
13
+ set(key, value, expires_in)
14
+ end
14
15
  end
15
16
  end
@@ -1,14 +1,15 @@
1
1
 
2
2
  require 'openssl'
3
3
 
4
- module RestCore; end
5
- module RestCore::Hmac
6
- module_function
7
- def sha256 key, data
8
- OpenSSL::HMAC.digest('sha256', key, data)
9
- end
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
- def sha1 key, data
12
- OpenSSL::HMAC.digest('sha1', key, data)
11
+ def sha1 key, data
12
+ OpenSSL::HMAC.digest('sha1', key, data)
13
+ end
13
14
  end
14
15
  end
@@ -1,64 +1,65 @@
1
1
 
2
- module RestCore; end
3
- module RestCore::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)
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
- module YajlRuby
17
- def self.extended mod
18
- mod.const_set(:ParseError, Yajl::ParseError)
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
- 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)
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
- 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)
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
- select_json!(self)
63
+ select_json!(self)
64
+ end
64
65
  end