Class: LibStorj::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-libstorj/env.rb

Constant Summary collapse

C_ANALOGUE =
::LibStorj::Ext::Storj::Env

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, **options) ⇒ Env

Returns a new instance of Env.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby-libstorj/env.rb', line 9

def initialize(path:, **options)
  # NB: ensure all options are pointers before passing them directly
  options = {} unless options.all? {|_, o| o.is_a? ::FFI::Pointer}

  if !path.nil? && File.exists?(path)
    options = ::LibStorj.load_yaml_options(path).merge options
  end

  # puts options
  @storj_env = ::LibStorj::Ext::Storj.method(:init_env).call(*options.values)

  # use ruby libuv's default_loop
  @storj_env[:loop] = ::Libuv::Ext.default_loop
end

Instance Attribute Details

#storj_envObject (readonly)

Returns the value of attribute storj_env.



5
6
7
# File 'lib/ruby-libstorj/env.rb', line 5

def storj_env
  @storj_env
end

Instance Method Details

#create_bucket(name, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby-libstorj/env.rb', line 71

def create_bucket(name, &block)
  req_data_type = ::LibStorj::Ext::Storj::CreateBucketRequest
  after_work_cb = req_data_type.after_work_cb
  ruby_handle = req_data_type.ruby_handle(&block)

  # calls uv_queue_work
  status = ::LibStorj::Ext::Storj::Bucket.create @storj_env,
                                          name,
                                          ruby_handle,
                                          after_work_cb
  reactor.run # calls uv_run
  status
end

#delete_bucket(bucket_id, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby-libstorj/env.rb', line 42

def delete_bucket(bucket_id, &block)
  req_data_type = ::LibStorj::Ext::Storj::JsonRequest
  after_work_cb = req_data_type.after_work_cb
  ruby_handle = req_data_type.ruby_handle do |error|
    yield error if block_given?
  end

  # calls uv_queue_work
  status = ::LibStorj::Ext::Storj::Bucket.delete @storj_env,
                                          bucket_id,
                                          ruby_handle,
                                          after_work_cb
  reactor.run # calls uv_run
  status
end

#delete_file(bucket_id, file_id, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby-libstorj/env.rb', line 98

def delete_file(bucket_id, file_id, &block)
  req_data_type = ::LibStorj::Ext::Storj::JsonRequest
  after_work_cb = req_data_type.after_work_cb
  ruby_handle = req_data_type.ruby_handle(&block)

  # calls uv_queue_work
  status = ::LibStorj::Ext::Storj::File.delete @storj_env,
                                        bucket_id,
                                        file_id,
                                        ruby_handle,
                                        after_work_cb
  reactor.run # calls uv_run
  status
end

#destroyObject



24
25
26
# File 'lib/ruby-libstorj/env.rb', line 24

def destroy
  ::LibStorj::Ext::Storj.destroy_env @storj_env
end

#get_buckets(&block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby-libstorj/env.rb', line 58

def get_buckets(&block)
  req_data_type = ::LibStorj::Ext::Storj::GetBucketRequest
  after_work_cb = req_data_type.after_work_cb
  ruby_handle = req_data_type.ruby_handle(&block)

  # calls uv_queue_work
  status = ::LibStorj::Ext::Storj::Bucket.all @storj_env,
                                       ruby_handle,
                                       after_work_cb
  reactor.run # calls uv_run
  status
end

#get_info(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-libstorj/env.rb', line 28

def get_info(&block)
  ruby_handle = ::LibStorj::Ext::Storj::JsonRequest.ruby_handle(&block)
  after_work_cb = ::LibStorj::Ext::Storj::JsonRequest.after_work_cb do |error|
    yield error if block_given?
  end

  # calls uv_queue_work
  status = ::LibStorj::Ext::Storj.get_info @storj_env,
                                  ruby_handle,
                                  after_work_cb
  reactor.run # calls uv_run
  status
end

#list_files(bucket_id, &block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby-libstorj/env.rb', line 85

def list_files(bucket_id, &block)
  req_data_type = ::LibStorj::Ext::Storj::ListFilesRequest
  after_work_cb = req_data_type.after_work_cb
  ruby_handle = req_data_type.ruby_handle(&block)

  status = ::LibStorj::Ext::Storj::File.all @storj_env,
                                     bucket_id,
                                     ruby_handle,
                                     after_work_cb
  reactor.run # calls uv_run
  status
end

#resolve_file(bucket_id:, file_id:, file_path:, progress_proc: nil, finished_proc: nil, &block) ⇒ Object



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
# File 'lib/ruby-libstorj/env.rb', line 113

def resolve_file(bucket_id:,
                 file_id:,
                 file_path:,
                 progress_proc: nil,
                 finished_proc: nil,
                 &block)
  file_descriptor = ::LibStorj::Ext::LibC.fopen(file_path, 'w+')
  progress_cb = FFI::Function.new :void, i[double uint64 uint64 pointer] do
  |progress, downloaded_bytes, total_bytes, handle|
    progress_proc.call progress, downloaded_bytes, total_bytes if progress_proc
  end

  # TODO: decide precedence
  finished_proc = finished_proc || block
  finished_cb = FFI::Function.new :void, i[int pointer pointer] do |status, file_id, handle|
    # do error handling based on status
    ::LibStorj::Ext::LibC.fclose(file_descriptor)
    finished_proc.call file_id
  end

  # ruby_handle = FFI::MemoryPointer::NULL
  ruby_handle = FFI::Function.new :void, [] do
  end

  # calls uv_queue_work
  state = ::LibStorj::Ext::Storj::File.resolve @storj_env,
                                                        bucket_id,
                                                        file_id,
                                                        file_descriptor,
                                                        ruby_handle,
                                                        progress_cb,
                                                        finished_cb
  reactor.run # calls uv_run
  state
end

#store_file(bucket_id:, file_path:, progress_proc: nil, finished_proc: nil, options: {}, &block) ⇒ Object



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
177
178
179
180
181
182
183
184
185
# File 'lib/ruby-libstorj/env.rb', line 149

def store_file(bucket_id:,
               file_path:,
               progress_proc: nil,
               finished_proc: nil,
               options: {},
               &block)
  default_options = {
      bucket_id: bucket_id,
      file_path: file_path,
  }

  options = options.merge(default_options) {|key, oldval| oldval}
  upload_options =
      ::LibStorj::Ext::Storj::UploadOptions.new options

  progress_cb = FFI::Function.new :void, i[double uint64 uint64 pointer] do
  |progress, bytes, total_bytes|
    progress_proc.call progress, bytes, total_bytes if progress_proc
  end

  #TODO: decide precedence
  finished_proc = finished_proc || block
  finished_cb = FFI::Function.new :void, i[int string pointer] do
  |status, file_id, handle|
    # do error handling based on status
    finished_proc.call file_id
  end

  # calls uv_queue_work
  state = ::LibStorj::Ext::Storj::File.store @storj_env,
                                             upload_options,
                                             FFI::MemoryPointer::NULL,
                                             progress_cb,
                                             finished_cb
  reactor.run #calls uv_run
  state
end