Class: Stupeflixclient::StupeflixBase
- Inherits:
-
Object
- Object
- Stupeflixclient::StupeflixBase
- Defined in:
- lib/stupeflixclient/stupeflix_base.rb
Direct Known Subclasses
Instance Method Summary collapse
- #answer_error(answer, message) ⇒ Object
- #connectionGet ⇒ Object
- #error(message) ⇒ Object
- #getContent(url, filename = nil, parameters = nil) ⇒ Object
- #getContent_(url, filename = nil, parameters = nil, raiseExceptionOn404 = true) ⇒ Object
- #getContentUrl(url, method, parameters) ⇒ Object
-
#initialize(accessKey, privateKey, host = "http://services.stupeflix.com", service = 'stupeflix-1.0', debug = false) ⇒ StupeflixBase
constructor
A new instance of StupeflixBase.
- #isZip(filename) ⇒ Object
- #logdebug(s) ⇒ Object
- #md5FileOrBody(filename, body = nil) ⇒ Object
- #paramString(parameters) ⇒ Object
-
#sendContent(method, url, contentType, filename = nil, body = nil, parameters = nil, sendcallback = nil) ⇒ Object
sendcallback is an object with - a ‘sendCallBack’ member function that accept a unique int argument (=number of bytes written so far) - a ‘sendBlockSize’ member function with no argument which return the size of block to be sent.
- #sign(strToSign, secretKey) ⇒ Object
- #signUrl(url, method, md5, mime, parameters = {}) ⇒ Object
- #strToSign(method, resource, md5, mime, datestr, parameters) ⇒ Object
Constructor Details
#initialize(accessKey, privateKey, host = "http://services.stupeflix.com", service = 'stupeflix-1.0', debug = false) ⇒ StupeflixBase
Returns a new instance of StupeflixBase.
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 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 8 def initialize( accessKey, privateKey, host = "http://services.stupeflix.com", service = 'stupeflix-1.0', debug = false) @accessKey = accessKey @privateKey = privateKey len = host.length - 7 if host[host.length - 1, 1] == "/" host = host[0, host.length - 1] end @host = host[7,len] @base_url = service @debug = debug @service = service @TEXT_XML_CONTENT_TYPE = "text/xml" @APPLICATION_ZIP_CONTENT_TYPE = "application/zip" @APPLICATION_JSON_CONTENT_TYPE = "application/json" @APPLICATION_URLENCODED_CONTENT_TYPE = "application/x-www-form-urlencoded" @PROFILES_PARAMETER = "Profiles" @XML_PARAMETER = "ProfilesXML" @MARKER_PARAMETER = "Marker" @MAXKEYS_PARAMETER = "MaxKeys" # Currently there is only the Marker parameter (used for partial enumeration) @parametersToAdd = [@MARKER_PARAMETER, @MAXKEYS_PARAMETER] @sleepTime = 1.0 @maxRetry = 4 @base = true end |
Instance Method Details
#answer_error(answer, message) ⇒ Object
116 117 118 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 116 def answer_error( answer, ) raise StandardError, sprintf( "%s\n%s", , answer['body']) end |
#connectionGet ⇒ Object
35 36 37 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 35 def connectionGet return Connection.new(@host, "/" + @base_url) end |
#error(message) ⇒ Object
111 112 113 114 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 111 def error( ) logdebug() raise StandardError, end |
#getContent(url, filename = nil, parameters = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 182 def getContent( url, filename = nil, parameters = nil) sleepTime = @sleepTime for i in 0..@maxRetry raiseExceptionOn404 = (i + 1) == @maxRetry ret = getContent_(url, filename, parameters, raiseExceptionOn404) if ret["status"] != 404 return ret end # Wait for amazon S3 ... sleep(1) end end |
#getContent_(url, filename = nil, parameters = nil, raiseExceptionOn404 = true) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 196 def getContent_( url, filename = nil, parameters = nil, raiseExceptionOn404 = true) method = "GET" url = getContentUrl(url, method, parameters) # GET DATA conn = connectionGet() answer = conn.request_get(url) body = answer['body'] headers = answer['headers'] status = headers['status'].to_i if status == 204 # there was no content obtainedSize = 0 if body.length != 0 error("204 status with non empty body.") end elsif status == 200 obtainedSize =headers['content-length'].to_i elsif status == 404 and not raiseExceptionOn404 return {"url" => headers['content-location'], "status" => 404} else msg = sprintf( "getContent : bad STATUS %s", status ) answer_error(answer, msg) end if body.length != obtainedSize error("Non matching body length and content-length") end if filename != nil f = File.open(filename, 'w') f.write(body) f.close if obtainedSize == 0 File.unlink(filename) else filesize = File.stat(filename).size if obtainedSize != filesize File.unlink(filename) error(sprintf( "file size is incorrect : file size = %d, body size = %d", filesize, obtainedSize)) end end end # NOW CHECK EVERYTHING IS OK md5, md5hex, md5base64 = md5FileOrBody(filename, body) if status != 204 obtainedMD5 = headers['etag'].gsub(/"/,"") if obtainedMD5 != md5hex if filename File.unlink(filename) end error(sprintf( "getDefinition : bad returned etag %s =! %s (ref)", md5hex, obtainedMD5)) end end if status == 200 logdebug(sprintf( "headers = %s", headers) ) url = headers['content-location'] ret = {'size' => obtainedSize, 'url' => url, 'headers' => headers} else ret = {'size' => obtainedSize, 'url' => url} end if not filename ret['body'] = body end ret["status"] = status return ret end |
#getContentUrl(url, method, parameters) ⇒ Object
178 179 180 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 178 def getContentUrl( url, method, parameters) return signUrl(url, method, "", "", parameters) end |
#isZip(filename) ⇒ Object
99 100 101 102 103 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 99 def isZip( filename) f = File.open(filename, 'r') header = f.read(4) return header == 'PK'+3.chr+4.chr end |
#logdebug(s) ⇒ Object
105 106 107 108 109 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 105 def logdebug( s) if @debug print s.to_s end end |
#md5FileOrBody(filename, body = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 75 def md5FileOrBody( filename, body = nil) md5 = Digest::MD5.new() if body != nil md5.update(body) else chunksize=1024 f = File.open(filename, 'r') while true chunk = f.read(chunksize) if not chunk break end md5.update(chunk) end f.close end digest = md5.digest return [digest, md5.hexdigest, Base64.encode64(digest).strip] end |
#paramString(parameters) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 39 def paramString( parameters) paramStr = "" if parameters != nil for p in @parametersToAdd if parameters.include?(p) paramStr += sprintf( "%s\n%s\n", p, parameters[p]) end end end return paramStr end |
#sendContent(method, url, contentType, filename = nil, body = nil, parameters = nil, sendcallback = nil) ⇒ Object
sendcallback is an object with
- a 'sendCallBack' member function that accept a unique int argument (=number of bytes written so far)
- a 'sendBlockSize' member function with no argument which return the size of block to be sent
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 123 def sendContent( method, url, contentType, filename = nil, body = nil, parameters = nil, sendcallback = nil) # SEND DATA conn = connectionGet() md5, md5hex, md5base64 = md5FileOrBody(filename, body) if filename size = File.stat(filename).size else size = body.length end headers = {'Content-MD5' => md5base64.to_s, 'Content-Length' => size.to_s, 'Content-Type' => contentType} url = signUrl(url, method, md5base64, contentType, parameters) # LAUNCH THE REQUEST : TODO : pass filename instead of body if method == "PUT" answer = conn.request_put(url, args = nil, body = body, filename = filename, headers = headers, sendcallback = sendcallback) elsif method == "POST" answer = conn.request_post(url, args = nil, body = body, filename = filename, headers = headers) elsif method == "DELETE" answer = conn.request_delete(url, headers = headers) end headers = answer['headers'] logdebug(headers) logdebug(answer['body']) # NOW CHECK THAT EVERYTHING IS OK status = headers['status'] if status != '200' msg = sprintf( "sendContent : bad STATUS %s", status ) answer_error(answer, msg) end if headers['etag'] == nil msg = "corrupted answer: no etag in headers. Response body is " + answer['body'] error(msg) end obtainedMD5 = headers['etag'] if obtainedMD5 != md5hex msg = sprintf( "sendContent : bad returned etags %s =! %s (ref)", obtainedMD5, md5hex) error(msg) end return answer end |
#sign(strToSign, secretKey) ⇒ Object
57 58 59 60 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 57 def sign( strToSign, secretKey) digest = OpenSSL::Digest.new('sha1') return OpenSSL::HMAC.hexdigest(digest, secretKey, strToSign) end |
#signUrl(url, method, md5, mime, parameters = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 62 def signUrl( url, method, md5, mime, parameters = {}) now =Time.now.to_i strToSign = strToSign(method, url, md5, mime, now, parameters) signature = sign(strToSign, @privateKey) url += sprintf( "?Date=%s&AccessKey=%s&Signature=%s", now, @accessKey,signature) if parameters parameters.each_pair do |k,v| url += sprintf("&%s=%s", k,v) end end return url end |
#strToSign(method, resource, md5, mime, datestr, parameters) ⇒ Object
51 52 53 54 55 |
# File 'lib/stupeflixclient/stupeflix_base.rb', line 51 def strToSign( method, resource, md5, mime, datestr, parameters) paramStr = paramString(parameters) stringToSign = sprintf( "%s\n%s\n%s\n%s\n%s\n%s", method, md5, mime, datestr, '/' + @service + resource, paramStr) return stringToSign end |