s_savon_model 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ .DS_Store
2
+ .yardoc
3
+ doc
4
+ coverage
5
+ tmp
6
+ *~
7
+ *.gem
8
+ .bundle
9
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Daniel Harrington
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ Savon::Model
2
+ ============
3
+
4
+ Model for SOAP service oriented applications.
5
+
6
+ [Bugs](http://github.com/rubiii/savon_model/issues) | [Docs](http://rubydoc.info/gems/savon_model/frames)
7
+
8
+ Installation
9
+ ------------
10
+
11
+ The gem is available through [Rubygems](http://rubygems.org/gems/savon_model) and can be installed via:
12
+
13
+ $ gem install savon_model
14
+
15
+ Getting started
16
+ ---------------
17
+
18
+ class User
19
+ include Savon::Model
20
+
21
+ client do [1]
22
+ http.headers["Pragma"] = "no-cache"
23
+ end
24
+
25
+ document "http://example.com/users?wsdl" [2.0]
26
+ basic_auth "login", "password" [2.1]
27
+ endpoint "http://example.com/users" [2.2]
28
+ namespace "http://v1.example.com/users" [3]
29
+
30
+ actions :get_user, :get_all_users [4]
31
+
32
+ def self.all
33
+ get_all_users.to_array [5]
34
+ end
35
+
36
+ def self.find(id)
37
+ get_user(:id => id).to_hash [6]
38
+ end
39
+
40
+ def self.delete(id)
41
+ client.request(:delete_user) do [7]
42
+ soap.body = { :id => 1 }
43
+ end.to_hash
44
+ end
45
+ end
46
+
47
+ 1. You can call the `client` method with a block of settings to be passed to `Savon::Client.new`.
48
+ The `client` method memoizes a `Savon::Client` instance, so you need to call this method before
49
+ it gets called by any other method.
50
+
51
+ 2. Sets the SOAP endpoint.
52
+
53
+ 3. Sets the SOAP namespace.
54
+
55
+ 4. Specifies the SOAP actions provided by the service. This method dynamically creates both class
56
+ and instance methods named after the arguments. These methods accept an optional SOAP body Hash
57
+ or XML String to be passed to the `Savon::Client` instance.
58
+
59
+ 5. `User.all` calls the `get_all_users` SOAP action and returns a `Savon::Response`.
60
+
61
+ 6. `User.find` calls the `get_user` SOAP action with a SOAP body Hash and returns a `Savon::Response`.
62
+
63
+ 7. This is an example of what happens "behind the scenes" on [6]. You can always use the `client`
64
+ method to directly access and use the `Savon::Client` instance.
@@ -0,0 +1,40 @@
1
+ require "rake"
2
+
3
+ begin
4
+ require "yard"
5
+
6
+ YARD::Rake::YardocTask.new do |t|
7
+ t.files = ["README.md", "lib/**/*.rb"]
8
+ end
9
+ rescue LoadError
10
+ desc message = %{"gem install yard" to generate documentation}
11
+ task("yard") { abort message }
12
+ end
13
+
14
+ begin
15
+ require "metric_fu"
16
+
17
+ MetricFu::Configuration.run do |c|
18
+ c.metrics = [:churn, :flog, :flay, :reek, :roodi, :saikuro] # :rcov seems to be broken
19
+ c.graphs = [:flog, :flay, :reek, :roodi]
20
+ c.flay = { :dirs_to_flay => ["lib"], :minimum_score => 20 }
21
+ c.rcov[:rcov_opts] << "-Ilib -Ispec"
22
+ end
23
+ rescue LoadError
24
+ desc message = %{"gem install metric_fu" to generate metrics}
25
+ task("metrics:all") { abort message }
26
+ end
27
+
28
+ begin
29
+ require "rspec/core/rake_task"
30
+
31
+ RSpec::Core::RakeTask.new do |t|
32
+ t.rspec_opts = %w(-fd -c)
33
+ end
34
+ rescue LoadError
35
+ task :spec do
36
+ abort "Run 'gem install rspec' to be able to run specs"
37
+ end
38
+ end
39
+
40
+ task :default => :spec
@@ -0,0 +1,120 @@
1
+ require "savon"
2
+
3
+ module Savon
4
+
5
+ # = Savon::Model
6
+ #
7
+ # Model for SOAP service oriented applications.
8
+ module Model
9
+
10
+ VERSION = "0.4.0"
11
+
12
+ def self.handle_response=(recipe)
13
+ @handle_response = recipe
14
+ end
15
+
16
+ def self.handle_response
17
+ @handle_response
18
+ end
19
+
20
+ module ClassMethods
21
+
22
+ def self.extend_object(base)
23
+ super
24
+ base.init_savon_model
25
+ end
26
+
27
+ def init_savon_model
28
+ class_action_module
29
+ instance_action_module
30
+ end
31
+
32
+ # Accepts one or more SOAP actions and generates both class and instance methods named
33
+ # after the given actions. Each generated method accepts an optional SOAP body Hash and
34
+ # a block to be passed to <tt>Savon::Client#request</tt> and executes a SOAP request.
35
+ def actions(*actions)
36
+ actions.each do |action|
37
+ define_class_action action
38
+ define_instance_action action
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def define_class_action(action)
45
+ class_action_module.module_eval <<-CODE
46
+ def #{action.to_s.snakecase}(body = nil, &block)
47
+ response = client.request :wsdl, #{action.inspect}, :body => body, &block
48
+ Savon::Model.handle_response ? Savon::Model.handle_response.call(response) : response
49
+ end
50
+ CODE
51
+ end
52
+
53
+ def define_instance_action(action)
54
+ instance_action_module.module_eval <<-CODE
55
+ def #{action.to_s.snakecase}(body = nil, &block)
56
+ self.class.#{action.to_s.snakecase} body, &block
57
+ end
58
+ CODE
59
+ end
60
+
61
+ def class_action_module
62
+ @class_action_module ||= Module.new do
63
+
64
+ def client(&block)
65
+ @client ||= Savon::Client.new &block
66
+ end
67
+
68
+ def endpoint(uri)
69
+ client.wsdl.endpoint = uri
70
+ end
71
+
72
+ def document(uri)
73
+ client.wsdl.document = uri
74
+ end
75
+
76
+ def basic_auth(login, password)
77
+ client.http.auth.basic login, password
78
+ end
79
+
80
+ def namespace(uri)
81
+ client.wsdl.namespace = uri
82
+ end
83
+
84
+ end.tap { |mod| extend mod }
85
+ end
86
+
87
+ def instance_action_module
88
+ @instance_action_module ||= Module.new do
89
+
90
+ def client(&block)
91
+ self.class.client &block
92
+ end
93
+
94
+ def endpoint(uri)
95
+ self.class.endpoint uri
96
+ end
97
+
98
+ def document(uri)
99
+ self.class.document uri
100
+ end
101
+
102
+ def basic_auth(login, password)
103
+ self.class.basic_auth login, password
104
+ end
105
+
106
+ def namespace(uri)
107
+ self.class.namespace uri
108
+ end
109
+
110
+ end.tap { |mod| include mod }
111
+ end
112
+
113
+ end
114
+
115
+ def self.included(base)
116
+ base.extend ClassMethods
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1 @@
1
+ require "savon/model"
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path("../lib/", __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require "savon_model"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "s_savon_model"
8
+ s.version = Savon::Model::VERSION
9
+ s.authors = "Daniel Harrington"
10
+ s.email = "me@rubiii.com"
11
+ s.homepage = "http://github.com/rubiii/#{s.name}"
12
+ s.summary = "SOAP model"
13
+ s.description = "Model for SOAP service oriented applications."
14
+
15
+ s.rubyforge_project = s.name
16
+
17
+ s.add_dependency "httpi", ">= 0.7.8"
18
+ s.add_dependency "savon", ">= 0.8.2"
19
+
20
+ s.add_development_dependency "rspec", "~> 2.4.0"
21
+ s.add_development_dependency "autotest"
22
+ s.add_development_dependency "mocha", "~> 0.9.8"
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.require_path = "lib"
26
+ end
@@ -0,0 +1,136 @@
1
+ require "spec_helper"
2
+ require "savon/model"
3
+
4
+ describe Savon::Model do
5
+ let(:model) { Class.new { include Savon::Model } }
6
+
7
+ describe ".handle_response" do
8
+ before(:all) { model.actions :get_user, "GetAllUsers" }
9
+
10
+ it "should be used for pre-processing SOAP responses" do
11
+ Savon::Model.handle_response = lambda { |response| response }
12
+
13
+ model.client.stubs(:request).returns("response")
14
+ model.get_user.should == "response"
15
+ end
16
+ end
17
+
18
+ describe ".client" do
19
+ it "should should pass a given block to a new Savon::Client"
20
+
21
+ it "should memoize the Savon::Client" do
22
+ model.client.should equal(model.client)
23
+ end
24
+ end
25
+
26
+ describe ".endpoint" do
27
+ it "should set the SOAP endpoint" do
28
+ model.endpoint "http://example.com"
29
+ model.client.wsdl.endpoint.should == "http://example.com"
30
+ end
31
+ end
32
+
33
+ describe ".namespace" do
34
+ it "should set the target namespace" do
35
+ model.namespace "http://v1.example.com"
36
+ model.client.wsdl.namespace.should == "http://v1.example.com"
37
+ end
38
+ end
39
+
40
+ describe ".actions" do
41
+ before(:all) { model.actions :get_user, "GetAllUsers" }
42
+
43
+ it "should define class methods each action" do
44
+ model.should respond_to(:get_user, :get_all_users)
45
+ end
46
+
47
+ it "should define instance methods each action" do
48
+ model.new.should respond_to(:get_user, :get_all_users)
49
+ end
50
+
51
+ context "(class-level)" do
52
+ it "should execute SOAP requests with a given body" do
53
+ model.client.expects(:request).with(:wsdl, :get_user, :body => { :id => 1 })
54
+ model.get_user :id => 1
55
+ end
56
+
57
+ it "should accept and pass Strings for action names" do
58
+ model.client.expects(:request).with(:wsdl, "GetAllUsers", :body => { :id => 1 })
59
+ model.get_all_users :id => 1
60
+ end
61
+ end
62
+
63
+ context "(instance-level)" do
64
+ it "should delegate to the corresponding class method" do
65
+ model.expects(:get_all_users).with(:active => true)
66
+ model.new.get_all_users :active => true
67
+ end
68
+ end
69
+ end
70
+
71
+ describe "#client" do
72
+ it "should return the class-level Savon::Client" do
73
+ model.new.client.should == model.client
74
+ end
75
+ end
76
+
77
+ describe "#endpoint" do
78
+ it "should delegate to .endpoint" do
79
+ model.expects(:endpoint).with("http://example.com")
80
+ model.new.endpoint "http://example.com"
81
+ end
82
+ end
83
+
84
+ describe "#namespace" do
85
+ it "should delegate to .namespace" do
86
+ model.expects(:namespace).with("http://v1.example.com")
87
+ model.new.namespace "http://v1.example.com"
88
+ end
89
+ end
90
+
91
+ describe "overwriting action methods" do
92
+ context "(class-level)" do
93
+ let :supermodel do
94
+ supermodel = model.dup
95
+ supermodel.actions :get_user
96
+
97
+ def supermodel.get_user(body = nil, &block)
98
+ p "super"
99
+ super
100
+ end
101
+
102
+ supermodel
103
+ end
104
+
105
+ it "should be possible" do
106
+ supermodel.client.expects(:request).with(:wsdl, :get_user, :body => { :id => 1 })
107
+ supermodel.expects(:p).with("super") # stupid, but works
108
+
109
+ supermodel.get_user :id => 1
110
+ end
111
+ end
112
+
113
+ context "(instance-level)" do
114
+ let :supermodel do
115
+ supermodel = model.dup
116
+ supermodel.actions :get_user
117
+ supermodel = supermodel.new
118
+
119
+ def supermodel.get_user(body = nil, &block)
120
+ p "super"
121
+ super
122
+ end
123
+
124
+ supermodel
125
+ end
126
+
127
+ it "should be possible" do
128
+ supermodel.client.expects(:request).with(:wsdl, :get_user, :body => { :id => 1 })
129
+ supermodel.expects(:p).with("super") # stupid, but works
130
+
131
+ supermodel.get_user :id => 1
132
+ end
133
+ end
134
+ end
135
+
136
+ end
@@ -0,0 +1,6 @@
1
+ require "bundler"
2
+ Bundler.require :default, :test
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :mocha
6
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s_savon_model
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
+ platform: ruby
12
+ authors:
13
+ - Daniel Harrington
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-21 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: httpi
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 7
33
+ - 8
34
+ version: 0.7.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: savon
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 59
46
+ segments:
47
+ - 0
48
+ - 8
49
+ - 2
50
+ version: 0.8.2
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 31
62
+ segments:
63
+ - 2
64
+ - 4
65
+ - 0
66
+ version: 2.4.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: autotest
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ hash: 43
92
+ segments:
93
+ - 0
94
+ - 9
95
+ - 8
96
+ version: 0.9.8
97
+ type: :development
98
+ version_requirements: *id005
99
+ description: Model for SOAP service oriented applications.
100
+ email: me@rubiii.com
101
+ executables: []
102
+
103
+ extensions: []
104
+
105
+ extra_rdoc_files: []
106
+
107
+ files:
108
+ - .gitignore
109
+ - .rspec
110
+ - Gemfile
111
+ - LICENSE
112
+ - README.md
113
+ - Rakefile
114
+ - lib/savon/model.rb
115
+ - lib/savon_model.rb
116
+ - savon_model.gemspec
117
+ - spec/savon/model_spec.rb
118
+ - spec/spec_helper.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/rubiii/s_savon_model
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project: s_savon_model
149
+ rubygems_version: 1.5.2
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: SOAP model
153
+ test_files: []
154
+