Class: Agent
- Inherits:
-
Object
- Object
- Agent
- Defined in:
- lib/polyphony/http/client/agent.rb
Overview
Implements an HTTP agent
Constant Summary collapse
- OPTS_DEFAULT =
{}.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #do_request(ctx, &block) ⇒ Object
- #format_uri(url, ctx) ⇒ Object
- #get(url, opts = OPTS_DEFAULT, &block) ⇒ Object
-
#initialize ⇒ Agent
constructor
A new instance of Agent.
- #post(url, opts = OPTS_DEFAULT, &block) ⇒ Object
- #redirect(url, ctx, opts, &block) ⇒ Object
- #redirect_url(url, ctx) ⇒ Object
- #request(url, opts = OPTS_DEFAULT, &block) ⇒ Object
- #request_ctx(url, opts) ⇒ Object
- #send_request_and_check_response(adapter, ctx, &block) ⇒ Object
- #uri_key(uri) ⇒ Object
- #url_to_uri(url, opts) ⇒ Object
Constructor Details
#initialize ⇒ Agent
Returns a new instance of Agent.
24 25 26 27 28 |
# File 'lib/polyphony/http/client/agent.rb', line 24 def initialize @pools = Hash.new do |h, k| h[k] = SiteConnectionManager.new(k) end end |
Class Method Details
.default ⇒ Object
20 21 22 |
# File 'lib/polyphony/http/client/agent.rb', line 20 def self.default @default ||= new end |
.get(*args, &block) ⇒ Object
12 13 14 |
# File 'lib/polyphony/http/client/agent.rb', line 12 def self.get(*args, &block) default.get(*args, &block) end |
.post(*args, &block) ⇒ Object
16 17 18 |
# File 'lib/polyphony/http/client/agent.rb', line 16 def self.post(*args, &block) default.post(*args, &block) end |
Instance Method Details
#do_request(ctx, &block) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/polyphony/http/client/agent.rb', line 103 def do_request(ctx, &block) key = uri_key(ctx[:uri]) @pools[key].acquire do |adapter| send_request_and_check_response(adapter, ctx, &block) end rescue Exception => e p e puts e.backtrace.join("\n") end |
#format_uri(url, ctx) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/polyphony/http/client/agent.rb', line 72 def format_uri(url, ctx) format( '%<scheme>s://%<host>s%<url>s', scheme: ctx[:uri].scheme, host: ctx[:uri].host, url: url ) end |
#get(url, opts = OPTS_DEFAULT, &block) ⇒ Object
32 33 34 |
# File 'lib/polyphony/http/client/agent.rb', line 32 def get(url, opts = OPTS_DEFAULT, &block) request(url, opts.merge(method: :GET), &block) end |
#post(url, opts = OPTS_DEFAULT, &block) ⇒ Object
36 37 38 |
# File 'lib/polyphony/http/client/agent.rb', line 36 def post(url, opts = OPTS_DEFAULT, &block) request(url, opts.merge(method: :POST), &block) end |
#redirect(url, ctx, opts, &block) ⇒ Object
54 55 56 57 |
# File 'lib/polyphony/http/client/agent.rb', line 54 def redirect(url, ctx, opts, &block) url = redirect_url(url, ctx) request(url, opts, &block) end |
#redirect_url(url, ctx) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/polyphony/http/client/agent.rb', line 59 def redirect_url(url, ctx) case url when /^http(?:s)?\:\/\// url when /^\/\/(.+)$/ ctx[:uri].scheme + url when /^\// format_uri(url, ctx) else ctx[:uri] + url end end |
#request(url, opts = OPTS_DEFAULT, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/polyphony/http/client/agent.rb', line 40 def request(url, opts = OPTS_DEFAULT, &block) ctx = request_ctx(url, opts) response = do_request(ctx, &block) case response.status_code when 301, 302 redirect(response.headers['Location'], ctx, opts, &block) when 200, 204 response else raise "Error received from server: #{response.status_code}" end end |
#request_ctx(url, opts) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/polyphony/http/client/agent.rb', line 81 def request_ctx(url, opts) { method: opts[:method] || :GET, uri: url_to_uri(url, opts), opts: opts, retry: 0 } end |
#send_request_and_check_response(adapter, ctx, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/polyphony/http/client/agent.rb', line 114 def send_request_and_check_response(adapter, ctx, &block) response = adapter.request(ctx) case response.status_code when 200, 204 if block block.(response) else # read body response.body end end response end |
#uri_key(uri) ⇒ Object
128 129 130 |
# File 'lib/polyphony/http/client/agent.rb', line 128 def uri_key(uri) { scheme: uri.scheme, host: uri.host, port: uri.port } end |
#url_to_uri(url, opts) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/polyphony/http/client/agent.rb', line 90 def url_to_uri(url, opts) uri = URI(url) if opts[:query] query = opts[:query].map { |k, v| "#{k}=#{v}" }.join('&') if uri.query v.query = "#{uri.query}&#{query}" else uri.query = query end end uri end |