File: iterator.rb

package info (click to toggle)
ruby-aliyun-sdk 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: ruby: 7,909; ansic: 204; makefile: 4
file content (92 lines) | stat: -rw-r--r-- 2,338 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- encoding: utf-8 -*-

module Aliyun
  module OSS
    ##
    # Iterator structs that wrap the multiple communications with the
    # server to provide an iterable result.
    #
    module Iterator

      ##
      # Iterator base that stores fetched results and fetch more if needed.
      #
      class Base
        def initialize(protocol, opts = {})
          @protocol = protocol
          @results, @more = [], opts
        end

        def next
          loop do
            # Communicate with the server to get more results
            fetch_more if @results.empty?

            # Return the first result
            r = @results.shift
            break unless r

            yield r
          end
        end

        def to_enum
          self.enum_for(:next)
        end

        private
        def fetch_more
          return if @more[:truncated] == false
          fetch(@more)
        end
      end # Base

      ##
      # Buckets iterator
      #
      class Buckets < Base
        def fetch(more)
          @results, cont = @protocol.list_buckets(more)
          @more[:marker] = cont[:next_marker]
          @more[:truncated] = cont[:truncated] || false
        end
      end # Buckets

      ##
      # Objects iterator
      #
      class Objects < Base
        def initialize(protocol, bucket_name, opts = {})
          super(protocol, opts)
          @bucket = bucket_name
        end

        def fetch(more)
          @results, cont = @protocol.list_objects(@bucket, more)
          @results = cont[:common_prefixes] + @results if cont[:common_prefixes]
          @more[:marker] = cont[:next_marker]
          @more[:truncated] = cont[:truncated] || false
        end
      end # Objects

      ##
      # Uploads iterator
      #
      class Uploads < Base
        def initialize(protocol, bucket_name, opts = {})
          super(protocol, opts)
          @bucket = bucket_name
        end

        def fetch(more)
          @results, cont = @protocol.list_multipart_uploads(@bucket, more)
          @results = cont[:common_prefixes] + @results if cont[:common_prefixes]
          @more[:id_marker] = cont[:next_id_marker]
          @more[:key_marker] = cont[:next_key_marker]
          @more[:truncated] = cont[:truncated] || false
        end
      end # Objects

    end # Iterator
  end # OSS
end # Aliyun