Class: AgentX::Request
- Inherits:
-
Object
- Object
- AgentX::Request
- Defined in:
- lib/agentx/request.rb
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #base_url ⇒ Object
- #body(body = nil) ⇒ Object
- #cache_key ⇒ Object
- #cacheable? ⇒ Boolean
- #full_url ⇒ Object
- #get(params = {}) ⇒ Object
- #head(params = {}) ⇒ Object
- #headers(headers = {}) ⇒ Object
- #host ⇒ Object
- #html ⇒ Object
- #html? ⇒ Boolean
-
#initialize(session, url, params = {}) ⇒ Request
constructor
A new instance of Request.
- #inspect ⇒ Object
-
#json ⇒ Object
Force: parse response as json (normally type is determined by correctly set headers..
- #json? ⇒ Boolean
- #params(params = nil) ⇒ Object
- #port ⇒ Object
- #post(body = {}) ⇒ Object
- #put(body = {}) ⇒ Object
- #request_time ⇒ Object
- #scheme ⇒ Object
- #timings ⇒ Object
- #xml ⇒ Object
- #xml? ⇒ Boolean
Constructor Details
#initialize(session, url, params = {}) ⇒ Request
Returns a new instance of Request.
7 8 9 10 11 12 |
# File 'lib/agentx/request.rb', line 7 def initialize(session, url, params={}) @session = session @url, @params = url, params @headers = session.headers @body = {} end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/agentx/request.rb', line 5 def method @method end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
5 6 7 |
# File 'lib/agentx/request.rb', line 5 def url @url end |
Instance Method Details
#base_url ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/agentx/request.rb', line 118 def base_url return @session.base_url if @session.base_url if uri.port != AgentX::Session::DEFAULT_PORT[uri.scheme] "#{uri.scheme}://#{uri.host}:#{uri.port}" else "#{uri.scheme}://#{uri.host}" end end |
#body(body = nil) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/agentx/request.rb', line 23 def body(body=nil) if @method @body else @body.merge!(body) if body self end end |
#cache_key ⇒ Object
138 139 140 141 |
# File 'lib/agentx/request.rb', line 138 def cache_key k = [full_url, method, body, params] Digest::MD5.hexdigest(Oj.dump(k, mode: :compat)) end |
#cacheable? ⇒ Boolean
61 62 63 |
# File 'lib/agentx/request.rb', line 61 def cacheable? method == 'GET' || method == 'HEAD' end |
#full_url ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/agentx/request.rb', line 128 def full_url @full_url ||= case when url.start_with?('/') "#{@session.base_url}#{url}" when url.start_with?('http://', 'https://') url else "#{@session.relative_base_url}#{url}" end end |
#get(params = {}) ⇒ Object
46 47 48 49 |
# File 'lib/agentx/request.rb', line 46 def get(params={}) params(params) http(:get) end |
#head(params = {}) ⇒ Object
41 42 43 44 |
# File 'lib/agentx/request.rb', line 41 def head(params={}) params(params) http(:head) end |
#headers(headers = {}) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/agentx/request.rb', line 14 def headers(headers={}) if @method @headers else headers.each { |k,v| set_header(k, v) } self end end |
#host ⇒ Object
106 107 108 |
# File 'lib/agentx/request.rb', line 106 def host uri.host end |
#html ⇒ Object
78 79 80 81 82 |
# File 'lib/agentx/request.rb', line 78 def html @parse_type = :html self end |
#html? ⇒ Boolean
84 85 86 |
# File 'lib/agentx/request.rb', line 84 def html? @parse_type == :html end |
#inspect ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/agentx/request.rb', line 98 def inspect if @method "(Request #{@method.to_s.upcase} #{url})" else "(Request #{url})" end end |
#json ⇒ Object
Force: parse response as json (normally type is determined by correctly set headers.
68 69 70 71 72 |
# File 'lib/agentx/request.rb', line 68 def json @parse_type = :json self end |
#json? ⇒ Boolean
74 75 76 |
# File 'lib/agentx/request.rb', line 74 def json? @parse_type == :json end |
#params(params = nil) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/agentx/request.rb', line 32 def params(params=nil) if @method @params else @params.merge!(params) if params self end end |
#port ⇒ Object
114 115 116 |
# File 'lib/agentx/request.rb', line 114 def port uri.port end |
#post(body = {}) ⇒ Object
51 52 53 54 |
# File 'lib/agentx/request.rb', line 51 def post(body={}) body(body) http(:post) end |
#put(body = {}) ⇒ Object
56 57 58 59 |
# File 'lib/agentx/request.rb', line 56 def put(body={}) body(body) http(:put) end |
#request_time ⇒ Object
151 152 153 |
# File 'lib/agentx/request.rb', line 151 def request_time timings[:http_request] end |
#scheme ⇒ Object
110 111 112 |
# File 'lib/agentx/request.rb', line 110 def scheme uri.scheme end |
#timings ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/agentx/request.rb', line 143 def timings ts = {} (@times || {}).each do |k,v| ts[k] = "#{'%.2f' % (v * 1000)}ms" end ts end |
#xml ⇒ Object
88 89 90 91 92 |
# File 'lib/agentx/request.rb', line 88 def xml @parse_type = :xml self end |
#xml? ⇒ Boolean
94 95 96 |
# File 'lib/agentx/request.rb', line 94 def xml? @parse_type == :xml end |