Permalink
Browse files

Merge pull request #6 from site5/feature/malcolm

Switched out local middleware for Malcolm
  • Loading branch information...
2 parents 8e4469e + 5a06a38 commit 574f69c662b0f0970fd1ca6365ce8a109226bb7b @jmazzi jmazzi committed Mar 19, 2012
View
@@ -18,8 +18,9 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'builder', '~> 3.0.0'
s.add_runtime_dependency 'faraday', '~> 0.8.0rc'
- s.add_runtime_dependency 'faraday_middleware', '~> 0.8.4'
- s.add_runtime_dependency 'multi_xml'
+ s.add_runtime_dependency 'malcolm', '~> 0.0.2'
+ s.add_runtime_dependency 'nori', '~> 1.1.0'
+ s.add_runtime_dependency 'xml-simple', '~> 1.1.1'
s.add_runtime_dependency('jruby-openssl', '~> 0.7.4') if RUBY_PLATFORM == 'java'
s.add_development_dependency 'rake', '~> 0.9.2.2'
View
@@ -1,8 +1,3 @@
-require 'faraday'
-require 'faraday_middleware'
-require 'fanny_pack/middleware/fantastico'
-require 'multi_xml'
-
module FannyPack
autoload :Version, 'fanny_pack/version'
autoload :Errors, 'fanny_pack/errors'
View
@@ -85,7 +85,13 @@ def self.list(list_type, details = false)
unless IP_TYPES.keys.include? list_type
raise ArgumentError, "Invalid list type"
end
- Request.new.commit cmd, :listType => IP_TYPES[list_type]
+ response = Request.new.commit cmd, :listType => IP_TYPES[list_type]
+ if details
+ items = response[:item].map{|i| i[:item] }
+ items.map { |item| attr_array_to_hash(item) }
+ else
+ response[:item]
+ end
end
# Delete an IP address from your Fantastico Account
@@ -98,7 +104,8 @@ def self.list(list_type, details = false)
# @example
# FannyPack::IP.delete '127.0.0.1'
def self.delete(ip)
- Request.new.commit :deleteIp, :ip => ip
+ response = Request.new.commit :deleteIp, :ip => ip
+ attr_array_to_hash(response[:item])
end
# Reactivate a deactivated IP address
@@ -111,7 +118,8 @@ def self.delete(ip)
# @example
# FannyPack::IP.reactivate '127.0.0.1'
def self.reactivate(ip)
- Request.new.commit :reactivateIp, :ip => ip
+ response = Request.new.commit :reactivateIp, :ip => ip
+ attr_array_to_hash(response[:item])
end
# Deactivate an IP address
@@ -124,7 +132,8 @@ def self.reactivate(ip)
# @example
# FannyPack::IP.deactivate '127.0.0.1'
def self.deactivate(ip)
- Request.new.commit :deactivateIp, :ip => ip
+ response = Request.new.commit :deactivateIp, :ip => ip
+ attr_array_to_hash(response[:item])
end
# Get details for an IP address
@@ -137,7 +146,18 @@ def self.deactivate(ip)
# @example
# FannyPack::IP.details '127.0.0.1'
def self.details(ip)
- Request.new.commit :getIpDetails, :ip => ip
+ response = Request.new.commit :getIpDetails, :ip => ip
+ attr_array_to_hash(response[:item])
+ end
+
+ private
+
+ def self.attr_array_to_hash(array)
+ array.inject({}) do |result, pair|
+ result[pair[:key]] = pair[:value]
+ result
+ end
end
+
end
end
@@ -1,97 +0,0 @@
-module FannyPack
- class FantasticoXMLBuilder < Faraday::Middleware
- dependency 'builder'
-
- def initialize(app, *args)
- @action = args[0]
- @params = args[1]
- @hash = args[2]
- super(app)
- end
-
- def call(env)
- env[:body] = to_xml
- @app.call(env)
- end
-
- private
-
- # Builds the SOAP Envelope to be sent to Fantastico for this request
- #
- # @return [String]
- def to_xml
- xml = Builder::XmlMarkup.new
- xml.instruct!
- xml.tag! 'env:Envelope', 'xmlns:env' => 'http://schemas.xmlsoap.org/soap/envelope/' do
- xml.tag! 'env:Body' do
- xml.tag! @action do
- xml.tag! 'accountHASH', @hash
- @params.each do |key, val|
- xml.tag! key, val
- end
- end
- end
- end
- xml.target!
- end
-
- end
-
- class FantasticoParser < Faraday::Response::Middleware
-
- def initialize(env, *args)
- @action = args[0]
- super(env)
- end
-
- def on_complete(env)
- env[:body] = parse(env[:body])
- end
-
- private
-
- # Parse the XML response from Fantastico
- #
- # Returns an Array or Hash depending on the API method called
- #
- # @param [String] data
- # The XML response from Fantastico
- #
- # @return [Hash, Array]
- def parse(data)
- res = find_key_in_hash(data, 'item')
- if @action.to_sym == :getIpListDetailed
- res.map! do |r|
- Hash[r['item'].map { |i| [i['key'], i['value']] }]
- end
- elsif @action.to_sym == :getIpList
- res
- else
- Hash[res.map { |r| [r['key'], r['value']] }] if res.is_a? Array
- end
- end
-
- # Finds +index+ in +hash+ by searching recursively
- #
- # @param [Hash] hash
- # The hash to search
- #
- # @param index
- # The hash key to look for
- def find_key_in_hash(hash, index)
- hash.each do |key, val|
- if val.respond_to? :has_key?
- if val.has_key? index
- return val[index]
- else
- return find_key_in_hash val, index
- end
- else
- val
- end
- end
- end
-
- end
-
-end
View
@@ -1,5 +1,5 @@
-Faraday.register_middleware :request, :fantastico_xml_builder => FannyPack::FantasticoXMLBuilder
-Faraday.register_middleware :response, :fantastico_parser => FannyPack::FantasticoParser
+require 'faraday'
+require 'malcolm'
module FannyPack
# FannyPack::Request handles forming the XML request to be sent to the
@@ -48,13 +48,18 @@ def commit(action, params = {})
@action = action
@params = params
- response = Faraday.new(:url => API_URL) do |c|
- c.headers = {'Content-Type' => 'text/xml; charset=utf-8'}
- c.request :fantastico_xml_builder, @action, @params, FannyPack.account_hash
- c.response :fantastico_parser, @action
- c.response :xml
+ conn = Faraday.new(:url => API_URL) do |c|
+ c.request :soap
+ c.response :soap, "item"
c.adapter :net_http
- end.post.body
+ end
+
+ request = conn.post do |r|
+ r.body = {@action => {'accountHASH' => FannyPack.account_hash}.merge(@params)}
+ r.headers = {'Content-Type' => 'text/xml; charset=utf-8'}
+ end
+
+ response = request.body
@success = !(response.is_a?(Hash) && response.has_key?("faultcode"))
@@ -1,48 +0,0 @@
-require 'spec_helper'
-
-describe FannyPack::FantasticoXMLBuilder do
-
- it "builds a soap envelope for request" do
- middleware = described_class.new(lambda{|env| env}, "test", {:param => "x"}, "34o8hti")
- env = {:body => nil, :request_headers => Faraday::Utils::Headers.new}
- result = middleware.call(env)
- xml = result[:body]
- xml.should include %{<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body>}
- end
-
-end
-
-describe FannyPack::FantasticoParser do
-
- it "returns an Array of Hashes if @action == :getIpListDetailed" do
- response_xml = YAML.load_file("spec/vcr_cassettes/ip/list.yml")["http_interactions"].last["response"]["body"]["string"]
- env = { :body => MultiXml.parse(response_xml) }
- middleware = described_class.new(lambda{|env| env}, :getIpListDetailed)
- res = middleware.on_complete(env)
- res.should be_a Array
- res.should have(2).items
- %w[ipAddress addedOn isVPS status].each do |key|
- res.first.should have_key key
- end
- end
-
- it "returns a flat Array if @action is :getIpList" do
- response_xml = YAML.load_file("spec/vcr_cassettes/ip/list.yml")["http_interactions"].first["response"]["body"]["string"]
- env = { :body => MultiXml.parse(response_xml) }
- middleware = described_class.new(lambda{|env| env}, :getIpList)
- res = middleware.on_complete(env)
- res.should be_a Array
- res.should have(2).items
- res[0].should == '127.0.0.1'
- res[1].should == '127.0.0.2'
- end
-
- it "returns a Hash if @action is not :getIpList or :getIpListDetailed" do
- response_xml = YAML.load_file("spec/vcr_cassettes/ip/add.yml")["http_interactions"].first["response"]["body"]["string"]
- env = { :body => MultiXml.parse(response_xml) }
- middleware = described_class.new(lambda{|env| env}, :addIp)
- res = middleware.on_complete(env)
- res.should be_a Hash
- end
-
-end
View
@@ -3,6 +3,8 @@
require 'vcr_patch'
require 'fanny_pack'
+FannyPack.account_hash = "test"
+
def requires_ip(&block)
expect { block.call }.to raise_error(ArgumentError)
end
@@ -18,4 +20,4 @@ def requires_ip(&block)
c.register_request_matcher :xml_body_without_order do |request1, request2|
MultiXml.parse(request1.body) == MultiXml.parse(request2.body)
end
-end
+end

0 comments on commit 574f69c

Please sign in to comment.