|
| 1 | +module Phaxio |
| 2 | + module Resources |
| 3 | + # Provides functionality for managing ATAs. |
| 4 | + class Ata < Resource |
| 5 | + ATAS_PATH = 'atas'.freeze |
| 6 | + private_constant :ATAS_PATH |
| 7 | + |
| 8 | + # @return [Integer] the ID of the ATA. |
| 9 | + # @!attribute id |
| 10 | + |
| 11 | + # @return [String] the name of the ATA. |
| 12 | + # @!attribute name |
| 13 | + |
| 14 | + # @return [String] the description of the ATA. |
| 15 | + # @!attribute description |
| 16 | + |
| 17 | + # @return [String] The user phone number associated with the ATA. |
| 18 | + # @!attribute user_phone_number |
| 19 | + |
| 20 | + # @return [String] The domain for the ATA. |
| 21 | + # @!attribute domain |
| 22 | + |
| 23 | + # @return [String] The username for the ATA. |
| 24 | + # @!attribute uername |
| 25 | + |
| 26 | + # @return [String] The password for the ATA. |
| 27 | + # @!attribute password |
| 28 | + |
| 29 | + has_normal_attributes %w[ |
| 30 | + id name description user_phone_number domain username password |
| 31 | + ] |
| 32 | + |
| 33 | + # A reference to an ATA. This is returned by certain actions which don't |
| 34 | + # return the full ATA. |
| 35 | + class Reference |
| 36 | + # @return [Integer] |
| 37 | + # The ID of the referenced ATA. |
| 38 | + attr_accessor :id |
| 39 | + |
| 40 | + def to_i |
| 41 | + id |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + def initialize id |
| 47 | + self.id = id |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + # A reference to a phone number, returned by ATA phone number management |
| 52 | + # actions. |
| 53 | + class PhoneNumberReference |
| 54 | + # @return [String] |
| 55 | + # The phone number. |
| 56 | + attr_accessor :phone_number |
| 57 | + |
| 58 | + def to_s |
| 59 | + phone_number |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def initialize phone_number |
| 65 | + self.phone_number = phone_number |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + class << self |
| 70 | + # @macro paging |
| 71 | + # List ATAs |
| 72 | + # @param params[Hash] |
| 73 | + # Any parameters to send to Phaxio. |
| 74 | + # @return [Phaxio::Resource::Collection<Phaxio::Resources::Ata>] |
| 75 | + # The collection of ATAs matching your request. |
| 76 | + # @raise [Phaxio::Error::PhaxioError] |
| 77 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/list |
| 78 | + def list params = {} |
| 79 | + response = Client.request :get, atas_endpoint, params |
| 80 | + response_collection response |
| 81 | + end |
| 82 | + |
| 83 | + # Create an ATA |
| 84 | + # @param params [Hash] |
| 85 | + # Any parameters to send to Phaxio. |
| 86 | + # - *name* [String] - A name used to identify the ATA. |
| 87 | + # - *description* [String] - A longer description of the ATA. |
| 88 | + # - *domain* [String] - A domain for the ATA. |
| 89 | + # @return [Phaxio::Resources::Ata] |
| 90 | + # The created ATA, including the generated username and password. |
| 91 | + # @raise [Phaxio::Error::PhaxioError] |
| 92 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/create |
| 93 | + def create params = {} |
| 94 | + response = Client.request :post, atas_endpoint, params |
| 95 | + response_record response |
| 96 | + end |
| 97 | + |
| 98 | + # Get an ATA |
| 99 | + # @param id [Integer] |
| 100 | + # The ID of the ATA to retrieve information about. |
| 101 | + # @param params [Hash] |
| 102 | + # Any parameters to send to Phaxio. This action takes no unique parameters. |
| 103 | + # @return [Phaxio::Resources::Ata] |
| 104 | + # The requested ATA. |
| 105 | + # @raise [Phaxio::Error::PhaxioError] |
| 106 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/get |
| 107 | + def get id, params = {} |
| 108 | + response = Client.request :get, ata_endpoint(id.to_i), params |
| 109 | + response_record response |
| 110 | + end |
| 111 | + alias :retrieve :get |
| 112 | + alias :find :get |
| 113 | + |
| 114 | + # Update an ATA |
| 115 | + # @param id [Integer] |
| 116 | + # The ID of the ATA to update. |
| 117 | + # @param params [Hash] |
| 118 | + # Any parameters to send to Phaxio. |
| 119 | + # - *name* [String] - A name used to identify the ATA. |
| 120 | + # - *description* [String] - A longer description of the ATA. |
| 121 | + # - *domain* [String] - A domain for the ATA. |
| 122 | + # @return [Phaxio::Resources::Ata] |
| 123 | + # The updated ATA. |
| 124 | + # @raise [Phaxio::Error::PhaxioError] |
| 125 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/update |
| 126 | + def update id, params = {} |
| 127 | + response = Client.request :patch, ata_endpoint(id.to_i), params |
| 128 | + response_record response |
| 129 | + end |
| 130 | + |
| 131 | + # Regenerate credentials for an ATA |
| 132 | + # @param id [Integer] |
| 133 | + # The ID of the ATA for which credentials should be regenerated. |
| 134 | + # @param params [Hash] |
| 135 | + # Any parameters to send to Phaxio. This action takes no unique parameters. |
| 136 | + # @return [Phaxio::Resources::Ata] |
| 137 | + # The ATA, including the new username and password. |
| 138 | + # @raise Phaxio::Error::PhaxioError |
| 139 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/regenerate_credentials |
| 140 | + def regenerate_credentials id, params = {} |
| 141 | + response = Client.request :patch, regenerate_credentials_endpoint(id.to_i), params |
| 142 | + response_record response |
| 143 | + end |
| 144 | + |
| 145 | + # Delete an ATA |
| 146 | + # @param id [Integer] |
| 147 | + # The Id of the ATA to delete. |
| 148 | + # @param params [Hash] |
| 149 | + # Any parameters to send to Phaxio. This action takes no unique parameters. |
| 150 | + # @return [Phaxio::Resources::Ata::Reference] |
| 151 | + # A reference to the deleted ATA. |
| 152 | + # @raise [Phaxio::Error::PhaxioError] |
| 153 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/delete |
| 154 | + def delete id, params = {} |
| 155 | + response = Client.request :delete, ata_endpoint(id.to_i), params |
| 156 | + response_reference response |
| 157 | + end |
| 158 | + |
| 159 | + # Add a phone number |
| 160 | + # @param id [Integer] |
| 161 | + # The ID of the ATA to which you want to add a number. |
| 162 | + # @param phone_number [String] |
| 163 | + # The phone number to add to the ATA. |
| 164 | + # @param params [Hash] |
| 165 | + # Any parameters to send to Phaxio. This action takes no unique parameters. |
| 166 | + # @return [Phaxio::Resources::Ata::PhoneNumberReference] |
| 167 | + # A reference to the added phone number. |
| 168 | + # @raise [Phaxio::Error::PhaxioError] |
| 169 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/add_phone_number |
| 170 | + def add_phone_number id, phone_number, params = {} |
| 171 | + response = Client.request :post, phone_number_endpoint(id, phone_number), params |
| 172 | + response_phone_number_reference response |
| 173 | + end |
| 174 | + |
| 175 | + # Remove a phone number |
| 176 | + # @param id [Integer] |
| 177 | + # The ID of the ATA from which you want to remove the phone number. |
| 178 | + # @param phone_number [String] |
| 179 | + # The phone number you want to remove. |
| 180 | + # @param params [Hash] |
| 181 | + # Any parameters to send to Phaxio. This action takes no unique parameters. |
| 182 | + # @return [Phaxio::Resources::Ata::PhoneNumberReference] |
| 183 | + # A reference to the removed phone number. |
| 184 | + # @raise [Phaxio::Error::PhaxioError] |
| 185 | + # @see https://www.phaxio.com/docs/api/v2.1/atas/remove_phone_number |
| 186 | + def remove_phone_number id, phone_number, params = {} |
| 187 | + response = Client.request :delete, phone_number_endpoint(id, phone_number), params |
| 188 | + response_phone_number_reference response |
| 189 | + end |
| 190 | + |
| 191 | + private |
| 192 | + |
| 193 | + def response_reference response |
| 194 | + Reference.new Integer(response['id']) |
| 195 | + end |
| 196 | + |
| 197 | + def response_phone_number_reference response |
| 198 | + PhoneNumberReference.new(response['phone_number']) |
| 199 | + end |
| 200 | + |
| 201 | + def atas_endpoint |
| 202 | + ATAS_PATH |
| 203 | + end |
| 204 | + |
| 205 | + def ata_endpoint id |
| 206 | + "#{atas_endpoint}/#{id}" |
| 207 | + end |
| 208 | + |
| 209 | + def regenerate_credentials_endpoint id |
| 210 | + "#{ata_endpoint(id)}/regenerate_credentials" |
| 211 | + end |
| 212 | + |
| 213 | + def phone_number_endpoint id, phone_number |
| 214 | + "#{ata_endpoint(id)}/phone_numbers/#{phone_number}" |
| 215 | + end |
| 216 | + end |
| 217 | + end |
| 218 | + end |
| 219 | +end |
0 commit comments