File: get_records.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (72 lines) | stat: -rw-r--r-- 2,664 bytes parent folder | download | duplicates (5)
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
module Fog
  module AWS
    class Kinesis
      class Real
        # Gets data records from a shard.
        #
        # ==== Options
        # * Limit<~Number>: The maximum number of records to return.
        # * ShardIterator<~String>: The position in the shard from which you want to start sequentially reading data records.
        # ==== Returns
        # * response<~Excon::Response>:
        #
        # ==== See Also
        # https://docs.aws.amazon.com/kinesis/latest/APIReference/API_GetRecords.html
        #
        def get_records(options={})
          body = {
            "Limit" => options.delete("Limit"),
            "ShardIterator" => options.delete("ShardIterator")
          }.reject{ |_,v| v.nil? }

          response = request({
                               'X-Amz-Target' => "Kinesis_#{@version}.GetRecords",
                               :body          => body,
                             }.merge(options))
          response.body = Fog::JSON.decode(response.body) unless response.body.nil?
          response
        end
      end

      class Mock
        def get_records(options={})
          shard_iterator = Fog::JSON.decode(options.delete("ShardIterator"))
          limit = options.delete("Limit") || -1
          stream_name = shard_iterator["StreamName"]
          shard_id = shard_iterator["ShardId"]
          starting_sequence_number = (shard_iterator["StartingSequenceNumber"] || 1).to_i

          unless stream = data[:kinesis_streams].detect{ |s| s["StreamName"] == stream_name }
            raise Fog::AWS::Kinesis::ResourceNotFound.new("Stream #{stream_name} under account #{@account_id} not found.")
          end

          unless shard = stream["Shards"].detect{ |shard| shard["ShardId"] == shard_id }
            raise Fog::AWS::Kinesis::ResourceNotFound.new("Could not find shard #{shard_id} in stream #{stream_name} under account #{@account_id}.")
          end

          records = []
          shard["Records"].each do |record|
            next if record["SequenceNumber"].to_i < starting_sequence_number
            records << record
            break if records.size == limit
          end

          shard_iterator["StartingSequenceNumber"] = if records.empty?
            starting_sequence_number.to_s
          else
            (records.last["SequenceNumber"].to_i + 1).to_s
          end

          response = Excon::Response.new
          response.status = 200
          response.body = {
            "MillisBehindLatest"=> 0,
            "NextShardIterator"=> Fog::JSON.encode(shard_iterator),
            "Records"=> records
          }
          response
        end
      end
    end
  end
end