malcolm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,22 @@
1
+ # Malcolm [![Malcolm Build Status][Build Icon]][Build Status]
2
+
3
+ Malcolm is a collection of middleware for [Faraday](https://github.com/technoweenie/faraday),
4
+ the excellent HTTP client interface based on [Rack](http://rack.github.com/).
5
+
6
+ [Build Icon]: https://secure.travis-ci.org/site5/malcolm.png?branch=master
7
+ [Build Status]: https://travis-ci.org/site5/malcolm
8
+
9
+ ## Note on Patches/Pull Requests
10
+
11
+ * Fork the project.
12
+ * Make your feature addition or bug fix.
13
+ * Add tests for it. This is important so I don't break it in a
14
+ future version unintentionally.
15
+ * Commit, do not mess with rakefile, version, or history.
16
+ (if you want to have your own version, that is fine but bump version in a
17
+ commit by itself I can ignore when I pull)
18
+ * Send me a pull request. Bonus points for topic branches.
19
+
20
+ ## Copyright
21
+
22
+ Copyright (c) 2012 Site5.com. See LICENSE for details.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # Malcolm [![Malcolm Build Status][Build Icon]][Build Status]
2
+
3
+ Malcolm is a collection of middleware for [Faraday](https://github.com/technoweenie/faraday),
4
+ the excellent HTTP client interface based on [Rack](http://rack.github.com/).
5
+
6
+ [Build Icon]: https://secure.travis-ci.org/site5/malcolm.png?branch=master
7
+ [Build Status]: https://travis-ci.org/site5/malcolm
8
+
9
+ ## Note on Patches/Pull Requests
10
+
11
+ * Fork the project.
12
+ * Make your feature addition or bug fix.
13
+ * Add tests for it. This is important so I don't break it in a
14
+ future version unintentionally.
15
+ * Commit, do not mess with rakefile, version, or history.
16
+ (if you want to have your own version, that is fine but bump version in a
17
+ commit by itself I can ignore when I pull)
18
+ * Send me a pull request. Bonus points for topic branches.
19
+
20
+ ## Copyright
21
+
22
+ Copyright (c) 2012 Site5.com. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'bundler'
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ rescue LoadError
5
+ puts "Please install rspec (bundle install)"
6
+ exit
7
+ end
8
+
9
+ RSpec::Core::RakeTask.new :spec
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ desc "Run all specs with rcov"
13
+ RSpec::Core::RakeTask.new(:rcov) do |t|
14
+ t.rcov = true
15
+ t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/,features\/}
16
+ end
17
+
18
+ task :default => [:spec]
@@ -0,0 +1,24 @@
1
+ module Malcolm
2
+ # Request middleware that converts params to XML and wraps them in a SOAP envelope.
3
+ #See http://www.w3.org/TR/2007/REC-soap12-part0-20070427/
4
+ class SOAPBuilder < Faraday::Middleware
5
+ dependency 'xmlsimple'
6
+
7
+ # Assumes request body is a hash of key-value pairs
8
+ def call(env)
9
+ env[:body] = wrap(env[:body])
10
+ @app.call env
11
+ end
12
+
13
+ private
14
+
15
+ # Builds an XML document around request data
16
+ def wrap(data)
17
+ raise "Request body already serialized. Please make sure it is a hash before applying SOAP Builder." unless data.is_a?(Hash)
18
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"><env:Body>".tap do |soap_envelope|
19
+ soap_envelope << XmlSimple.xml_out(data, :keep_root => true) if data
20
+ soap_envelope << "</env:Body></env:Envelope>"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,47 @@
1
+ module Malcolm
2
+ # Response middleware that unwraps a SOAP envelope for you
3
+ class SOAPParser < Faraday::Middleware
4
+ dependency 'nori'
5
+
6
+ Nori.configure do |config|
7
+ config.strip_namespaces = true
8
+ config.convert_tags_to { |tag| tag.snakecase.to_sym }
9
+ end
10
+
11
+ def initialize(env, *args)
12
+ key = args[0]
13
+ super(env)
14
+ end
15
+
16
+ # Expects response XML to already be parsed
17
+ def on_complete(env)
18
+ env[:body] = Nori.parse(env[:body])
19
+ env[:body] = env[:body][:envelope][:body]
20
+ env[:body] = find_key_in_hash(env[:body], @key)
21
+ end
22
+
23
+ private
24
+
25
+ # Finds +index+ in +hash+ by searching recursively
26
+ #
27
+ # @param [Hash] hash
28
+ # The hash to search
29
+ #
30
+ # @param index
31
+ # The hash key to look for
32
+ def find_key_in_hash(hash, index)
33
+ hash.each do |key, val|
34
+ if val.respond_to? :has_key?
35
+ if val.has_key? index
36
+ return val[index]
37
+ else
38
+ return find_key_in_hash val, index
39
+ end
40
+ else
41
+ val
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
data/lib/malcolm.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'faraday'
2
+
3
+ module Malcolm
4
+ autoload :SOAPBuilder, 'malcolm/request/soap_builder'
5
+ autoload :SOAPParser, 'malcolm/response/soap_parser'
6
+
7
+ Faraday.register_middleware :request,
8
+ :soap => SOAPBuilder
9
+
10
+ Faraday.register_middleware :response,
11
+ :soap => SOAPParser
12
+
13
+ end
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethods-delayed-quotes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
3
+ <SOAP-ENV:Body><ns1:addIpResponse>
4
+ <Result xsi:type="ns2:Map">
5
+ <item><key xsi:type="xsd:string">ip</key><value xsi:type="xsd:string">127.0.0.1</value></item>
6
+ <item><key xsi:type="xsd:string">id</key><value xsi:type="xsd:string">123456</value></item>
7
+ </Result>
8
+ </ns1:addIpResponse>
9
+ </SOAP-ENV:Body>
10
+ </SOAP-ENV:Envelope>
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Malcolm::SOAPBuilder do
4
+
5
+ it "builds a soap envelope for request" do
6
+ middleware = described_class.new(lambda{|env| env})
7
+ env = {:body => {"test" => "data"}, :request_headers => Faraday::Utils::Headers.new}
8
+ result = middleware.call(env)
9
+ xml = result[:body]
10
+ xml.should include %{<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body>}
11
+ end
12
+
13
+ it "converts body to xml" do
14
+ middleware = described_class.new(lambda{|env| env})
15
+ env = {:body => {"test" => "data"}, :request_headers => Faraday::Utils::Headers.new}
16
+ result = middleware.call(env)
17
+ xml = result[:body]
18
+ xml.should include %{<test>data}
19
+ end
20
+
21
+ it "raises if body isn't a hash" do
22
+ middleware = described_class.new(lambda{|env| env})
23
+ env = {:body => "test=data", :request_headers => Faraday::Utils::Headers.new}
24
+ expect { middleware.call(env) }.to raise_error
25
+ end
26
+
27
+
28
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Malcolm::SOAPParser do
4
+
5
+ it "parses xml" do
6
+ env = { :body => load_fixture("soap.xml") }
7
+ middleware = described_class.new(lambda{|env| env})
8
+ res = middleware.on_complete(env)
9
+ res.should be_a Hash
10
+ end
11
+
12
+ it "drills down to body" do
13
+ env = { :body => load_fixture("soap.xml") }
14
+ middleware = described_class.new(lambda{|env| env})
15
+ res = middleware.on_complete(env)
16
+ res.keys.should_not include(:envelope)
17
+ end
18
+
19
+ it "fetches given key" do
20
+ env = { :body => load_fixture("soap.xml") }
21
+ middleware = described_class.new(lambda{|env| env}, :item)
22
+ res = middleware.on_complete(env)
23
+ res.keys.should_not include(:envelope)
24
+ end
25
+
26
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'malcolm'
3
+
4
+ def load_fixture(name)
5
+ File.read("spec/fixtures/#{name}")
6
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: malcolm
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Floyd Wright
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-16 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: faraday
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: -245658415
30
+ segments:
31
+ - 0
32
+ - 8
33
+ - 0rc2
34
+ version: 0.8.0rc2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 11
46
+ segments:
47
+ - 0
48
+ - 9
49
+ - 2
50
+ - 2
51
+ version: 0.9.2.2
52
+ type: :development
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ hash: 47
63
+ segments:
64
+ - 2
65
+ - 8
66
+ - 0
67
+ version: 2.8.0
68
+ type: :development
69
+ version_requirements: *id003
70
+ description: " A collection of Faraday middleware\n"
71
+ email: fwright@site5.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - README.markdown
78
+ files:
79
+ - Rakefile
80
+ - README.md
81
+ - lib/malcolm/request/soap_builder.rb
82
+ - lib/malcolm/response/soap_parser.rb
83
+ - lib/malcolm.rb
84
+ - spec/fixtures/soap.xml
85
+ - spec/malcolm/request/soap_builder_spec.rb
86
+ - spec/malcolm/response/soap_parser_spec.rb
87
+ - spec/spec_helper.rb
88
+ - README.markdown
89
+ has_rdoc: true
90
+ homepage: https://github.com/site5/malcolm
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --charset=UTF-8
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: A collection of Faraday middleware
123
+ test_files: []
124
+