gateway-http 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem "gateway", ">= 0.1.0"
6
+ gem "net-http-pipeline", "~> 1.0"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "rspec", "~> 2.8.0"
12
+ gem "yard", "~> 0.7"
13
+ gem "rdoc", "~> 3.12"
14
+ gem "bundler", "~> 1.0.0"
15
+ gem "jeweler", "~> 1.8.3"
16
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Aaron Qian
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.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = gateway-http
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to gateway-http
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Aaron Qian. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "gateway-http"
18
+ gem.homepage = "http://github.com/aq1018/gateway-http"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{ Gateway wrapper client for Net::HTTP }
21
+ gem.description = %Q{ Gateway wrapper client for Net::HTTP }
22
+ gem.email = "aq1018@gmail.com"
23
+ gem.authors = ["Aaron Qian"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+
35
+ task :default => :spec
36
+
37
+ require 'yard'
38
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ require 'gateway'
2
+ require 'net/http/pipeline'
3
+ require 'gateway/http'
@@ -0,0 +1,158 @@
1
+ module Gateway
2
+ class HTTP < Gateway::Base
3
+ # purge connection that is stuck in bad state
4
+ # PipelineError should never happen in ATTI
5
+ categorize_error(Net::HTTP::Pipeline::PipelineError, {
6
+ :as => :retry,
7
+ :for => :pipeline
8
+ }) do | gateway |
9
+ gateway.purge_current_connection!
10
+ end
11
+
12
+ categorize_error Net::HTTP::Pipeline::Error,
13
+ :as => :bad_gateway, :for => :pipeline
14
+
15
+ categorize_error Net::HTTPError,
16
+ :as => :bad_gateway, :for => :all
17
+
18
+ # It's safe to specify all actions because non-idempotent requests
19
+ # will skip retry automatically
20
+ categorize_error Timeout::Error, Net::HTTPError,
21
+ :as => :retry, :for => :all
22
+
23
+ def self.normalize_uri uri
24
+ return uri if uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
25
+ uri = uri.to_s
26
+ uri = "http://#{uri}" unless /^(http|https):\/\// =~ uri
27
+ URI.parse(uri)
28
+ end
29
+
30
+ attr_reader :address, :use_ssl, :host, :port
31
+ attr_accessor :header
32
+
33
+ def initialize(name, opts)
34
+ super
35
+ @address = self.class.normalize_uri(opts[:uri])
36
+ @use_ssl = @address.scheme == "https"
37
+ @host = @address.host
38
+ @port = @address.port
39
+ @header = opts[:header] || {}
40
+ end
41
+
42
+ def pipeline(requests, opts={}, &block)
43
+ msg = requests.map{|r| absolute_url(r).to_s }.join(',')
44
+
45
+ execute('pipeline', msg, opts) do |conn|
46
+ conn.start unless conn.started?
47
+ conn.pipeline(requests.dup, &block)
48
+ end
49
+ end
50
+
51
+ def request(req, opts={})
52
+ opts = {
53
+ :persistent => false,
54
+ :retry => false
55
+ }.merge(opts) unless idempotent?(req)
56
+
57
+ action = req.method.downcase.to_sym
58
+
59
+ execute(action, absolute_url(req), opts) do |conn|
60
+ conn.start unless conn.started?
61
+ rsp = conn.request(req)
62
+ validate_response(req, rsp, valid_responses(opts)) if validate_response?(opts)
63
+ rsp
64
+ end
65
+ end
66
+
67
+ def absolute_url(req)
68
+ address + req.path
69
+ end
70
+
71
+ def get(path, header=nil, opts={})
72
+ req = prepare_request(:get, path, nil, header)
73
+ request(req, opts)
74
+ end
75
+
76
+ def post(path, body=nil, header=nil, opts={})
77
+ req = prepare_request(:post, path, body, header)
78
+ request(req, opts)
79
+ end
80
+
81
+ def put(path, body=nil, header=nil, opts={})
82
+ req= prepare_request(:put, path, body, header)
83
+ request(req, opts)
84
+ end
85
+
86
+ def delete(path, header=nil, opts={})
87
+ prepare_request(:delete, path, nil, header)
88
+ request(req, opts)
89
+ end
90
+
91
+ def idempotent?(req)
92
+ case req
93
+ when Net::HTTP::Delete, Net::HTTP::Get, Net::HTTP::Head,
94
+ Net::HTTP::Options, Net::HTTP::Put, Net::HTTP::Trace then
95
+ true
96
+ end
97
+ end
98
+
99
+ def validate_response?(opts)
100
+ opts.fetch(:validate_response, true)
101
+ end
102
+
103
+ def valid_responses(opts)
104
+ opts.fetch(:valid_responses, [ Net::HTTPSuccess ])
105
+ end
106
+
107
+ def validate_response(req, rsp, valid_rsp)
108
+ is_valid = valid_rsp.any?{|klass| rsp.is_a?(klass) }
109
+
110
+ raise Gateway::BadResponse.new(
111
+ "Invalid Response",
112
+ :status => rsp.code,
113
+ :url => absolute_url(req)
114
+ ) unless is_valid
115
+ end
116
+
117
+ def prepare_request(method, path, body, header)
118
+ klass = "Net::HTTP::#{method.to_s.classify}".constantize
119
+
120
+ header = self.header.merge(header || {})
121
+ req = klass.new path, header
122
+
123
+ if allow_body?(req)
124
+ if body.is_a?(Hash)
125
+ req.set_form_data body
126
+ elsif body.respond_to?(:rewind) && body.respond_to?(:read)
127
+ body.rewind
128
+ req.body = body.read
129
+ else
130
+ req.body = body.to_s
131
+ end
132
+ end
133
+ req
134
+ end
135
+
136
+ def allow_body?(req)
137
+ req.is_a?(Net::HTTP::Post) || req.is_a?(Net::HTTP::Put)
138
+ end
139
+
140
+ protected
141
+
142
+ def connect
143
+ conn = Net::HTTP.new(host, port)
144
+ conn.use_ssl = use_ssl
145
+ conn
146
+ end
147
+
148
+ def disconnect(conn)
149
+ conn.finish
150
+ rescue IOError
151
+ end
152
+
153
+ def reconnect(conn)
154
+ disconnect(conn)
155
+ conn
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "GatewayHttp" do
4
+ it "should be worked on"
5
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'gateway-http'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gateway-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Qian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gateway
16
+ requirement: &18457440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *18457440
25
+ - !ruby/object:Gem::Dependency
26
+ name: net-http-pipeline
27
+ requirement: &18472140 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *18472140
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &18471440 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.8.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *18471440
47
+ - !ruby/object:Gem::Dependency
48
+ name: yard
49
+ requirement: &18470680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *18470680
58
+ - !ruby/object:Gem::Dependency
59
+ name: rdoc
60
+ requirement: &18469940 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.12'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *18469940
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: &18469160 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.0.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *18469160
80
+ - !ruby/object:Gem::Dependency
81
+ name: jeweler
82
+ requirement: &18468380 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: 1.8.3
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *18468380
91
+ description: ! ' Gateway wrapper client for Net::HTTP '
92
+ email: aq1018@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ files:
99
+ - .document
100
+ - .rspec
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.rdoc
104
+ - Rakefile
105
+ - VERSION
106
+ - lib/gateway-http.rb
107
+ - lib/gateway/http.rb
108
+ - spec/gateway-http_spec.rb
109
+ - spec/spec_helper.rb
110
+ homepage: http://github.com/aq1018/gateway-http
111
+ licenses:
112
+ - MIT
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ segments:
124
+ - 0
125
+ hash: -993550479734440858
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.10
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Gateway wrapper client for Net::HTTP
138
+ test_files: []