Class: HTTPX::Request
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/httpx/request.rb
Defined Under Namespace
Classes: Body, ProcIO
Constant Summary
collapse
- METHODS =
[
:options, :get, :head, :post, :put, :delete, :trace, :connect,
:propfind, :proppatch, :mkcol, :copy, :move, :lock, :unlock,
:orderpatch,
:acl,
:report,
:patch,
:search
].freeze
- USER_AGENT =
"httpx.rb/#{VERSION}"
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(verb, uri, options = {}) ⇒ Request
Returns a new instance of Request.
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/httpx/request.rb', line 45
def initialize(verb, uri, options = {})
@verb = verb.to_s.downcase.to_sym
@uri = URI(URI.escape(uri.to_s))
@options = Options.new(options)
raise(Error, "unknown method: #{verb}") unless METHODS.include?(@verb)
@headers = @options..new(@options.)
@headers["user-agent"] ||= USER_AGENT
@headers["accept"] ||= "*/*"
@body = @options.request_body_class.new(@headers, @options)
@state = :idle
end
|
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
35
36
37
|
# File 'lib/httpx/request.rb', line 35
def body
@body
end
|
Returns the value of attribute headers.
35
36
37
|
# File 'lib/httpx/request.rb', line 35
def
@headers
end
|
#response ⇒ Object
Returns the value of attribute response.
37
38
39
|
# File 'lib/httpx/request.rb', line 37
def response
@response
end
|
#state ⇒ Object
Returns the value of attribute state.
35
36
37
|
# File 'lib/httpx/request.rb', line 35
def state
@state
end
|
#uri ⇒ Object
Returns the value of attribute uri.
35
36
37
|
# File 'lib/httpx/request.rb', line 35
def uri
@uri
end
|
#verb ⇒ Object
Returns the value of attribute verb.
35
36
37
|
# File 'lib/httpx/request.rb', line 35
def verb
@verb
end
|
Instance Method Details
#authority ⇒ Object
76
77
78
|
# File 'lib/httpx/request.rb', line 76
def authority
@uri.authority
end
|
#drain_body ⇒ Object
95
96
97
98
99
100
101
102
|
# File 'lib/httpx/request.rb', line 95
def drain_body
return nil if @body.nil?
@drainer ||= @body.each
chunk = @drainer.next
chunk.dup
rescue StopIteration
nil
end
|
#expects? ⇒ Boolean
211
212
213
214
|
# File 'lib/httpx/request.rb', line 211
def expects?
@headers["expect"] == "100-continue" &&
@response && @response.status == 100
end
|
#inspect ⇒ Object
104
105
106
|
# File 'lib/httpx/request.rb', line 104
def inspect
"#<Request #{@verb.to_s.upcase} #{path} @headers=#{@headers.to_hash} @body=#{@body}>"
end
|
60
61
62
|
# File 'lib/httpx/request.rb', line 60
def (h)
@headers = @headers.merge(h)
end
|
#origin ⇒ Object
81
82
83
|
# File 'lib/httpx/request.rb', line 81
def origin
@uri.origin
end
|
#path ⇒ Object
68
69
70
71
72
73
|
# File 'lib/httpx/request.rb', line 68
def path
path = uri.path.dup
path << "/" if path.empty?
path << "?#{query}" unless query.empty?
path
end
|
#query ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/httpx/request.rb', line 85
def query
return @query if defined?(@query)
query = []
if (q = @options.params)
query << URI.encode_www_form(q)
end
query << @uri.query if @uri.query
@query = query.join("&")
end
|
#scheme ⇒ Object
64
65
66
|
# File 'lib/httpx/request.rb', line 64
def scheme
@uri.scheme
end
|
#transition(nextstate) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/httpx/request.rb', line 179
def transition(nextstate)
case nextstate
when :idle
@response = nil
when :headers
return unless @state == :idle
when :body
return unless @state == :headers ||
@state == :expect
if @headers.key?("expect")
unless @response
@state = :expect
return
end
case @response.status
when 100
@response = nil
when 417
@response = @response
return
end
end
when :done
return if @state == :expect
end
@state = nextstate
nil
end
|