Class: Cloudpt::API::Client

Inherits:
Object
  • Object
show all
Includes:
Files
Defined in:
lib/cloudpt-api/client.rb,
lib/cloudpt-api/client/files.rb

Defined Under Namespace

Modules: Files

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Files

#copy_from_copy_ref, #download, #upload

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
# File 'lib/cloudpt-api/client.rb', line 11

def initialize(options = {})
  @connection = Cloudpt::API::Connection.new(:token  => options.delete(:token),
                                             :secret => options.delete(:secret))
  @raw        = Cloudpt::API::Raw.new :connection => @connection
  @options    = options
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



9
10
11
# File 'lib/cloudpt-api/client.rb', line 9

def connection
  @connection
end

#rawObject

Returns the value of attribute raw.



9
10
11
# File 'lib/cloudpt-api/client.rb', line 9

def raw
  @raw
end

Instance Method Details

#accountObject



37
38
39
# File 'lib/cloudpt-api/client.rb', line 37

def 
  Cloudpt::API::Object.init(self.raw., self)
end

#delta(cursor = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudpt-api/client.rb', line 60

def delta(cursor=nil)
  entries  = []
  has_more = true
  params   = cursor ? {:cursor => cursor} : {}
  while has_more
    response        = raw.delta(params)
    params[:cursor] = response['cursor']
    has_more        = response['has_more']
    entries.push     *response['entries']
  end

  files = entries.map do |entry|
    entry.last || {:is_deleted => true, :path => entry.first}
  end

  Delta.new(params[:cursor], Cloudpt::API::Object.convert(files, self))
end

#find(filename) ⇒ Object



20
21
22
23
24
# File 'lib/cloudpt-api/client.rb', line 20

def find(filename)
  data = self.raw.(:path => filename)
  data.delete('contents')
  Cloudpt::API::Object.convert(data, self)
end

#list(path = '') ⇒ Object



26
27
28
29
30
31
# File 'lib/cloudpt-api/client.rb', line 26

def list(path = '')
  response = raw.list :path => path
  results = Cloudpt::API::Object.convert(response, self)
  results['contents'] = Cloudpt::API::Object.convert(results.delete('contents') || [], self)
  results
end

#ls(path = '') ⇒ Object



33
34
35
# File 'lib/cloudpt-api/client.rb', line 33

def ls(path = '')
  Cloudpt::API::Dir.init({'path' => path}, self).ls
end

#mkdir(path) ⇒ Object



41
42
43
44
45
46
# File 'lib/cloudpt-api/client.rb', line 41

def mkdir(path)
  # Remove the characters not allowed by Cloudpt
  path = path.gsub(/(^\/)|([\\\:\?\*\<\>\"\|]+)/, '')
  response = raw.create_folder :path => "/#{path}"
  Cloudpt::API::Dir.init(response, self)
end

#search(term, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cloudpt-api/client.rb', line 48

def search(term, options = {})
  options[:path] ||= ''
  results = raw.search({ :query => term }.merge(options))
  #search results have broken paths, fix them here for now
  results.each do |result|
    if Cloudpt::API::Config.mode == 'sandbox'
      result['path'] = result['path'].sub(/^\/([^\/]+)?/, '')
    end
  end
  Cloudpt::API::Object.convert(results, self)
end