Class: Gateway::HTTP
- Inherits:
-
Base
- Object
- Base
- Gateway::HTTP
- Defined in:
- lib/gateway/http.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#header ⇒ Object
Returns the value of attribute header.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#use_ssl ⇒ Object
readonly
Returns the value of attribute use_ssl.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_url(req) ⇒ Object
- #allow_body?(req) ⇒ Boolean
- #delete(path, header = nil, opts = {}) ⇒ Object
- #get(path, header = nil, opts = {}) ⇒ Object
- #head(path, header = nil, opts = {}) ⇒ Object
- #idempotent?(req) ⇒ Boolean
-
#initialize(name, opts) ⇒ HTTP
constructor
A new instance of HTTP.
- #open_timeout ⇒ Object
- #pipeline(requests, opts = {}, &block) ⇒ Object
- #post(path, body = nil, header = nil, opts = {}) ⇒ Object
- #prepare_request(method, path, body, header) ⇒ Object
- #put(path, body = nil, header = nil, opts = {}) ⇒ Object
- #read_timeout ⇒ Object
- #request(req, opts = {}) ⇒ Object
- #valid_responses(opts) ⇒ Object
- #validate_response(req, rsp, valid_rsp) ⇒ Object
- #validate_response?(opts) ⇒ Boolean
Constructor Details
#initialize(name, opts) ⇒ HTTP
Returns a new instance of HTTP.
32 33 34 35 36 37 38 39 |
# File 'lib/gateway/http.rb', line 32 def initialize(name, opts) super @address = self.class.normalize_uri(opts[:uri]) @use_ssl = @address.scheme == "https" @host = @address.host @port = @address.port @header = opts[:header] || {} end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
29 30 31 |
# File 'lib/gateway/http.rb', line 29 def address @address end |
#header ⇒ Object
Returns the value of attribute header.
30 31 32 |
# File 'lib/gateway/http.rb', line 30 def header @header end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
29 30 31 |
# File 'lib/gateway/http.rb', line 29 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
29 30 31 |
# File 'lib/gateway/http.rb', line 29 def port @port end |
#use_ssl ⇒ Object (readonly)
Returns the value of attribute use_ssl.
29 30 31 |
# File 'lib/gateway/http.rb', line 29 def use_ssl @use_ssl end |
Class Method Details
.normalize_uri(uri) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/gateway/http.rb', line 22 def self.normalize_uri uri return uri if uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS) uri = uri.to_s uri = "http://#{uri}" unless /^(http|https):\/\// =~ uri URI.parse(uri) end |
Instance Method Details
#absolute_url(req) ⇒ Object
66 67 68 |
# File 'lib/gateway/http.rb', line 66 def absolute_url(req) address + req.path end |
#allow_body?(req) ⇒ Boolean
140 141 142 |
# File 'lib/gateway/http.rb', line 140 def allow_body?(req) req.is_a?(Net::HTTP::Post) || req.is_a?(Net::HTTP::Put) end |
#delete(path, header = nil, opts = {}) ⇒ Object
90 91 92 93 |
# File 'lib/gateway/http.rb', line 90 def delete(path, header=nil, opts={}) prepare_request(:delete, path, nil, header) request(req, opts) end |
#get(path, header = nil, opts = {}) ⇒ Object
75 76 77 78 |
# File 'lib/gateway/http.rb', line 75 def get(path, header=nil, opts={}) req = prepare_request(:get, path, nil, header) request(req, opts) end |
#head(path, header = nil, opts = {}) ⇒ Object
70 71 72 73 |
# File 'lib/gateway/http.rb', line 70 def head(path, header=nil, opts={}) req = prepare_request(:head, path, nil, header) request(req, opts) end |
#idempotent?(req) ⇒ Boolean
95 96 97 98 99 100 101 |
# File 'lib/gateway/http.rb', line 95 def idempotent?(req) case req when Net::HTTP::Delete, Net::HTTP::Get, Net::HTTP::Head, Net::HTTP::Options, Net::HTTP::Put, Net::HTTP::Trace then true end end |
#open_timeout ⇒ Object
148 149 150 |
# File 'lib/gateway/http.rb', line 148 def open_timeout [:open_timeout] end |
#pipeline(requests, opts = {}, &block) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/gateway/http.rb', line 41 def pipeline(requests, opts={}, &block) msg = requests.map{|r| absolute_url(r).to_s }.join(',') execute('pipeline', msg, opts) do |conn| conn.start unless conn.started? conn.pipeline(requests.dup, &block) end end |
#post(path, body = nil, header = nil, opts = {}) ⇒ Object
80 81 82 83 |
# File 'lib/gateway/http.rb', line 80 def post(path, body=nil, header=nil, opts={}) req = prepare_request(:post, path, body, header) request(req, opts) end |
#prepare_request(method, path, body, header) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gateway/http.rb', line 121 def prepare_request(method, path, body, header) klass = "Net::HTTP::#{method.to_s.classify}".constantize header = self.header.merge(header || {}) req = klass.new path, header if allow_body?(req) if body.is_a?(Hash) req.set_form_data body elsif body.respond_to?(:rewind) && body.respond_to?(:read) body.rewind req.body = body.read else req.body = body.to_s end end req end |
#put(path, body = nil, header = nil, opts = {}) ⇒ Object
85 86 87 88 |
# File 'lib/gateway/http.rb', line 85 def put(path, body=nil, header=nil, opts={}) req= prepare_request(:put, path, body, header) request(req, opts) end |
#read_timeout ⇒ Object
144 145 146 |
# File 'lib/gateway/http.rb', line 144 def read_timeout [:read_timeout] end |
#request(req, opts = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gateway/http.rb', line 50 def request(req, opts={}) opts = { :persistent => false, :retry => false }.merge(opts) unless idempotent?(req) action = req.method.downcase.to_sym execute(action, absolute_url(req), opts) do |conn| conn.start unless conn.started? rsp = conn.request(req) validate_response(req, rsp, valid_responses(opts)) if validate_response?(opts) rsp end end |
#valid_responses(opts) ⇒ Object
107 108 109 |
# File 'lib/gateway/http.rb', line 107 def valid_responses(opts) opts.fetch(:valid_responses, [ Net::HTTPSuccess ]) end |
#validate_response(req, rsp, valid_rsp) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/gateway/http.rb', line 111 def validate_response(req, rsp, valid_rsp) is_valid = valid_rsp.any?{|klass| rsp.is_a?(klass) } raise Gateway::BadResponse.new( "Invalid Response", :status => rsp.code, :url => absolute_url(req) ) unless is_valid end |
#validate_response?(opts) ⇒ Boolean
103 104 105 |
# File 'lib/gateway/http.rb', line 103 def validate_response?(opts) opts.fetch(:validate_response, true) end |