Class: GandiV5::Email::Mailbox
- Inherits:
-
Object
- Object
- GandiV5::Email::Mailbox
- Includes:
- Data
- Defined in:
- lib/gandi_v5/email/mailbox.rb,
lib/gandi_v5/email/mailbox/responder.rb
Overview
A mailbox that lives within a domain.
Defined Under Namespace
Classes: Responder
Constant Summary collapse
- TYPES =
%i[standard premium free].freeze
- QUOTAS =
{ free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze
Instance Attribute Summary collapse
-
#address ⇒ String
readonly
Full email address.
-
#aliases ⇒ nil, Array<String>
readonly
Mailbox alias list.
-
#fallback_email ⇒ nil, String
readonly
Fallback email addresse.
-
#fqdn ⇒ String
readonly
Domain name.
-
#login ⇒ String
readonly
Mailbox login.
- #quota_used ⇒ Integer readonly
- #responder ⇒ nil, GandiV5::Email::Mailbox::Responder readonly
- #type ⇒ :standard, ... readonly
- #uuid ⇒ String (also: #mailbox_uuid) readonly
Class Method Summary collapse
-
.create(fqdn, login, password, aliases: [], type: :standard) ⇒ GandiV5::Email::Mailbox
Create a new mailbox.
-
.fetch(fqdn, uuid) ⇒ GandiV5::Email::Mailbox
Get information for a mailbox.
-
.list(fqdn, page: (1..), **params) ⇒ Array<GandiV5::Email::Mailbox>
List mailboxes for a domain.
Instance Method Summary collapse
-
#delete ⇒ String
Delete the mailbox and it’s contents.
-
#initialize(**members) ⇒ GandiV5::Email::Slot
constructor
Create a new GandiV5::Email::Mailbox.
-
#purge ⇒ String
Purge the contents of the mailbox.
-
#quota ⇒ Integer
Get the quota for this type of mailbox.
-
#quota_usage ⇒ Float
Get the quota usage for this mailbox.
-
#refresh ⇒ GandiV5::Email::Mailbox
Requery Gandi fo this mailbox’s information.
-
#to_s ⇒ String
Returns the string representation of the mailbox.
-
#update(**body) ⇒ String
Update the mailbox’s settings.
Methods included from Data
#from_gandi, included, #to_gandi, #to_h, #values_at
Constructor Details
#initialize(**members) ⇒ GandiV5::Email::Slot
Create a new GandiV5::Email::Mailbox
48 49 50 51 |
# File 'lib/gandi_v5/email/mailbox.rb', line 48 def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end |
Instance Attribute Details
#address ⇒ String (readonly)
Returns full email address.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#aliases ⇒ nil, Array<String> (readonly)
Returns mailbox alias list. A local-part (what comes before the “@”) of an email address. It can contain a wildcard “*” before or after at least two characters to redirect everything thats matches the local-part pattern.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#fallback_email ⇒ nil, String (readonly)
Returns fallback email addresse.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#fqdn ⇒ String (readonly)
Returns domain name.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#login ⇒ String (readonly)
Returns mailbox login.
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#quota_used ⇒ Integer (readonly)
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#responder ⇒ nil, GandiV5::Email::Mailbox::Responder (readonly)
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#type ⇒ :standard, ... (readonly)
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
#uuid ⇒ String (readonly) Also known as: mailbox_uuid
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/gandi_v5/email/mailbox.rb', line 27 class Mailbox include GandiV5::Data TYPES = %i[standard premium free].freeze QUOTAS = { free: 3 * 1024**3, standard: 3 * 1024**3, premium: 50 * 1024**3 }.freeze members :address, :login, :quota_used, :aliases, :fallback_email member :type, gandi_key: 'mailbox_type', converter: GandiV5::Data::Converter::Symbol member :uuid, gandi_key: 'id' member :fqdn, gandi_key: 'domain' member :responder, converter: GandiV5::Email::Mailbox::Responder alias mailbox_uuid uuid # Create a new GandiV5::Email::Mailbox # @param members [Hash<Symbol => Object>] # @return [GandiV5::Email::Slot] def initialize(**members) super(**members) responder.instance_exec(self) { |mb| @mailbox = mb } if responder? end # Delete the mailbox and it's contents. # If you delete a mailbox for which you have purchased a slot, # this action frees the slot so it once again becomes available # for use with a new mailbox, or for deletion. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def delete _response, data = GandiV5.delete url data['message'] end # Purge the contents of the mailbox. # @see https://api.gandi.net/docs/email#delete-v5-email-mailboxes-domain-mailbox_id-contents # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end # Requery Gandi fo this mailbox's information. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end # Update the mailbox's settings. # @see https://api.gandi.net/docs/email#patch-v5-email-mailboxes-domain-mailbox_id # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param responder [Hash, GandiV5::Mailbox::Responder, #to_gandi, #to_h] # auto responder settings. # @return [String] The confirmation message from Gandi. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end # Create a new mailbox. # Note that before you can create a mailbox, you must have a slot available. # @see https://api.gandi.net/docs/email#post-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param login [String, #to_s] the login name (and first part of email address). # @param password [String, #to_s] the password to use. # @param aliases [Array<String, #to_s>] any alternative email address to be used. # @param type [:standard, :premium] the type of mailbox slot to use. # @return [GandiV5::Email::Mailbox] The created mailbox. # @raise [GandiV5::Error] if no slots are available. # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end # Get information for a mailbox. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain-mailbox_id # @param fqdn [String, #to_s] the fully qualified domain name for the mailbox. # @param uuid [String, #to_s] unique identifier of the mailbox. # @return [GandiV5::Email::Mailbox] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end # List mailboxes for a domain. # @see https://api.gandi.net/docs/email#get-v5-email-mailboxes-domain # @param fqdn [String, #to_s] the fully qualified domain name for the mailboxes. # @param page [Integer, #each<Integer>] which page(s) of results to get. # If page is not provided keep querying until an empty list is returned. # If page responds to .each then iterate until an empty list is returned. # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page. # @param sort_by [#to_s] (optional default "login") # how to sort the results ("login", "-login"). # @param login [String] (optional) filter the list by login (pattern) # e.g. ("alice" "*lice", "alic*"). # @return [Array<GandiV5::Email::Mailbox>] # @raise [GandiV5::Error::GandiError] if Gandi returns an error. def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end # Get the quota for this type of mailbox. # @return [Integer] bytes. def quota QUOTAS[type] end # Get the quota usage for this mailbox. # @return [Float] fraction of quota used (typically between 0.0 and 1.0) def quota_usage quota_used.to_f / quota end # Returns the string representation of the mailbox. # Includes the type, address, quota usage, activeness of responder (if present) # and aliases (if present). # @return [String] def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end private # rubocop:disable Style/GuardClause def self.check_password(password) if !(9..200).cover?(password.length) fail ArgumentError, 'password must be between 9 and 200 characters' elsif password.count('A-Z') < 1 fail ArgumentError, 'password must contain at least one upper case character' elsif password.count('0-9') < 3 fail ArgumentError, 'password must contain at least three numbers' elsif password.count('^a-z^A-Z^0-9') < 1 fail ArgumentError, 'password must contain at least one special character' end end private_class_method :check_password # rubocop:enable Style/GuardClause def check_password(password) self.class.send :check_password, password end def url "#{BASE}email/mailboxes/#{CGI.escape fqdn}/#{CGI.escape uuid}" end def self.url(fqdn, uuid = nil) "#{BASE}email/mailboxes/#{CGI.escape fqdn}" + (uuid ? "/#{CGI.escape uuid}" : '') end private_class_method :url def self.crypt_password(password) # You can also send a hashed password in sha512-crypt ie: {SHA512-CRYPT}$6$xxxx$yyyy salt = SecureRandom.random_number(36**8).to_s(36) password.crypt('$6$' + salt) end private_class_method :crypt_password def crypt_password(password) self.class.send :crypt_password, password end end |
Class Method Details
.create(fqdn, login, password, aliases: [], type: :standard) ⇒ GandiV5::Email::Mailbox
Create a new mailbox. Note that before you can create a mailbox, you must have a slot available.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/gandi_v5/email/mailbox.rb', line 119 def self.create(fqdn, login, password, aliases: [], type: :standard) fail ArgumentError, "#{type.inspect} is not a valid type" unless TYPES.include?(type) if GandiV5::Email::Slot.list.none? { |slot| slot.mailbox_type == type && slot.inactive? } fail GandiV5::Error, "no available #{type} slots" end check_password password body = { mailbox_type: type, login: login, password: crypt_password(password), aliases: aliases.push }.to_json response, _data = GandiV5.post url(fqdn), body fetch fqdn, response.headers[:location].split('/').last end |
.fetch(fqdn, uuid) ⇒ GandiV5::Email::Mailbox
Get information for a mailbox.
144 145 146 147 |
# File 'lib/gandi_v5/email/mailbox.rb', line 144 def self.fetch(fqdn, uuid) _response, data = GandiV5.get url(fqdn, uuid) from_gandi data end |
.list(fqdn, page: (1..), **params) ⇒ Array<GandiV5::Email::Mailbox>
List mailboxes for a domain.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/gandi_v5/email/mailbox.rb', line 162 def self.list(fqdn, page: (1..), **params) page = [page.to_i] unless page.respond_to?(:each) params['~login'] = params.delete(:login) params.reject! { |_k, v| v.nil? } mailboxes = [] page.each do |page_number| _response, data = GandiV5.get url(fqdn), params: params.merge(page: page_number) break if data.empty? mailboxes += data.map { |mailbox| from_gandi mailbox } break if data.count < params.fetch(:per_page, 100) end mailboxes end |
Instance Method Details
#delete ⇒ String
Delete the mailbox and it’s contents. If you delete a mailbox for which you have purchased a slot, this action frees the slot so it once again becomes available for use with a new mailbox, or for deletion.
60 61 62 63 |
# File 'lib/gandi_v5/email/mailbox.rb', line 60 def delete _response, data = GandiV5.delete url data['message'] end |
#purge ⇒ String
Purge the contents of the mailbox.
69 70 71 72 |
# File 'lib/gandi_v5/email/mailbox.rb', line 69 def purge _response, data = GandiV5.delete "#{url}/contents" data['message'] end |
#quota ⇒ Integer
Get the quota for this type of mailbox.
181 182 183 |
# File 'lib/gandi_v5/email/mailbox.rb', line 181 def quota QUOTAS[type] end |
#quota_usage ⇒ Float
Get the quota usage for this mailbox.
187 188 189 |
# File 'lib/gandi_v5/email/mailbox.rb', line 187 def quota_usage quota_used.to_f / quota end |
#refresh ⇒ GandiV5::Email::Mailbox
Requery Gandi fo this mailbox’s information.
77 78 79 80 81 82 |
# File 'lib/gandi_v5/email/mailbox.rb', line 77 def refresh _response, data = GandiV5.get url from_gandi data responder.instance_exec(self) { |mb| @mailbox = mb } if responder? self end |
#to_s ⇒ String
Returns the string representation of the mailbox. Includes the type, address, quota usage, activeness of responder (if present) and aliases (if present).
195 196 197 198 199 200 |
# File 'lib/gandi_v5/email/mailbox.rb', line 195 def to_s s = "[#{type}] #{address} (#{quota_used}/#{quota} (#{(quota_usage * 100).round}%))" s += " with #{responder.active? ? 'active' : 'inactive'} responder" if responder s += " aka: #{aliases.join(', ')}" if aliases&.any? s end |
#update(**body) ⇒ String
Update the mailbox’s settings.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gandi_v5/email/mailbox.rb', line 93 def update(**body) return 'Nothing to update.' if body.empty? check_password body[:password] if body.key?(:password) body[:password] = crypt_password(body[:password]) if body.key?(:password) if (responder = body[:responder]) body[:responder] = responder.respond_to?(:to_gandi) ? responder.to_gandi : responder.to_h end _response, data = GandiV5.patch url, body.to_json refresh data['message'] end |