File: subscription.rb

package info (click to toggle)
ruby-aws-sdk 1.52.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,164 kB
  • ctags: 4,821
  • sloc: ruby: 28,085; makefile: 7
file content (165 lines) | stat: -rw-r--r-- 4,809 bytes parent folder | download | duplicates (4)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class SNS

    # Represents a subscription of a single endpoint to an SNS topic.
    # To create a subscription, use the {Topic#subscribe} method.
    # Depending on the endpoint type, you may also need to use
    # {Topic#confirm_subscription}.
    class Subscription

      include Core::Model
      include HasDeliveryPolicy

      # @api private
      def initialize(arn, opts = {})
        @arn = arn
        @topic_arn = opts[:topic_arn]
        @endpoint = opts[:endpoint]
        @protocol = opts[:protocol]
        @owner_id = opts[:owner_id]
        super
      end

      # @return [String] The ARN of the subscription.
      attr_reader :arn

      # @return [String] The endpoint.  This can be an HTTP or HTTPS
      # URL, an e-mail address, or a queue ARN.
      attr_reader :endpoint

      # @return [String] The protocol.  Possible values:
      #
      #  * `:http`
      #  * `:https`
      #  * `:email`
      #  * `:email_json`
      #  * `:sqs`
      attr_reader :protocol

      # @return [String] The AWS account ID of the subscription owner.
      def owner_id
        @owner_id ||= get_attributes['Owner']
      end

      # @return [String]
      def topic_arn
        @topic_arn ||= get_attributes['TopicArn']
      end

      # @return [Topic]
      def topic
        Topic.new(topic_arn, :config => config)
      end

      # Deletes this subscription.
      # @return [nil]
      def unsubscribe
        client.unsubscribe(:subscription_arn => arn)
        nil
      end

      # @return [Boolean] Returns true if the subscription confirmation
      #   request was authenticated.
      def confirmation_authenticated?

        return true if @authenticated

        if authenticated = get_attributes['ConfirmationWasAuthenticated']
          @authenticated = true
        else
          false
        end

      end

      # You can get the parsed JSON hash from {#delivery_policy}.
      # @return [nil,String] Returns the delivery policy JSON string.
      def delivery_policy_json
        get_attributes['DeliveryPolicy']
      end

      # You can get the parsed JSON hash from {#effective_delivery_policy}.
      # @return [nil,String] Returns the effective delivery policy JSON string.
      def effective_delivery_policy_json
        get_attributes['EffectiveDeliveryPolicy']
      end

      # @return [Boolean] Returns true if the subscriptions has raw message delivery enabled.
      def raw_message_delivery
        raw_value = get_attributes['RawMessageDelivery']
        raw_value.downcase == 'true'
      end

      # @param [Boolean] raw_delivery Whether to enable or disable raw message delivery.
      def raw_message_delivery= raw_delivery
        value = if raw_delivery
          'true'
        else
          'false'
        end
        update_subscription_attribute('RawMessageDelivery', value)
      end

      # @note This method requests the entire list of subscriptions
      #   for the topic (if known) or the account (if the topic is not
      #   known).  It can be expensive if the number of subscriptions
      #   is high.
      #
      # @return [Boolean] Returns true if the subscription exists.
      def exists?
        begin
          get_attributes
          true
        rescue Errors::NotFound, Errors::InvalidParameter
          false
        end
      end

      # @api private
      def inspect
        "<#{self.class} arn:#{arn}>"
      end

      # @return [Boolean] Returns true if the subscriptions have the same
      #   resource ARN.
      def eql? other
        other.kind_of?(Subscription) and other.arn == arn
      end
      alias_method :==, :eql?

      protected
      def update_subscription_attribute name, value
        client_opts = {}
        client_opts[:subscription_arn] = arn
        client_opts[:attribute_name] = name
        client_opts[:attribute_value] = value
        client.set_subscription_attributes(client_opts)
      end

      protected
      def update_delivery_policy policy_json
        update_subscription_attribute('DeliveryPolicy', policy_json)
      end

      protected
      def get_attributes
        client.get_subscription_attributes(:subscription_arn => arn).attributes
      end

    end

  end
end