Class: FlyingV

Inherits:
Object
  • Object
show all
Defined in:
lib/flyingv.rb

Class Method Summary collapse

Class Method Details

.get(key) ⇒ Object



5
6
7
8
9
10
# File 'lib/flyingv.rb', line 5

def self.get(key)
  res = Net::HTTP.get(URI.parse("http://api.openkeyval.org/#{key}"))
  JSON.parse(res)
rescue JSON::ParserError
  res
end

.get_file(key, target_path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/flyingv.rb', line 56

def self.get_file(key, target_path)
  master = self.get(key)
  f = File.open(target_path, "w")
  master["chunk_keys"].each do |chunk_key|
    chunk = self.get(chunk_key)
    puts "master key sanity check PASSED! -- getting #{chunk_key}" if(chunk["master_key"] == key)
    f.write(chunk["content"])
  end
  f.close
  puts "complete"
end

.post(key, value) ⇒ Object



12
13
14
15
# File 'lib/flyingv.rb', line 12

def self.post(key, value)
  value = value.to_json if(value.is_a?(Hash) or value.is_a?(Array))
  Net::HTTP.post_form(URI.parse("http://api.openkeyval.org/#{key}"), {'data' => value})
end

.post_file(key, path) ⇒ Object



17
18
19
20
21
22
# File 'lib/flyingv.rb', line 17

def self.post_file(key, path)
  size = File.size(path)
  content = File.open(path).read
  original_name = File.basename(path)
  self.post(key, {"content" => content, "original_name" => original_name})
end

.put_file(key, path) ⇒ Object



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
# File 'lib/flyingv.rb', line 24

def self.put_file(key, path)
  size = File.size(path)
  chunks = (size / 2048.0).ceil
  return self.post_file(key, path) if(chunks == 1)
  original_name = File.basename(path)
  content = File.open(path).read
  next_chunk_key = nil
  chunk_keys = []
  sanity_check = ""
  chunks.downto(1) do |i|
    k = "#{key}_chunk_#{i}"
    chunk_keys << k
    if(i == chunks)
      chunk_content = content[((i-1)*2048)..-1]
    else
      chunk_content = content[((i-1)*2048)..(i*2048-1)]
    end
    sanity_check += chunk_content
    puts "storing key: #{k}, content size #{chunk_content.size}"
    self.post(k, {
      "content" => chunk_content, 
      "master_key" => key, 
      "chunk" => i, 
      "next" => next_chunk_key})
    next_chunk_key = k
  end
  puts "sanity check size = #{sanity_check.size}, content size = #{content.size}"
  self.post(key, {
    "original_name" => original_name,
    "chunk_keys" => chunk_keys.reverse})  
end