Class: RestclientCommunicator::Communication
- Inherits:
-
Object
- Object
- RestclientCommunicator::Communication
- Defined in:
- lib/restclient_communicator.rb
Overview
Your code goes here…
Constant Summary collapse
- VALID_SCHEMES =
["http","https"]
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#errorcode ⇒ Object
Returns the value of attribute errorcode.
-
#file ⇒ Object
Returns the value of attribute file.
-
#http_code ⇒ Object
Returns the value of attribute http_code.
-
#options ⇒ Object
Returns the value of attribute options.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #check_url(url) ⇒ Object
-
#initialize(url, options = {}) ⇒ Communication
constructor
A new instance of Communication.
- #start ⇒ Object
Constructor Details
#initialize(url, options = {}) ⇒ Communication
Returns a new instance of Communication.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/restclient_communicator.rb', line 15 def initialize(url, ={}) return if url.blank? = { :method => :get, :open_timeout => 5, :read_timeout => 5, :max_redirects => 1, :raw_response => false, } @options = .reverse_merge() @errorcode, @response, @http_code, @body, @file = nil self.check_url(url) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
11 12 13 |
# File 'lib/restclient_communicator.rb', line 11 def body @body end |
#errorcode ⇒ Object
Returns the value of attribute errorcode.
11 12 13 |
# File 'lib/restclient_communicator.rb', line 11 def errorcode @errorcode end |
#file ⇒ Object
Returns the value of attribute file.
11 12 13 |
# File 'lib/restclient_communicator.rb', line 11 def file @file end |
#http_code ⇒ Object
Returns the value of attribute http_code.
11 12 13 |
# File 'lib/restclient_communicator.rb', line 11 def http_code @http_code end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/restclient_communicator.rb', line 11 def @options end |
#response ⇒ Object
Returns the value of attribute response.
11 12 13 |
# File 'lib/restclient_communicator.rb', line 11 def response @response end |
Instance Method Details
#check_url(url) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/restclient_communicator.rb', line 29 def check_url(url) begin new_url = Addressable::URI.parse(url) #muss ich jetztz bereinigen, da dieser wert 1:1 an restclient geht if new_url if !VALID_SCHEMES.include? new_url.scheme @errorcode = "CE9991" return end [:url] = new_url.normalize.to_str self.start else @errorcode = "CE9993" end rescue Addressable::URI::TypeError, Addressable::URI::NoMethodError, Addressable::URI::InvalidURIError => e @errorcode = "CE9999" end end |
#start ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/restclient_communicator.rb', line 51 def start begin #for result codes between 200 and 207, a RestClient::Response will be returned #for result codes 301, 302 or 307, the redirection will be followed if the request is a GET or a HEAD #for result code 303, the redirection will be followed and the request transformed into a GET #for other cases, a RestClient::ExceptionWithResponse holding the Response will be raised; a specific exception class will be thrown for known error codes #call .response on the exception to get the server's response @response = RestClient::Request.execute(**@options) @http_code = @response.code case @http_code when 200,207 #https://github.com/rest-client/rest-client/blob/master/lib/restclient/raw_response.rb #In addition, if you do not use the response as a string, you can access #a Tempfile object at res.file, which contains the path to the raw #downloaded request body. if @options[:raw_response] @file = @response.file else @body = @response.body end else case @options[:method] when :get, :head #bei get wird automatisch einem redirect gefolgt #For GET and HEAD requests, rest-client automatically follows redirection. case @http_code when 301,302,307 @body = @response.body end when :post case @http_code when 301, 302, 307 @response.follow_redirection end end end rescue RestClient::MovedPermanently, RestClient::Found, RestClient::TemporaryRedirect => e @response = e.response @http_code = @response.code @errorcode = "CE9920" if @options[:max_redirects] == 0 #case @options[:method] #when :post #todo das ist nich nicht so oaky #e.response.follow_redirection rescue RestClient::Unauthorized => e @http_code = e.response.code @errorcode = "CE9901" rescue RestClient::Forbidden => e @http_code = e.response.code @errorcode = "CE9902" rescue RestClient::ImATeapot => e @http_code = e.response.code @errorcode = "CE9903" rescue RestClient::NotFound => e @http_code = e.response.code @errorcode = "CE9904" rescue RestClient::Exceptions::Timeout => e #könnte man auch noch unterscheiden RestClient::Exceptions::Timeout::OpenTimeout, RestClient::Exceptions::Timeout::ReadTimeout #https://github.com/rest-client/rest-client/blob/master/lib/restclient/exceptions.rb @errorcode = "CE9905" rescue RestClient::ServerBrokeConnection => e @errorcode = "CE9906" rescue RestClient::SSLCertificateNotVerified => e @errorcode = "CE9907" rescue RestClient::PayloadTooLarge => e @errorcode = "CE9908" @http_code = e.response.code rescue RestClient::RequestURITooLong => e @errorcode = "CE9909" rescue RestClient::RequestedRangeNotSatisfiable => e @errorcode = "CE9910" end end |