Class: LibStorj::Ext::Storj::CreateBucketRequest

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/ruby-libstorj/ext/create_bucket_request.rb

Class Method Summary collapse

Class Method Details

.after_work_cbObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby-libstorj/ext/create_bucket_request.rb', line 16

def self.after_work_cb
  args = [::LibStorj::Ext::UV::Work.by_ref, :int]

  FFI::Function.new :void, args do |work_req_ptr|
    req = new work_req_ptr[:data]
    response = ::LibStorj::Ext::JsonC.stringify req[:response]
    error = ::LibStorj::Ext::Curl.curl_code_to_string req[:error_code]
    c_handle = FFI::Function.new :void, %i[string pointer], req[:handle]

    next c_handle.call(error, ::FFI::MemoryPointer::NULL) unless error.empty?

    status_code, bucket = req.values_at %i[status_code bucket]
    if ((status_code > 299) || bucket.id.nil?) && error.empty?
      response_error = JSON.parse(response)['error']
      error = response_error
      next c_handle.call(error, ::FFI::MemoryPointer::NULL)
    end

    bucket_pointer = req[:bucket]
    c_handle.call nil, bucket_pointer
  end
end

.ruby_handle(&block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby-libstorj/ext/create_bucket_request.rb', line 39

def self.ruby_handle(&block)
  FFI::Function.new :void, %i[string pointer] do |error, bucket_pointer|
    bucket = if bucket_pointer.null?
               nil
             else
               ::LibStorj::Ext::Storj::Bucket.new(bucket_pointer)
             end

    yield error, bucket if block_given?
  end
end