Class: HttpspecSimple::Request
- Inherits:
-
Object
- Object
- HttpspecSimple::Request
- Defined in:
- lib/httpspec_simple/request.rb
Defined Under Namespace
Classes: CONFIG_CLASS
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#response_time ⇒ Object
readonly
Returns the value of attribute response_time.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(url, opt = {}) ⇒ Request
constructor
A new instance of Request.
- #process_time ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(url, opt = {}) ⇒ Request
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/httpspec_simple/request.rb', line 8 def initialize(url, opt = {}) @url = URI.parse(url) http = Net::HTTP.new(@url.host, @url.port) http.open_timeout = opt[:timeout] || Request.configuration.timeout http.read_timeout = opt[:timeout] || Request.configuration.timeout retry_count = (opt[:retry] || Request.configuration.retry).to_i res, @response_time = process_time do http.start do |http| open_timeout_error = if Net.const_defined?(:OpenTimeout) then Net::OpenTimeout else Timeout::Error end read_timeout_error = if Net.const_defined?(:ReadTimeout) then Net::ReadTimeout else Timeout::Error end begin req = Net::HTTP::Get.new(@url.request_uri) {}.merge( Request.configuration.headers || {} ).merge( opt[:headers] || {} ).each {|k, v| req[k] = v } if (basic_auth = opt[:basic_auth] || Request.configuration.basic_auth) req.basic_auth *basic_auth if Array === basic_auth end res = http.request(req) raise RequestError.new if res.kind_of?(Net::HTTPClientError) or res.kind_of?(Net::HTTPServerError) rescue open_timeout_error, read_timeout_error, RequestError retry if (retry_count-=1) > 0 end res end end @status = res ? res.code : 'timeout' unless res.nil? @body = res.body charset = (res['content-type'] || '').split(';').select{|i| i.start_with?('charset=')}.map{|i| i.sub('charset=', '')}[0] @body.force_encoding(charset) unless charset.nil? end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
6 7 8 |
# File 'lib/httpspec_simple/request.rb', line 6 def body @body end |
#response_time ⇒ Object (readonly)
Returns the value of attribute response_time.
6 7 8 |
# File 'lib/httpspec_simple/request.rb', line 6 def response_time @response_time end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
6 7 8 |
# File 'lib/httpspec_simple/request.rb', line 6 def status @status end |
Class Method Details
.configuration ⇒ Object
62 63 64 |
# File 'lib/httpspec_simple/request.rb', line 62 def configuration @config ||= reset_configuration end |
.configure {|config| ... } ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/httpspec_simple/request.rb', line 51 def configure config = CONFIG_CLASS.new(20, 0) yield config if block_given? configuration.timeout = config.timeout configuration.retry = config.retry configuration.headers = config.headers configuration.basic_auth = config.basic_auth end |
.reset_configuration ⇒ Object
66 67 68 |
# File 'lib/httpspec_simple/request.rb', line 66 def reset_configuration @config = CONFIG_CLASS.new(20, 0, {}) end |
Instance Method Details
#process_time ⇒ Object
40 41 42 43 44 |
# File 'lib/httpspec_simple/request.rb', line 40 def process_time start_time = Time.now ret = yield [ret, Time.now - start_time] end |
#to_s ⇒ Object
46 47 48 |
# File 'lib/httpspec_simple/request.rb', line 46 def to_s @url.to_s end |