Class: LibStorj::Ext::Storj::ListFilesRequest

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

Class Method Summary collapse

Class Method Details

.after_work_cbObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby-libstorj/ext/list_files_request.rb', line 20

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

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

    catch :no_response_error do
      begin
        response_string = ::LibStorj::Ext::JsonC.stringify(response)
        response_hash = JSON.parse(response_string)

        unless response_hash.is_a?(Hash) && !response_hash['error'].nil?
          throw :no_response_error
        end

        error = response_error if response_error && error.empty?
      rescue JSON::ParserError => err
        # response didn't contain valid JSON;
        # therefore, it probably doesn't contain an error message
      end
    end

    c_handle.call error, files, total_files
  end
end

.ruby_handle(&block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby-libstorj/ext/list_files_request.rb', line 49

def self.ruby_handle(&block)
  FFI::Function.new :void, %i[string pointer int] do
  |error, files_pointer, file_count|
    files = ::LibStorj::Ext::Storj::File.pointer_to_array files_pointer,
                                                          file_count

    error = nil if error.empty?

    yield error, files
  end
end