vend 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,6 +19,8 @@ module Vend
19
19
  # from the Vend API
20
20
  class Unauthorized < StandardError; end
21
21
 
22
+ class RedirectionLimitExceeded < StandardError; end
23
+
22
24
  # Nonspecific HTTP error which is usually thrown when a non 2xx response
23
25
  # is received.
24
26
  class HTTPError < StandardError
@@ -4,7 +4,7 @@ require 'cgi'
4
4
  module Vend
5
5
  class HttpClient
6
6
 
7
- UNAUTHORIZED_MESSAGE = "Client not authorized. Check your store URL and credentials are correct and try again."
7
+ UNAUTHORIZED_MESSAGE = "Client not authorized. Check your store credentials are correct and try again."
8
8
  # Read timeout in seconds
9
9
  READ_TIMEOUT = 240
10
10
 
@@ -25,9 +25,9 @@ module Vend
25
25
  end
26
26
 
27
27
  # sets up a http connection
28
- def get_http_connection(host, port)
28
+ def get_http_connection(host, port, use_ssl = true)
29
29
  http = Net::HTTP.new(host, port)
30
- http.use_ssl = true
30
+ http.use_ssl = use_ssl
31
31
  http.verify_mode = verify_mode
32
32
  # Default read_tiemout is 60 seconds, some of the Vend API calls are
33
33
  # taking longer than this.
@@ -61,9 +61,15 @@ module Vend
61
61
  # request('foo', :method => :post, :body => '{\"baz\":\"baloo\"}'
62
62
  #
63
63
  def request(path, options = {})
64
- options = {:method => :get}.merge options
65
- url = URI.parse(base_url + path)
66
- http = get_http_connection(url.host, url.port)
64
+ options = {:method => :get, :redirect_count => 0}.merge options
65
+ raise RedirectionLimitExceeded if options[:redirect_count] > 10
66
+ url = if path.kind_of?(URI)
67
+ path
68
+ else
69
+ URI.parse(base_url + path)
70
+ end
71
+ ssl = (url.scheme == 'https')
72
+ http = get_http_connection(url.host, url.port, ssl)
67
73
 
68
74
  # FIXME extract method
69
75
  method = ("Net::HTTP::" + options[:method].to_s.classify).constantize
@@ -73,10 +79,17 @@ module Vend
73
79
  request.body = options[:body] if options[:body]
74
80
  logger.debug url
75
81
  response = http.request(request)
76
- raise Unauthorized.new(UNAUTHORIZED_MESSAGE) if response.kind_of?(Net::HTTPUnauthorized)
77
- raise HTTPError.new(response) unless response.kind_of?(Net::HTTPSuccess)
78
- logger.debug response
79
- JSON.parse response.body unless response.body.nil? or response.body.empty?
82
+ if response.kind_of?(Net::HTTPRedirection)
83
+ location = URI.parse(response['location'])
84
+ logger.debug "Following redirect to %s" % [location]
85
+ options[:redirect_count] = options[:redirect_count] + 1
86
+ request(location, options)
87
+ else
88
+ raise Unauthorized.new(UNAUTHORIZED_MESSAGE) if response.kind_of?(Net::HTTPUnauthorized)
89
+ raise HTTPError.new(response) unless response.kind_of?(Net::HTTPSuccess)
90
+ logger.debug response
91
+ JSON.parse response.body unless response.body.nil? or response.body.empty?
92
+ end
80
93
  end
81
94
 
82
95
  # Returns the SSL verification mode, based upon the value of verify_ssl?
data/lib/vend/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vend #:nodoc:
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -126,5 +126,23 @@ describe Vend::HttpClient do
126
126
  response.should == {"foo" => "bar"}
127
127
  end
128
128
 
129
+ it "follows redirects" do
130
+ stub_request(:get, "https://username:password@foo/bar/foo").
131
+ to_return(:status => 302, :body => '{"bar":"baz"}', :headers => {"Location" => "http://username:password@foo/bar/floo"})
132
+
133
+ stub_request(:get, "http://username:password@foo/bar/floo").
134
+ to_return(:status => 200, :body => '{"foo":"bar"}', :headers => {})
135
+
136
+ response = subject.request('foo')
137
+ response.should == {"foo" => "bar"}
138
+ end
139
+
140
+ it "raises an exception when the redirection limit is exceeded" do
141
+ stub_request(:get, "https://username:password@foo/bar/foo").
142
+ to_return(:status => 302, :body => '{"bar":"baz"}', :headers => {"Location" => "https://username:password@foo/bar/foo"})
143
+ expect {
144
+ subject.request('foo')
145
+ }.to raise_exception(Vend::RedirectionLimitExceeded)
146
+ end
129
147
  end
130
148
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-25 00:00:00.000000000 Z
12
+ date: 2012-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -160,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
160
  version: '0'
161
161
  segments:
162
162
  - 0
163
- hash: 502015210106226139
163
+ hash: 4352042861581051323
164
164
  required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  none: false
166
166
  requirements:
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  segments:
171
171
  - 0
172
- hash: 502015210106226139
172
+ hash: 4352042861581051323
173
173
  requirements: []
174
174
  rubyforge_project: vend
175
175
  rubygems_version: 1.8.24