Class: Plivo::Resources::SubaccountInterface

Inherits:
Base::ResourceInterface show all
Defined in:
lib/plivo/resources/accounts.rb

Constant Summary

Constants included from Utils

Utils::TYPE_WHITELIST

Instance Method Summary collapse

Methods included from Utils

GetSortedQueryParamString?, compute_signatureV3?, expected_type?, expected_value?, generate_url?, getMapFromQueryString?, is_one_among_string_url?, multi_valid_param?, raise_invalid_request, valid_account?, valid_date_format?, valid_mainaccount?, valid_multiple_destination_integers?, valid_multiple_destination_nos?, valid_param?, valid_range?, valid_signature?, valid_signatureV3?, valid_subaccount?, valid_url?

Constructor Details

#initialize(client, resource_list_json = nil) ⇒ SubaccountInterface

Returns a new instance of SubaccountInterface.



54
55
56
57
58
59
# File 'lib/plivo/resources/accounts.rb', line 54

def initialize(client, resource_list_json = nil)
  @_name = 'Subaccount'
  @_resource_type = Subaccount
  @_identifier_string = 'auth_id'
  super
end

Instance Method Details

#create(name, enabled = false) ⇒ Object

Parameters:

  • name (String)
  • enabled (Boolean) (defaults to: false)


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
# File 'lib/plivo/resources/accounts.rb', line 53

class SubaccountInterface < Base::ResourceInterface
  def initialize(client, resource_list_json = nil)
    @_name = 'Subaccount'
    @_resource_type = Subaccount
    @_identifier_string = 'auth_id'
    super
  end

  # @param [String] subaccount_id
  def get(subaccount_id)
    valid_subaccount?(subaccount_id, true)
    perform_get(subaccount_id)
  end

  # @param [String] name
  # @param [Boolean] enabled
  def create(name, enabled = false)
    valid_param?(:name, name, [String, Symbol], true)
    valid_param?(:enabled, enabled, [TrueClass, FalseClass],
                 true, [true, false])

    params = {
      name: name,
      enabled: enabled
    }

    perform_create(params)
  end

  # @param [Array] options
  def list(options = nil)
    return perform_list if options.nil?

    params = {}

    %i[offset limit].each do |param|
      if options.key?(param) && valid_param?(param, options[param],
                                             [Integer, Integer], true)
        params[param] = options[param]
      end
    end

    if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
      raise_invalid_request('The maximum number of results that can be '\
      "fetched is 20. limit can't be more than 20 or less than 1")
    end

    raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0

    perform_list(params)
  end

  def each
    offset = 0
    loop do
      subaccount_list = list(offset: offset)
      subaccount_list[:objects].each { |subaccount| yield subaccount }
      offset += 20
      return unless subaccount_list.length == 20
    end
  end

  def update(subaccount_id, name, enabled = false)
    Subaccount.new(@_client, resource_id: subaccount_id).update(name, enabled)
  end

  def delete(subaccount_id, cascade = false)
    valid_subaccount?(subaccount_id, true)
    Subaccount.new(@_client, resource_id: subaccount_id).delete(cascade)
  end
end

#delete(subaccount_id, cascade = false) ⇒ Object



119
120
121
122
# File 'lib/plivo/resources/accounts.rb', line 119

def delete(subaccount_id, cascade = false)
  valid_subaccount?(subaccount_id, true)
  Subaccount.new(@_client, resource_id: subaccount_id).delete(cascade)
end

#eachObject



105
106
107
108
109
110
111
112
113
# File 'lib/plivo/resources/accounts.rb', line 105

def each
  offset = 0
  loop do
    subaccount_list = list(offset: offset)
    subaccount_list[:objects].each { |subaccount| yield subaccount }
    offset += 20
    return unless subaccount_list.length == 20
  end
end

#get(subaccount_id) ⇒ Object

Parameters:

  • subaccount_id (String)


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
# File 'lib/plivo/resources/accounts.rb', line 53

class SubaccountInterface < Base::ResourceInterface
  def initialize(client, resource_list_json = nil)
    @_name = 'Subaccount'
    @_resource_type = Subaccount
    @_identifier_string = 'auth_id'
    super
  end

  # @param [String] subaccount_id
  def get(subaccount_id)
    valid_subaccount?(subaccount_id, true)
    perform_get(subaccount_id)
  end

  # @param [String] name
  # @param [Boolean] enabled
  def create(name, enabled = false)
    valid_param?(:name, name, [String, Symbol], true)
    valid_param?(:enabled, enabled, [TrueClass, FalseClass],
                 true, [true, false])

    params = {
      name: name,
      enabled: enabled
    }

    perform_create(params)
  end

  # @param [Array] options
  def list(options = nil)
    return perform_list if options.nil?

    params = {}

    %i[offset limit].each do |param|
      if options.key?(param) && valid_param?(param, options[param],
                                             [Integer, Integer], true)
        params[param] = options[param]
      end
    end

    if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
      raise_invalid_request('The maximum number of results that can be '\
      "fetched is 20. limit can't be more than 20 or less than 1")
    end

    raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0

    perform_list(params)
  end

  def each
    offset = 0
    loop do
      subaccount_list = list(offset: offset)
      subaccount_list[:objects].each { |subaccount| yield subaccount }
      offset += 20
      return unless subaccount_list.length == 20
    end
  end

  def update(subaccount_id, name, enabled = false)
    Subaccount.new(@_client, resource_id: subaccount_id).update(name, enabled)
  end

  def delete(subaccount_id, cascade = false)
    valid_subaccount?(subaccount_id, true)
    Subaccount.new(@_client, resource_id: subaccount_id).delete(cascade)
  end
end

#list(options = nil) ⇒ Object

Parameters:

  • options (Array) (defaults to: nil)


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
# File 'lib/plivo/resources/accounts.rb', line 53

class SubaccountInterface < Base::ResourceInterface
  def initialize(client, resource_list_json = nil)
    @_name = 'Subaccount'
    @_resource_type = Subaccount
    @_identifier_string = 'auth_id'
    super
  end

  # @param [String] subaccount_id
  def get(subaccount_id)
    valid_subaccount?(subaccount_id, true)
    perform_get(subaccount_id)
  end

  # @param [String] name
  # @param [Boolean] enabled
  def create(name, enabled = false)
    valid_param?(:name, name, [String, Symbol], true)
    valid_param?(:enabled, enabled, [TrueClass, FalseClass],
                 true, [true, false])

    params = {
      name: name,
      enabled: enabled
    }

    perform_create(params)
  end

  # @param [Array] options
  def list(options = nil)
    return perform_list if options.nil?

    params = {}

    %i[offset limit].each do |param|
      if options.key?(param) && valid_param?(param, options[param],
                                             [Integer, Integer], true)
        params[param] = options[param]
      end
    end

    if options.key?(:limit) && (options[:limit] > 20 || options[:limit] <= 0)
      raise_invalid_request('The maximum number of results that can be '\
      "fetched is 20. limit can't be more than 20 or less than 1")
    end

    raise_invalid_request("Offset can't be negative") if options.key?(:offset) && options[:offset] < 0

    perform_list(params)
  end

  def each
    offset = 0
    loop do
      subaccount_list = list(offset: offset)
      subaccount_list[:objects].each { |subaccount| yield subaccount }
      offset += 20
      return unless subaccount_list.length == 20
    end
  end

  def update(subaccount_id, name, enabled = false)
    Subaccount.new(@_client, resource_id: subaccount_id).update(name, enabled)
  end

  def delete(subaccount_id, cascade = false)
    valid_subaccount?(subaccount_id, true)
    Subaccount.new(@_client, resource_id: subaccount_id).delete(cascade)
  end
end

#update(subaccount_id, name, enabled = false) ⇒ Object



115
116
117
# File 'lib/plivo/resources/accounts.rb', line 115

def update(subaccount_id, name, enabled = false)
  Subaccount.new(@_client, resource_id: subaccount_id).update(name, enabled)
end