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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby-box/client.rb', line 66

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



42
43
44
45
# File 'lib/ruby-box/client.rb', line 42

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

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



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

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



25
26
27
28
29
30
# File 'lib/ruby-box/client.rb', line 25

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

#folder(path = '/') ⇒ Object



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

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

#item(path) ⇒ Object



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

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

#meObject



111
112
113
114
# File 'lib/ruby-box/client.rb', line 111

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

#root_folderObject



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

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

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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby-box/client.rb', line 52

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



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

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

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



47
48
49
50
# File 'lib/ruby-box/client.rb', line 47

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

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



83
84
85
86
87
88
# File 'lib/ruby-box/client.rb', line 83

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



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

def upload_file(local_path, remote_path, overwrite=true)
  file_name = local_path.split('/').pop
  folder = create_folder( remote_path )
  return unless folder
  ::File.open( local_path ) do |data|
    folder.upload_file(file_name, data, overwrite)
  end
end