Class: Upyun::Rest

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/upyun/rest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

included, #md5

Constructor Details

#initialize(bucket, operator, password, options = {timeout: 60}, endpoint = Upyun::ED_AUTO) ⇒ Rest

Returns a new instance of Rest.



11
12
13
14
15
16
17
# File 'lib/upyun/rest.rb', line 11

def initialize(bucket, operator, password, options={timeout: 60}, endpoint=Upyun::ED_AUTO)
  @bucket = bucket
  @operator = operator
  @password = md5(password)
  @options = options
  @endpoint = endpoint
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/upyun/rest.rb', line 9

def options
  @options
end

Instance Method Details

#delete(path) ⇒ Object



65
66
67
# File 'lib/upyun/rest.rb', line 65

def delete(path)
  request(:delete, path)
end

#get(path, savepath = nil, headers = {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/upyun/rest.rb', line 42

def get(path, savepath=nil, headers={})
  res = request(:get, path, headers: headers)
  return res if res.is_a?(Hash) || !savepath

  dir = File.dirname(savepath)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)
  File.write(savepath, res)
end

#getinfo(path) ⇒ Object Also known as: head



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/upyun/rest.rb', line 51

def getinfo(path)
  request(:head, path) do |hds|
    #  File info:
    #  x-upyun-file-type
    #  x-upyun-file-size
    #  x-upyun-file-date
    hds.select { |k| k.to_s.match(/^x_upyun_file/i) }.reduce({}) do |memo, (k, v)|
      memo.merge!({k[8..-1].to_sym => /^\d+$/.match(v) ? v.to_i : v})
    end
  end
end

#getlist(path = '/') ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/upyun/rest.rb', line 73

def getlist(path='/')
  res = request(:get, path)
  return res if res.is_a?(Hash)

  res.split("\n").map do |f|
    attrs = f.split("\t")
    {
      name: attrs[0],
      type: attrs[1] == 'N' ? :file : :folder,
      length: attrs[2].to_i,
      last_modified: attrs[3].to_i
    }
  end
end

#mkdir(path) ⇒ Object



69
70
71
# File 'lib/upyun/rest.rb', line 69

def mkdir(path)
  request(:post, path, {headers: {folder: true, mkdir: true}})
end

#put(path, file, headers = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/upyun/rest.rb', line 19

def put(path, file, headers={})
  headers = headers.merge({'mkdir' => true}) unless headers.key?('mkdir')
  body = file.respond_to?(:read) ? IO.binread(file) : file
  options = {body: body, length: size(file), headers: headers}

  # If the type of current bucket is Picture,
  # put an image maybe return a set of headers
  # represent the image's metadata
  # x-upyun-width
  # x-upyun-height
  # x-upyun-frames
  # x-upyun-file-type
  res = request(:put, path, options) do |hds|
    hds.select { |k| k.to_s.match(/^x_upyun_/i) }.reduce({}) do |memo, (k, v)|
      memo.merge!({k[8..-1].to_sym => /^\d+$/.match(v) ? v.to_i : v})
    end
  end

  res == {} ? true : res
ensure
  file.close if file.respond_to?(:close)
end

#usageObject



88
89
90
91
92
93
94
95
# File 'lib/upyun/rest.rb', line 88

def usage
  res = request(:get, '/', {query: 'usage'})
  return res if res.is_a?(Hash)

  # RestClient has a bug, body.to_i returns the code instead of body,
  # see more on https://github.com/rest-client/rest-client/pull/103
  res.dup.to_i
end