Class: Stupeflixclient::Connection
- Inherits:
-
Object
- Object
- Stupeflixclient::Connection
- Defined in:
- lib/stupeflixclient/connection.rb
Instance Method Summary collapse
- #dump(message, request_uri, method) ⇒ Object
- #fetch(uri_str, limit = 10) ⇒ Object
-
#initialize(server, base_url) ⇒ Connection
constructor
A new instance of Connection.
- #request(resource, method = "get", args = nil, body = nil, filename = nil, headers = {}, sendcallback = nil) ⇒ Object
- #request_delete(resource, args = nil, headers = {}) ⇒ Object
- #request_get(resource, args = nil, headers = {}) ⇒ Object
- #request_head(resource, args = nil, headers = {}) ⇒ Object
- #request_method(verb) ⇒ Object
- #request_post(resource, args = nil, body = nil, filename = nil, headers = {}) ⇒ Object
- #request_put(resource, args = nil, body = nil, filename = nil, headers = {}, sendcallback = nil) ⇒ Object
Constructor Details
#initialize(server, base_url) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 12 |
# File 'lib/stupeflixclient/connection.rb', line 7 def initialize(server, base_url) @server = server @base_url = base_url @MAX_NETWORK_RETRY = 1 @debuglevel = 0 end |
Instance Method Details
#dump(message, request_uri, method) ⇒ Object
36 37 38 39 40 |
# File 'lib/stupeflixclient/connection.rb', line 36 def dump( , request_uri, method) if @debuglevel > 0 print , " " , time.asctime, " " , request_uri, "\n" end end |
#fetch(uri_str, limit = 10) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/stupeflixclient/connection.rb', line 46 def fetch(uri_str, limit = 10) # You should choose better exception. raise ArgumentError, 'HTTP redirect too deep' if limit == 0 response = Net::HTTP.get_response(URI.parse(uri_str)) case response when Net::HTTPSuccess then response when Net::HTTPRedirection then fetch(response['location'], limit - 1) else response.error! end end |
#request(resource, method = "get", args = nil, body = nil, filename = nil, headers = {}, sendcallback = nil) ⇒ Object
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 |
# File 'lib/stupeflixclient/connection.rb', line 60 def request( resource, method = "get", args = nil, body = nil, filename=nil, headers={}, sendcallback = nil) params = nil path = resource if headers == nil headers = {} end headers['User-Agent'] = 'Basic Agent' # TEMPORARY : add support for streaming if method != "get" if not body and filename bodystream = File.open(filename, 'r') end end if args path += "?" + urllib.urlencode(args) end url = 'http://' + @server + @base_url + path dump("Connection Request starting", path, method.upcase) for i in 0..@MAX_NETWORK_RETRY begin # http.set_debug_output $stdout # headers['Expect'] = '100-Continue' response = nil while true url = URI.parse(url) request = request_method(method).new(url.path + "?" + url.query, headers) if bodystream request.body_stream = bodystream elsif body request.body = body end http = Net::HTTP.new(url.host, url.port) result = http.start {|http| response = http.request(request) } case response when Net::HTTPRedirection then url = response['location'] else break end end break rescue StandardError => e if i == (@MAX_NETWORK_RETRY - 1) raise end end end response["status"] = response.code.to_s return {'headers' => response, 'body' => response.body} end |
#request_delete(resource, args = nil, headers = {}) ⇒ Object
18 19 20 |
# File 'lib/stupeflixclient/connection.rb', line 18 def request_delete( resource, args = nil, headers={}) return request(resource, "delete", args, headers=headers) end |
#request_get(resource, args = nil, headers = {}) ⇒ Object
14 15 16 |
# File 'lib/stupeflixclient/connection.rb', line 14 def request_get( resource, args = nil, headers={}) return request(resource, "get", args, body = nil, filename = nil, headers=headers) end |
#request_head(resource, args = nil, headers = {}) ⇒ Object
22 23 24 |
# File 'lib/stupeflixclient/connection.rb', line 22 def request_head( resource, args = nil, headers={}) return request(resource, "head", args, headers=headers) end |
#request_method(verb) ⇒ Object
42 43 44 |
# File 'lib/stupeflixclient/connection.rb', line 42 def request_method(verb) Net::HTTP.const_get(verb.to_s.capitalize) end |
#request_post(resource, args = nil, body = nil, filename = nil, headers = {}) ⇒ Object
26 27 28 |
# File 'lib/stupeflixclient/connection.rb', line 26 def request_post( resource, args = nil, body = nil, filename=nil, headers={}) return request(resource, "post", args , body = body, filename=filename, headers=headers) end |
#request_put(resource, args = nil, body = nil, filename = nil, headers = {}, sendcallback = nil) ⇒ Object
30 31 32 33 34 |
# File 'lib/stupeflixclient/connection.rb', line 30 def request_put(resource, args = nil, body = nil, filename=nil, headers={}, sendcallback = nil) dump("In request_put in connection", resource, "PUT") resp = request(resource, "put", args , body = body, filename=filename, headers=headers, sendcallback = sendcallback) return resp end |