Class: VoxcastApi::Purge

Inherits:
Base show all
Defined in:
lib/voxcast_api/purge.rb

Direct Known Subclasses

Version01

Instance Attribute Summary

Attributes inherited from Base

#key, #secret_key

Attributes inherited from Http

#password, #url, #user

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#api_sig, #auth_key?, #devices, #execute_method, #get_basic_auth, #get_data, #get_method, #get_path_and_params, #initialize, #log_download, #log_list, #lookup_method, #populate, #process_response, #status, #test

Methods inherited from Http

#execute, #get_basic_auth, #get_http, #get_method, #get_path_and_params, #initialize, #timestamp

Constructor Details

This class inherits a constructor from VoxcastApi::Base

Class Method Details

.get(entry) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/voxcast_api/purge.rb', line 7

def self.get(entry)
  if entry.purge_url =~ /purge.php/
    VoxcastApi::Version0.new(entry)
  elsif entry.purge_url =~ /1\.0/
    VoxcastApi::Purge.new(entry)
  else
    VoxcastApi::Version01.new(entry)
  end
end

Instance Method Details

#_purge_files(files, type) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/voxcast_api/purge.rb', line 67

def _purge_files(files, type)
  return if files.size == 0
  if type == :files
    method = :purge_file
  elsif type == :dirs
    method = :purge_dir
  else
    return {:status => false, :detail => "invalid type #{type}"}
  end
  params = { 'device_id' => device_id, 
    'paths' => get_urls(files)
  }
  ret = execute_method(method, params)
  if ret[:status]
    resp = ret[:detail]
    tran_id =  resp['transaction']['id']
    @logger.info("purged #{files.size} #{type}, tran id #{tran_id}")
    status = {:status => true, :detail => tran_id}
  else
    status = {:status => false, :detail => ret[:detail]}
  end
  status
end

#get_urls(files) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/voxcast_api/purge.rb', line 91

def get_urls(files)
  paths = Set.new
  files.each do |f| 
    f = (f[0] != ?\/ ? '/'+f : f)
    # remove trailling slashes
    f = f.chomp('/')
    paths << f
  end
  paths.to_a.join("\n")
end

#limitObject



17
18
19
# File 'lib/voxcast_api/purge.rb', line 17

def limit
  250
end

#purge(files) ⇒ Object



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/voxcast_api/purge.rb', line 21

def purge(files)
  # this is a new purge which will use the new api calls to voxel.
  # this is documented here 
  # http://api.voxel.net/docs/
  purge_all = files.detect {|f| f == '/'}
  if purge_all
    params = {'device_id' => device_id}
    ret = execute_method(:purge_site, params)
    if ret[:status]
      resp = ret[:detail]
      return resp['transaction']['id']
    else
      return ret
    end
  else
    # take the files and break them up into directories and files
    files_list, dirs_list = filter_files(files, limit = self.limit)
    retError = {:status => true, :detail => []}
    trans_ids = []
    files_list.each do |f|
      ret = _purge_files(f, :files)
      if ret
        if ret[:status]
          trans_ids << ret[:detail]
        else
          retError[:status] = false
          retError[:detail] << ret[:detail]
        end
      end
    end
    dirs_list.each do |d|
      ret = _purge_files(d, :dirs)
      if ret
        if ret[:status]
          trans_ids << ret[:detail]
        else
          retError[:status] = false
          retError[:detail] << ret[:detail]
        end
      end
    end
  end
  return retError if !retError[:status]
  trans_ids
end