orthrus 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/orthrus.rb +9 -6
- data/lib/orthrus/remote_method.rb +6 -6
- data/lib/orthrus/remote_options.rb +24 -0
- data/lib/orthrus/version.rb +1 -1
- data/test/test_class_methods.rb +23 -0
- data/test/test_helper.rb +27 -0
- data/test/test_remote_method.rb +8 -19
- data/test/test_remote_options.rb +30 -0
- metadata +9 -9
- data/.rvmrc +0 -1
- data/test/test_define_remote_method.rb +0 -24
- data/test/test_remote_defaults.rb +0 -26
data/.gitignore
CHANGED
data/lib/orthrus.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'orthrus/remote_method'
|
2
|
+
require 'orthrus/remote_options'
|
2
3
|
|
3
4
|
module Orthrus
|
4
5
|
# Include Orthrus functionality in the given class
|
@@ -8,30 +9,32 @@ module Orthrus
|
|
8
9
|
end
|
9
10
|
|
10
11
|
module ClassMethods
|
12
|
+
# Getter for remote options
|
13
|
+
# @return [RemoteOptions] the remote options for this class
|
14
|
+
def remote_options
|
15
|
+
@remote_options ||= RemoteOptions.new
|
16
|
+
end
|
11
17
|
|
12
18
|
# Define default settings for the remote connection.
|
13
19
|
# @attribute remote_defaults
|
14
20
|
# @param [Hash] options to be set as default
|
15
21
|
# @return [Hash] the current remote default settings
|
16
22
|
def remote_defaults(options)
|
17
|
-
|
18
|
-
@remote_defaults.merge!(options) if options
|
19
|
-
@remote_defaults
|
23
|
+
remote_options.smart_merge!(options)
|
20
24
|
end
|
21
25
|
|
22
26
|
# If we get subclassed, make sure that child inherits the remote defaults
|
23
27
|
# of the parent class.
|
24
28
|
def inherited(child)
|
25
|
-
child.__send__(:remote_defaults,
|
29
|
+
child.__send__(:remote_defaults, remote_options)
|
26
30
|
end
|
27
31
|
|
28
32
|
# Declare a remote method and create a class method as wrapper
|
29
33
|
# @param [Symbol] name of the remote method
|
30
34
|
# @param [Hash] options for the remote method
|
31
35
|
def define_remote_method(name, options = {})
|
32
|
-
remote_options = (@remote_defaults || {}).merge(options)
|
33
36
|
@remote_methods ||= {}
|
34
|
-
@remote_methods[name] = RemoteMethod.new(remote_options)
|
37
|
+
@remote_methods[name] = RemoteMethod.new(remote_options.smart_merge(options))
|
35
38
|
|
36
39
|
class_eval <<-SRC
|
37
40
|
def self.#{name.to_s}(args = {})
|
@@ -7,11 +7,11 @@ module Orthrus
|
|
7
7
|
# @param [Hash] options for the request
|
8
8
|
def initialize(options = {})
|
9
9
|
options[:method] ||= :get
|
10
|
-
@options
|
11
|
-
@base_uri
|
12
|
-
@path
|
13
|
-
@on_success
|
14
|
-
@on_failure
|
10
|
+
@options = options === RemoteOptions ? options : RemoteOptions.new(options)
|
11
|
+
@base_uri = options.delete(:base_uri)
|
12
|
+
@path = options.delete(:path)
|
13
|
+
@on_success = options[:on_success] || lambda { |response| response }
|
14
|
+
@on_failure = options[:on_failure] || lambda { |response| response }
|
15
15
|
end
|
16
16
|
|
17
17
|
# Perform the request, handle response and return the result
|
@@ -19,7 +19,7 @@ module Orthrus
|
|
19
19
|
# @return [Response, Object] the Typhoeus::Response or the result of the on_complete block
|
20
20
|
def run(args = {})
|
21
21
|
url = base_uri + interpolated_path(args)
|
22
|
-
request = Typhoeus::Request.new(url, @options.
|
22
|
+
request = Typhoeus::Request.new(url, @options.smart_merge(args))
|
23
23
|
handle_response(request)
|
24
24
|
Typhoeus::Hydra.hydra.queue request
|
25
25
|
Typhoeus::Hydra.hydra.run
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Orthrus
|
2
|
+
class RemoteOptions < Hash
|
3
|
+
# @param [Hash] options to be set as remote options
|
4
|
+
def initialize(options = {})
|
5
|
+
replace(options)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Merge the given hash and its subhashes.
|
9
|
+
# @param [Hash] other_hash to merge against
|
10
|
+
# @return [nil, RemoteOptions] the merged options
|
11
|
+
def smart_merge(hash)
|
12
|
+
return self if hash.nil?
|
13
|
+
merge(hash) do |key, o, n|
|
14
|
+
o.is_a?(Hash) && n.is_a?(Hash) ? o.merge(n) : n
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Perform smart merge and replace the current content
|
19
|
+
# @params (@see #deep_merge)
|
20
|
+
def smart_merge!(other_hash)
|
21
|
+
replace(smart_merge(other_hash))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/orthrus/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestClassMethods < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_setting_remote_defaults
|
6
|
+
assert_not_nil defaults = Astronomy.instance_variable_get(:@remote_options)
|
7
|
+
assert_equal "http://astronomical.test", defaults[:base_uri]
|
8
|
+
assert_equal "Basic authentication", defaults[:headers][:authentication]
|
9
|
+
assert_equal "json", defaults[:params][:format]
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_overwritting_remote_defaults_in_subclass
|
13
|
+
assert_not_nil defaults = Biology.instance_variable_get(:@remote_options)
|
14
|
+
assert_equal "http://biological.test", defaults[:base_uri]
|
15
|
+
assert_equal "Basic authentication", defaults[:headers][:authentication]
|
16
|
+
assert_equal "json", defaults[:params][:format]
|
17
|
+
assert_equal false, defaults[:params][:boring]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_define_remote_method
|
21
|
+
assert Astronomy.respond_to? :find
|
22
|
+
end
|
23
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -5,4 +5,31 @@ require 'orthrus'
|
|
5
5
|
require 'json'
|
6
6
|
|
7
7
|
class Test::Unit::TestCase
|
8
|
+
class Astronomy
|
9
|
+
include Orthrus
|
10
|
+
remote_defaults :headers => { :authentication => "Basic authentication" },
|
11
|
+
:base_uri => "http://astronomical.test",
|
12
|
+
:params => { :format => "json" }
|
13
|
+
|
14
|
+
define_remote_method :find,
|
15
|
+
:path => "/planets/:identifier",
|
16
|
+
:params => { :include_details => true }
|
17
|
+
end
|
18
|
+
|
19
|
+
class Biology < Astronomy
|
20
|
+
remote_defaults :base_uri => "http://biological.test",
|
21
|
+
:params => { :boring => false }
|
22
|
+
end
|
23
|
+
|
24
|
+
def setup
|
25
|
+
hydra = Typhoeus::Hydra.hydra
|
26
|
+
@index_response = Typhoeus::Response.new(:code => 200, :body => '{ "planets" : ["mars", "earth", "venus"] }', :time => 0.3)
|
27
|
+
@mars_response = Typhoeus::Response.new(:code => 200, :body => '{ "mars" : { "density" : "3.9335 g/cm3" , "temperature" : "210K" } }', :time => 0.3)
|
28
|
+
@error_response = Typhoeus::Response.new(:code => 404, :body => '{ "eris" : "is no planet but a TNO" }', :time => 0.3)
|
29
|
+
@put_response = Typhoeus::Response.new(:code => 403, :body => 'Creating planets is not your business!', :time => 0.3)
|
30
|
+
hydra.stub(:get, "http://astronomical.test/planets").and_return(@index_response)
|
31
|
+
hydra.stub(:get, "http://astronomical.test/planets/mars").and_return(@mars_response)
|
32
|
+
hydra.stub(:get, "http://astronomical.test/planets/eris").and_return(@error_response)
|
33
|
+
hydra.stub(:put, "http://astronomical.test/planets").and_return(@put_response)
|
34
|
+
end
|
8
35
|
end
|
data/test/test_remote_method.rb
CHANGED
@@ -1,17 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TestDefineRemoteMethod < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
hydra = Typhoeus::Hydra.hydra
|
6
|
-
@index_response = Typhoeus::Response.new(:code => 200, :body => '{ "planets" : ["mars", "earth", "venus"] }', :time => 0.3)
|
7
|
-
@show_response = Typhoeus::Response.new(:code => 200, :body => '{ "mars" : { "density" : "3.9335 g/cm3" , "temperature" : "210K" } }', :time => 0.3)
|
8
|
-
@error_response = Typhoeus::Response.new(:code => 404, :body => '{ "eris" : "is no planet but a TNO" }', :time => 0.3)
|
9
|
-
@put_response = Typhoeus::Response.new(:code => 403, :body => 'Creating planets is not your business!', :time => 0.3)
|
10
|
-
hydra.stub(:get, "http://astronomical.joe/planets").and_return(@index_response)
|
11
|
-
hydra.stub(:get, "http://astronomical.joe/planets/mars").and_return(@show_response)
|
12
|
-
hydra.stub(:get, "http://astronomical.joe/planets/eris").and_return(@error_response)
|
13
|
-
hydra.stub(:put, "http://astronomical.joe/planets").and_return(@put_response)
|
14
|
-
end
|
15
4
|
|
16
5
|
def test_path_interpolation
|
17
6
|
remote_method = Orthrus::RemoteMethod.new :path => "/some/:id/with/:child"
|
@@ -23,7 +12,7 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
|
|
23
12
|
|
24
13
|
def test_simple_remote_method_get
|
25
14
|
remote_method = Orthrus::RemoteMethod.new(
|
26
|
-
:base_uri => "http://astronomical.
|
15
|
+
:base_uri => "http://astronomical.test",
|
27
16
|
:path => "/planets",
|
28
17
|
:method => :get
|
29
18
|
)
|
@@ -32,7 +21,7 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
|
|
32
21
|
|
33
22
|
def test_simple_remote_method_with_get_as_default
|
34
23
|
remote_method = Orthrus::RemoteMethod.new(
|
35
|
-
:base_uri => "http://astronomical.
|
24
|
+
:base_uri => "http://astronomical.test",
|
36
25
|
:path => "/planets"
|
37
26
|
)
|
38
27
|
assert_equal @index_response.body, remote_method.run.body
|
@@ -40,16 +29,16 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
|
|
40
29
|
|
41
30
|
def test_remote_method_with_interpolation
|
42
31
|
remote_method = Orthrus::RemoteMethod.new(
|
43
|
-
:base_uri => "http://astronomical.
|
32
|
+
:base_uri => "http://astronomical.test",
|
44
33
|
:path => "/planets/:identifier",
|
45
34
|
:method => :get
|
46
35
|
)
|
47
|
-
assert_equal @
|
36
|
+
assert_equal @mars_response.body, remote_method.run(:identifier => :mars).body
|
48
37
|
end
|
49
38
|
|
50
39
|
def test_remote_method_with_success_handler
|
51
40
|
remote_method = Orthrus::RemoteMethod.new(
|
52
|
-
:base_uri => "http://astronomical.
|
41
|
+
:base_uri => "http://astronomical.test",
|
53
42
|
:path => "/planets/:identifier",
|
54
43
|
:on_success => lambda { |response| JSON.parse(response.body) },
|
55
44
|
:method => :get
|
@@ -61,7 +50,7 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
|
|
61
50
|
|
62
51
|
def test_remote_method_with_failure_handler
|
63
52
|
remote_method = Orthrus::RemoteMethod.new(
|
64
|
-
:base_uri => "http://astronomical.
|
53
|
+
:base_uri => "http://astronomical.test",
|
65
54
|
:path => "/planets/:identifier",
|
66
55
|
:on_failure => lambda { |response| JSON.parse(response.body) },
|
67
56
|
:method => :get
|
@@ -72,11 +61,11 @@ class TestDefineRemoteMethod < Test::Unit::TestCase
|
|
72
61
|
|
73
62
|
def test_remote_method_put
|
74
63
|
remote_method = Orthrus::RemoteMethod.new(
|
75
|
-
:base_uri => "http://astronomical.
|
64
|
+
:base_uri => "http://astronomical.test",
|
76
65
|
:path => "/planets",
|
77
66
|
:method => :put
|
78
67
|
)
|
79
|
-
response = remote_method.run(:body => '{
|
68
|
+
response = remote_method.run(:body => '{"planet":"naboo"}')
|
80
69
|
assert_equal "Creating planets is not your business!", response.body
|
81
70
|
end
|
82
71
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestRemoteOptions < Test::Unit::TestCase
|
4
|
+
def test_remote_options_init
|
5
|
+
remote_options = Orthrus::RemoteOptions.new(:base_uri => "http://astronomical.joe", :params => {:format => :json})
|
6
|
+
assert_kind_of Hash, remote_options
|
7
|
+
assert_equal "http://astronomical.joe", remote_options[:base_uri]
|
8
|
+
assert_equal :json, remote_options[:params][:format]
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_remote_options_merge
|
12
|
+
remote_options = Orthrus::RemoteOptions.new(:params => {:format => :json})
|
13
|
+
remote_options = remote_options.smart_merge(:params => {:planet => :juno})
|
14
|
+
assert_equal :json, remote_options[:params][:format]
|
15
|
+
assert_equal :juno, remote_options[:params][:planet]
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_remote_options_merge_on_self
|
19
|
+
remote_options = Orthrus::RemoteOptions.new(:params => {:format => :json})
|
20
|
+
remote_options.smart_merge!(:params => {:planet => :juno})
|
21
|
+
assert_equal :json, remote_options[:params][:format]
|
22
|
+
assert_equal :juno, remote_options[:params][:planet]
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_remote_options_merge_with_nil
|
26
|
+
remote_options = Orthrus::RemoteOptions.new(:params => {:format => :xml})
|
27
|
+
remote_options.smart_merge!(nil)
|
28
|
+
assert_equal :xml, remote_options[:params][:format]
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: orthrus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Johannes Opper
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-06-
|
18
|
+
date: 2011-06-18 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -74,19 +74,19 @@ extra_rdoc_files: []
|
|
74
74
|
|
75
75
|
files:
|
76
76
|
- .gitignore
|
77
|
-
- .rvmrc
|
78
77
|
- Gemfile
|
79
78
|
- LICENSE
|
80
79
|
- README.markdown
|
81
80
|
- Rakefile
|
82
81
|
- lib/orthrus.rb
|
83
82
|
- lib/orthrus/remote_method.rb
|
83
|
+
- lib/orthrus/remote_options.rb
|
84
84
|
- lib/orthrus/version.rb
|
85
85
|
- orthrus.gemspec
|
86
|
-
- test/
|
86
|
+
- test/test_class_methods.rb
|
87
87
|
- test/test_helper.rb
|
88
|
-
- test/test_remote_defaults.rb
|
89
88
|
- test/test_remote_method.rb
|
89
|
+
- test/test_remote_options.rb
|
90
90
|
has_rdoc: true
|
91
91
|
homepage: http://github.com/xijo/orthrus
|
92
92
|
licenses: []
|
@@ -122,7 +122,7 @@ signing_key:
|
|
122
122
|
specification_version: 3
|
123
123
|
summary: Remote method handling for Typhoeus
|
124
124
|
test_files:
|
125
|
-
- test/
|
125
|
+
- test/test_class_methods.rb
|
126
126
|
- test/test_helper.rb
|
127
|
-
- test/test_remote_defaults.rb
|
128
127
|
- test/test_remote_method.rb
|
128
|
+
- test/test_remote_options.rb
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use 1.8.7@orthrus
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestDefineRemoteMethod < Test::Unit::TestCase
|
4
|
-
class A
|
5
|
-
include Orthrus
|
6
|
-
define_remote_method :method_on_a,
|
7
|
-
:path => 'entities',
|
8
|
-
:base_uri => "http://fancydomain.com"
|
9
|
-
end
|
10
|
-
|
11
|
-
class B
|
12
|
-
include Orthrus
|
13
|
-
remote_defaults :base_uri => "http://superfancy.com"
|
14
|
-
define_remote_method :method_on_b, :path => "entities/index"
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_define_remote_method_without_defaults
|
18
|
-
assert A.respond_to? :method_on_a
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_define_remote_method_with_remote_defaults
|
22
|
-
assert B.respond_to? :method_on_b
|
23
|
-
end
|
24
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestRemoteDefaults < Test::Unit::TestCase
|
4
|
-
class A
|
5
|
-
include Orthrus
|
6
|
-
remote_defaults :headers => { :authentication => "Basic someting" },
|
7
|
-
:base_uri => "http://fancydomain.com"
|
8
|
-
end
|
9
|
-
|
10
|
-
class B < A
|
11
|
-
remote_defaults :base_uri => "http://superfancy.com"
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_setting_remote_defaults
|
15
|
-
assert_not_nil defaults = A.instance_variable_get(:@remote_defaults)
|
16
|
-
assert_equal "http://fancydomain.com", defaults[:base_uri]
|
17
|
-
assert_equal "Basic someting", defaults[:headers][:authentication]
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_overwritting_remote_defaults_in_subclass
|
21
|
-
assert_not_nil defaults = B.instance_variable_get(:@remote_defaults)
|
22
|
-
assert_equal "http://superfancy.com", defaults[:base_uri]
|
23
|
-
assert_equal "Basic someting", defaults[:headers][:authentication]
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|