Class: Hubic
- Inherits:
-
Object
- Object
- Hubic
- Defined in:
- lib/hubic.rb,
lib/hubic/store.rb,
lib/hubic/version.rb,
lib/hubic/file_ops.rb,
lib/hubic/openstack.rb
Defined Under Namespace
Classes: Container, Error, Object, Store
Constant Summary collapse
- VERSION =
"0.0.3"
- TYPE_OCTET_STREAM =
'application/octet-stream'
- TYPE_DIRECTORY =
'application/directory'
Class Method Summary collapse
-
.default_client_id=(client_id) ⇒ Object
Set the default client Id to use.
-
.default_client_secret=(client_secret) ⇒ Object
Set the default client Secret to use.
-
.default_redirect_uri=(redirect_uri) ⇒ Object
Set the default redirect URI to use.
-
.for_user(user, password = nil, store: Store[user], force: false, &password_requester) ⇒ Hubic
Create a Hubic handler.
Instance Method Summary collapse
- #account ⇒ Object
-
#api_hubic(method, path, params = nil) ⇒ Hash
Make a call to the Hubic API.
- #api_openstack(method, path, params = nil) ⇒ Object
- #container(name) ⇒ Object
- #containers ⇒ Object
- #copy(src, dst) ⇒ Object
- #credentials ⇒ Object
- #default_container=(name) ⇒ Object
- #delete(src) ⇒ Object
- #download(obj, dst = nil, size: nil, offset: 0, &block) ⇒ Object
-
#for_user(user, password = nil, store: Store[user], force: false, &password_requester) ⇒ Object
Initialize the Hubic handler to perform operations on behalf of the user.
- #get_metadata(obj) ⇒ Object
- #get_object(obj, dst = nil, size: nil, offset: 0, &block) ⇒ Object
-
#initialize(client_id = @@client_id, client_secret = @@client_secret, redirect_uri = @@redirect_uri) ⇒ Hubic
constructor
Create a Hubic handler.
- #list(path = '/', container = @default_container) ⇒ Object
- #md5(obj) ⇒ Object
-
#mkdir(obj, parents: false) ⇒ Object
Create directory.
- #move(src, dst) ⇒ Object
- #normalize_object(obj) ⇒ Object
-
#objects(container = @default_container, path: nil, limit: nil, gt: nil, lt: nil) ⇒ Array
List objects store in a container.
- #openstack_setup(endpoint, token, expires) ⇒ Object
- #openstack_setup_refresh(force: false) ⇒ Object
- #parse_response_for_meta(response) ⇒ Object
- #put_object(obj, src, type = TYPE_OCTET_STREAM, &block) ⇒ Object
- #upload(src, obj, type = TYPE_OCTET_STREAM, &block) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(client_id = @@client_id, client_secret = @@client_secret, redirect_uri = @@redirect_uri) ⇒ Hubic
Create a Hubic handler
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hubic.rb', line 73 def initialize(client_id = @@client_id, client_secret = @@client_secret, redirect_uri = @@redirect_uri) @store = nil @refresh_token = nil @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri @conn = Faraday.new('https://api.hubic.com') do |faraday| faraday.request :url_encoded faraday.adapter :net_http faraday..params_encoder = Faraday::FlatParamsEncoder end @default_container = "default" end |
Class Method Details
.default_client_id=(client_id) ⇒ Object
Set the default client Id to use
39 40 41 |
# File 'lib/hubic.rb', line 39 def self.default_client_id=(client_id) @@client_id = client_id end |
.default_client_secret=(client_secret) ⇒ Object
Set the default client Secret to use
44 45 46 |
# File 'lib/hubic.rb', line 44 def self.default_client_secret=(client_secret) @@client_secret = client_secret end |
.default_redirect_uri=(redirect_uri) ⇒ Object
Set the default redirect URI to use
49 50 51 |
# File 'lib/hubic.rb', line 49 def self.default_redirect_uri=(redirect_uri) @@redirect_uri = redirect_uri end |
.for_user(user, password = nil, store: Store[user], force: false, &password_requester) ⇒ Hubic
Create a Hubic handler
60 61 62 63 64 65 66 |
# File 'lib/hubic.rb', line 60 def self.for_user(user, password=nil, store: Store[user], force: false, &password_requester) h = Hubic.new(@@client_id, @@client_secret, @@redirect_uri) h.for_user(user, password, store: store, force: force, &password_requester) h end |
Instance Method Details
#account ⇒ Object
126 127 128 |
# File 'lib/hubic.rb', line 126 def account api_hubic(:get, '/1.0/account') end |
#api_hubic(method, path, params = nil) ⇒ Hash
Make a call to the Hubic API
145 146 147 148 149 150 151 |
# File 'lib/hubic.rb', line 145 def api_hubic(method, path, params=nil) r = @conn.method(method).call(path) do |req| req.headers['Authorization'] = "Bearer #{@access_token}" req.params = params if params end JSON.parse(r.body) end |
#api_openstack(method, path, params = nil) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/hubic/openstack.rb', line 240 def api_openstack(method, path, params=nil) openstack_setup_refresh params ||= {} params[:format] ||= :json p = "#{@os[:endpoint]}#{'/' if path[0] != ?/}#{path}" r = @os[:conn].method(method).call(p) do |req| req.headers['X-Auth-Token'] = @os[:token] req.params = params end if r.body.nil? || r.body.empty? then [ nil, r.headers ] else [ JSON.parse(r.body), r.headers ] end end |
#container(name) ⇒ Object
232 233 234 |
# File 'lib/hubic/openstack.rb', line 232 def container(name) Container.new(self, name) end |
#containers ⇒ Object
226 227 228 229 230 |
# File 'lib/hubic/openstack.rb', line 226 def containers j, = api_openstack(:get, '/') Hash[j.map {|c| [ c['name'], { :size => c['bytes'].to_i, :count => c['count'].to_i } ] } ] end |
#copy(src, dst) ⇒ Object
71 72 73 |
# File 'lib/hubic/file_ops.rb', line 71 def copy(src, dst) raise "not implemented yet" end |
#credentials ⇒ Object
130 131 132 |
# File 'lib/hubic.rb', line 130 def credentials api_hubic(:get, '/1.0/account/credentials') end |
#default_container=(name) ⇒ Object
236 237 238 |
# File 'lib/hubic/openstack.rb', line 236 def default_container=(name) @default_container = name end |
#delete(src) ⇒ Object
79 80 81 |
# File 'lib/hubic/file_ops.rb', line 79 def delete(src) raise "not implemented yet" end |
#download(obj, dst = nil, size: nil, offset: 0, &block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/hubic/file_ops.rb', line 34 def download(obj, dst=nil, size: nil, offset: 0, &block) # Handle file name or nil as a posible destination io = nil dst = Pathname(dst) if String === dst _dst = case dst when Pathname then io = dst.open('w') when NilClass then io = StringIO.new else dst end # Get object = get_object(obj, _dst, size: size, offset: offset, &block) # If file name update the timestamp if (Pathname === dst) && [:lastmod] dst.utime([:lastmod], [:lastmod]) end # If destination is nil, returns the downloaded file # insteaded of the meta data if dst.nil? then io.flush.string else end ensure io.close unless io.nil? end |
#for_user(user, password = nil, store: Store[user], force: false, &password_requester) ⇒ Object
Initialize the Hubic handler to perform operations on behalf of the user
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 |
# File 'lib/hubic.rb', line 95 def for_user(user, password=nil, store: Store[user], force: false, &password_requester) @store = store @refresh_token = @store['refresh_token'] if @store && !force if @refresh_token data = refresh_access_token @access_token = data[:access_token] @expires_at = data[:expires_at ] else password ||= password_requester.call(user) if password_requester if password.nil? raise ArgumentError, "password requiered for user authorization" end code = get_request_code(user, password) data = get_access_token(code) @access_token = data[:access_token ] @expires_at = data[:expires_in ] @refresh_token = data[:refresh_token] if @store @store['refresh_token'] = @refresh_token @store.save end end end |
#get_metadata(obj) ⇒ Object
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 |
# File 'lib/hubic/openstack.rb', line 64 def (obj) container, path, uri = normalize_object(obj) = {} hdrs = {} hdrs['X-Auth-Token'] = @os[:token] http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true # http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.start http.request_head(uri.request_uri, hdrs) {|response| case response when Net::HTTPSuccess = (response) when Net::HTTPNotFound = nil when Net::HTTPRedirection fail "http redirect is not currently handled" when Net::HTTPUnauthorized # TODO: Need to refresh token else fail "resource unavailable: #{uri} (#{response.class})" end } ensure http.finish unless http.nil? end |
#get_object(obj, dst = nil, size: nil, offset: 0, &block) ⇒ Object
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 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 |
# File 'lib/hubic/openstack.rb', line 99 def get_object(obj, dst=nil, size: nil, offset: 0, &block) container, path, uri = normalize_object(obj) if !(IO === dst) && !dst.respond_to?(:write) && !(Proc === dst) && !dst.respond_to?(:call) raise ArgumentError, "unsupported destination" end = {} hdrs = {} hdrs['X-Auth-Token'] = @os[:token] if size hdrs['Range'] = sprintf("bytes=%d-%d", offset, offset + size - 1) end http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true # http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.start http.request_get(uri.request_uri, hdrs) {|response| case response when Net::HTTPSuccess when Net::HTTPRedirection fail "http redirect is not currently handled" when Net::HTTPUnauthorized # TODO: Need to refresh token else fail "resource unavailable: #{uri} (#{response.class})" end = (response) if block block.call() end response.read_body {|segment| if IO === dst then dst.write(segment) elsif Proc === dst then dst.call(segment) elsif dst.respond_to?(:write) then dst.write(segment) elsif dst.respond_to?(:call ) then dst.call(segment) end if block block.call(segment) end } } if block block.call(:done) end ensure http.finish unless http.nil? end |
#list(path = '/', container = @default_container) ⇒ Object
89 90 91 |
# File 'lib/hubic/file_ops.rb', line 89 def list(path = '/', container = @default_container) objects(container, path: path) end |
#md5(obj) ⇒ Object
83 84 85 86 |
# File 'lib/hubic/file_ops.rb', line 83 def md5(obj) = (obj) [:etag] end |
#mkdir(obj, parents: false) ⇒ Object
Create directory
7 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 |
# File 'lib/hubic/file_ops.rb', line 7 def mkdir(obj, parents: false) container, path, = normalize_object(obj) # Check for parents (create or raise error) parent = File.dirname(path) if ! %w[. /].include?(parent) if ( = ([container, parent])).nil? if parents mkdir([container, parent], parents: parents) else raise Error, "parent doesn't exist" end elsif [:type] != TYPE_DIRECTORY raise Error, "parent is not a directory" end end # Check if place is already taken before creating directory if ( = (obj)).nil? put_object(obj, nil, TYPE_DIRECTORY) elsif [:type] != TYPE_DIRECTORY raise Error::Exists, "a file already exists" elsif !parents raise Error::Exists, "the directory already exists" end end |
#move(src, dst) ⇒ Object
75 76 77 |
# File 'lib/hubic/file_ops.rb', line 75 def move(src, dst) raise "not implemented yet" end |
#normalize_object(obj) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/hubic/openstack.rb', line 285 def normalize_object(obj) openstack_setup_refresh # TODO: no need to refresh just get the endpoint c, p = case obj when String [ @default_container, obj ] when Hash [ obj[:name] || obj[:path], (obj[:container] || @default_container).to_s ] when Array case obj.length when 1 then [ @default_container, obj ] when 2 then Symbol === obj[1] ? [ obj[1], obj[0] ] : obj else raise ArguementError end else raise ArgumentError end c = c.to_s p = p[1..-1] if p[0] == ?/ [ c, p, URI("#{@os[:endpoint]}/#{c}/#{p}") ] end |
#objects(container = @default_container, path: nil, limit: nil, gt: nil, lt: nil) ⇒ Array
List objects store in a container.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/hubic/openstack.rb', line 212 def objects(container = @default_container, path: nil, limit: nil, gt: nil, lt: nil) path = path[1..-1] if path && path[0] == ?/ p = { path: path, limit: limit, marker: gt, end_marker: lt }.delete_if {|k,v| v.nil? } j, = api_openstack(:get, container, p) Hash[j.map {|o| [ o['name'], { :hash => o['hash'], :lastmod => Time.parse(o['last_modified']), :size => o['bytes'].to_i, :type => o['content_type'] } ] } ] end |
#openstack_setup(endpoint, token, expires) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/hubic/openstack.rb', line 270 def openstack_setup(endpoint, token, expires) conn = Faraday.new do |faraday| faraday.request :multipart faraday.request :url_encoded faraday.adapter :net_http faraday..params_encoder = Faraday::FlatParamsEncoder end @os = { :expires => expires, :token => token, :endpoint => endpoint, :conn => conn } end |
#openstack_setup_refresh(force: false) ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/hubic/openstack.rb', line 259 def openstack_setup_refresh(force: false) return unless force || @os.nil? || @os[:expires] <= Time.now data = self.credentials endpoint = data['endpoint'] token = data['token'] expires = Time.parse(data['expires']) openstack_setup(endpoint, token, expires) end |
#parse_response_for_meta(response) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/hubic/openstack.rb', line 55 def (response) { :lastmod => (Time.parse(response['last-modified']) rescue nil), :length => response.content_length, :type => response['content-type'], :etag => response['etag'] } end |
#put_object(obj, src, type = TYPE_OCTET_STREAM, &block) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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 |
# File 'lib/hubic/openstack.rb', line 162 def put_object(obj, src, type = TYPE_OCTET_STREAM, &block) container, path, uri = normalize_object(obj) case src when String io = File.open(src) when NilClass io = StringIO.new('') else raise ArgumentError, 'Not Implemented Yet' end hdrs = {} hdrs['X-Auth-Token' ] = @os[:token] hdrs['Transfer-Encoding'] = 'chunked' hdrs['Content-Type' ] = type http = Net::HTTP.new(uri.host, uri.port) if uri.scheme == 'https' http.use_ssl = true # http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.start request = Net::HTTP::Put.new(uri.request_uri, hdrs) request.body_stream = io http.request(request) {|response| case response when Net::HTTPSuccess when Net::HTTPRedirection fail "http redirect is not currently handled" when Net::HTTPUnauthorized # TODO: Need to refresh token else fail "resource unavailable: #{uri} (#{response.class})" end puts response.inspect } if block block.call(:done) end ensure io.close unless io.nil? http.finish unless http.nil? end |
#upload(src, obj, type = TYPE_OCTET_STREAM, &block) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/hubic/file_ops.rb', line 62 def upload(src, obj, type=TYPE_OCTET_STREAM, &block) case src when String, Pathname type = (MIME::Types.of(src).first || MIME::Types[TYPE_OCTET_STREAM].first).content_type end put_object(obj, src, type, &block) end |
#usage ⇒ Object
134 135 136 |
# File 'lib/hubic.rb', line 134 def usage api_hubic(:get, '/1.0/account/usage') end |