Class: RubyBox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-box/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Client

Returns a new instance of Client.


10
11
12
# File 'lib/ruby-box/client.rb', line 10

def initialize(session)
  @session = session
end

Instance Method Details

#create_folder(path) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby-box/client.rb', line 75

def create_folder(path)
  folder = root_folder
  folder_names = split_path(path)
  folder_names.each do |folder_name|
    new_folder = folder.folders(folder_name).first        
    if !new_folder
      begin
        new_folder = folder.create_subfolder(folder_name)
      rescue RubyBox::ItemNameInUse => e
        new_folder = folder.folders(folder_name).first
      end
    end
    folder = new_folder
  end
  folder
end

#download(path) ⇒ Object


51
52
53
54
# File 'lib/ruby-box/client.rb', line 51

def download(path)
  file = file(path)
  file.download if file
end

#event_response(stream_position = 0, stream_type = :all, limit = 100) ⇒ Object


114
115
116
117
118
119
# File 'lib/ruby-box/client.rb', line 114

def event_response(stream_position=0, stream_type=:all, limit=100)
  q = fmt_events_args stream_position, stream_type, limit
  url = "#{RubyBox::API_URL}/events?#{q}"
  resp = @session.get( url )
  EventResponse.new(@session, resp)
end

#file(path) ⇒ Object


34
35
36
37
38
39
# File 'lib/ruby-box/client.rb', line 34

def file(path)
  path = split_path( path.sub(/^\.\//, '') )
  file_name = path.pop
  folder = folder_from_split_path( path )
  folder.files(file_name).first if folder
end

#file_by_id(id) ⇒ Object


29
30
31
32
# File 'lib/ruby-box/client.rb', line 29

def file_by_id(id)
  file = File.new(@session, {'id' => id})
  file.reload_meta
end

#folder(path = '/') ⇒ Object


23
24
25
26
27
# File 'lib/ruby-box/client.rb', line 23

def folder(path='/')
  path = path.sub(/(^\.$)|(^\.\/)/, '') if path # handle folders with leading '.'
  return root_folder if ['', '/'].member?(path)
  folder_from_split_path( split_path(path) )
end

#folder_by_id(id) ⇒ Object


18
19
20
21
# File 'lib/ruby-box/client.rb', line 18

def folder_by_id(id)
  folder = Folder.new(@session, {'id' => id})
  folder.reload_meta
end

#item(path) ⇒ Object


41
42
43
44
45
46
47
48
49
# File 'lib/ruby-box/client.rb', line 41

def item(path)
  path = split_path( path.sub(/^\.\//, '') )
  item_name = path.pop
  folder = folder_from_split_path( path )

  folder.items.select do |item|
    item.instance_variable_get(:@raw_item)['name'] and item.name == item_name
  end.first
end

#login_tokenObject


134
135
136
# File 'lib/ruby-box/client.rb', line 134

def 
  RubyBox::LoginToken.new(@session, {}).create
end

#meObject


121
122
123
124
# File 'lib/ruby-box/client.rb', line 121

def me
  resp = @session.get( "#{RubyBox::API_URL}/users/me" )
  User.new(@session, resp)
end

#root_folderObject


14
15
16
# File 'lib/ruby-box/client.rb', line 14

def root_folder
  folder_by_id('0')
end

#search(query, item_limit = 100, offset = 0) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby-box/client.rb', line 61

def search(query, item_limit=100, offset=0)
  Enumerator.new do |yielder|
    while true
      url = "#{RubyBox::API_URL}/search?query=#{URI::encode(query)}&limit=#{item_limit}&offset=#{offset}"
      resp = @session.get( url )
      resp['entries'].each do |entry|
        yielder.yield(RubyBox::Item.factory(@session, entry))
      end
      offset += resp['entries'].count
      break if resp['offset'].to_i + resp['limit'].to_i >= resp['total_count'].to_i
    end
  end
end

#split_path(path) ⇒ Object


109
110
111
112
# File 'lib/ruby-box/client.rb', line 109

def split_path(path)
  path.gsub!(/(^\/)|(\/$)/, '')
  path.split('/')
end

#stream(path, opts = {}) ⇒ Object


56
57
58
59
# File 'lib/ruby-box/client.rb', line 56

def stream(path, opts={})
  file = file(path)
  file.stream(opts) if file
end

#upload_data(path, data, overwrite = true) ⇒ Object


92
93
94
95
96
97
# File 'lib/ruby-box/client.rb', line 92

def upload_data(path, data, overwrite=true)
  path = split_path(path)
  file_name = path.pop
  folder = create_folder(path.join('/'))
  folder.upload_file(file_name, data, overwrite) if folder
end

#upload_file(local_path, remote_path, overwrite = true) ⇒ Object


99
100
101
102
# File 'lib/ruby-box/client.rb', line 99

def upload_file(local_path, remote_path, overwrite=true)
  folder = create_folder( remote_path )
  upload_file_to_folder(local_path, folder, overwrite)
end

#upload_file_by_folder_id(local_path, folder_id, overwrite = true) ⇒ Object


104
105
106
107
# File 'lib/ruby-box/client.rb', line 104

def upload_file_by_folder_id(local_path, folder_id, overwrite=true)
  folder = folder_by_id(folder_id)
  upload_file_to_folder(local_path, folder, overwrite)
end

#users(filter_term = "", limit = 100, offset = 0) ⇒ Object


126
127
128
129
130
131
132
# File 'lib/ruby-box/client.rb', line 126

def users(filter_term = "", limit = 100, offset = 0)
  url = "#{RubyBox::API_URL}/users?filter_term=#{URI::encode(filter_term)}&limit=#{limit}&offset=#{offset}"
  resp = @session.get( url )
  resp['entries'].map do |entry|
    RubyBox::Item.factory(@session, entry)
  end
end