Permalink
Browse files

Included proxy config

  • Loading branch information...
dkonishi committed Aug 21, 2012
1 parent 9247d6e commit 80aa9063477101a1f600d4268ae40abe5804ac4e
Showing with 43 additions and 9 deletions.
  1. +8 −0 lib/rbraspag.rb
  2. +17 −7 lib/rbraspag/poster.rb
  3. +18 −2 spec/poster_spec.rb
View
@@ -34,4 +34,12 @@ def self.config_file_path=(path)
def self.config_file_path
@config_path || 'config/braspag.yml'
end
+
+ def self.proxy_address=(value)
+ @proxy_address = value
+ end
+
+ def self.proxy_address
+ @proxy_address
+ end
end
View
@@ -1,21 +1,31 @@
module Braspag
class Poster
- def initialize(request)
- @request = request
+ def initialize(url)
+ @request = ::HTTPI::Request.new(url)
end
def do_post(method, data)
- Braspag::logger.info("[Braspag] ##{method}: #{@request.url}, data: #{mask_data(data).inspect}") if Braspag::logger
+ @request.body = data
+ @request.proxy = Braspag.proxy_address if Braspag.proxy_address
- response = ::HTTPI.post @request
+ with_logger(method) do
+ ::HTTPI.post @request
+ end
+ end
- Braspag::logger.info("[Braspag] ##{method} returns: #{response.body.inspect}") if Braspag::logger
+ private
+ def with_logger(method)
+ if Braspag::logger
+ Braspag::logger.info("[Braspag] ##{method}: #{@request.url}, data: #{mask_data(@request.body).inspect}")
+ response = yield
+ Braspag::logger.info("[Braspag] ##{method} returns: #{response.body.inspect}")
+ else
+ response = yield
+ end
response
end
- private
-
def mask_data(data)
copy_data = data.dup
copy_data['cardNumber'] = "************%s" % copy_data['cardNumber'][-4..-1] if copy_data['cardNumber']
View
@@ -1,14 +1,16 @@
require 'spec_helper'
+require 'ostruct'
describe Braspag::Poster do
- let(:request) { mock(:url => 'http://foo/bar') }
+ let(:request) { OpenStruct.new(:url => 'http://foo/bar') }
let(:response) { mock(:body => 'success') }
let(:logger) { mock(:info => nil) }
- subject { described_class.new(request) }
+ subject { described_class.new('http://foo/bar') }
before { Braspag.logger = logger }
describe "#do_post" do
before do
+ ::HTTPI::Request.should_receive(:new).with('http://foo/bar').and_return(request)
::HTTPI.should_receive(:post).with(request).and_return(response)
end
@@ -33,5 +35,19 @@
subject.do_post(:doe, { :foo => :bar, :egg => :span })
}.to_not raise_error
end
+
+ it "should not set the proxy if the proxy_address is not set" do
+ request.should_not_receive(:proxy=)
+ subject.do_post(:foo, {})
+ end
+
+ context "using a proxy" do
+ before { Braspag.proxy_address = 'http://proxy.com' }
+
+ it "should set the proxy if the proxy_address is set" do
+ request.should_receive(:proxy=).with('http://proxy.com')
+ subject.do_post(:foo, {})
+ end
+ end
end
end

0 comments on commit 80aa906

Please sign in to comment.