class Charging::Domain
Represents a Charging
domain.
Constants
- ATTRIBUTES
- READ_ONLY_ATTRIBUTES
Public Class Methods
Finds all domains for a specified account. It requites an ServiceAccount
instance, and you should pass page
and/or limit
to apply on find.
Returns a Collection
(Array-like) of Domain
API method: GET /account/domains/
API documentation: charging.financeconnect.com.br/static/docs/accounts_and_domains.html#get-account-domains-limit-limit-page-page
# File lib/charging/domain.rb, line 57 def self.find_all(account, page = DEFAULT_PAGE, limit = DEFAULT_LIMIT) Helpers.required_arguments!('service account' => account) response = get_account_domains(account, page, limit) Collection.new(account, response) end
Finds a domain by its authentication token. It requites an token
.
Returns a Domain
instance or raises a Http::LastResponseError
if something went wrong, like unauthorized request, not found.
API method: GET /domain/
API documentation: charging.financeconnect.com.br/static/docs/accounts_and_domains.html#get-subuser-domain
# File lib/charging/domain.rb, line 92 def self.find_by_token(token) Helpers.required_arguments!('token' => token) response = get_domain(token) raise_last_response_unless 200, response load_persisted_domain(MultiJson.decode(response.body), response) end
Finds a domain by your uuid. It requites an ServiceAccount
instance and a String uuid
.
Returns a Domain
instance or raises a Http::LastResponseError
if something went wrong, like unauthorized request, not found.
API method: GET /account/domains/:uuid/
API documentation: charging.financeconnect.com.br/static/docs/accounts_and_domains.html#get-account-domains-uuid
# File lib/charging/domain.rb, line 74 def self.find_by_uuid(account, uuid) Helpers.required_arguments!('service account' => account, uuid: uuid) response = get_account_domain(account, uuid) raise_last_response_unless 200, response load_persisted_domain(MultiJson.decode(response.body), response, account) end
Initializes a domain instance
Charging::Base::new
# File lib/charging/domain.rb, line 14 def initialize(attributes, account, response = nil) super(attributes, response) @account = account end
Private Class Methods
# File lib/charging/domain.rb, line 127 def self.delete_account_domains(domain) token = domain.account.application_token Http.delete("/account/domains/#{domain.uuid}/", token, domain.etag) end
# File lib/charging/domain.rb, line 132 def self.post_account_domains(token, attributes) Http.post('/account/domains/', token, MultiJson.encode(attributes)) end
Public Instance Methods
Creates current domain at API.
API method: POST /account/domains/
API documentation: charging.financeconnect.com.br/static/docs/accounts_and_domains.html#post-account-domains
Charging::Base#create!
# File lib/charging/domain.rb, line 24 def create! super do raise 'can not create without a service account' if invalid_account? Domain.post_account_domains(account.application_token, attributes) end reload_attributes! end
Destroys current domain at API.
API method: DELETE /account/domains/:uuid/
API documentation: charging.financeconnect.com.br/static/docs/accounts_and_domains.html#delete-account-domains-uuid
Charging::Base#destroy!
# File lib/charging/domain.rb, line 39 def destroy! super do raise 'can not destroy without a service account' if invalid_account? raise 'can not destroy a not persisted domain' unless persisted? Domain.delete_account_domains(self) end end
Private Instance Methods
# File lib/charging/domain.rb, line 123 def invalid_account? account.nil? end
# File lib/charging/domain.rb, line 119 def load_errors(*error_messages) @errors = error_messages.flatten end
# File lib/charging/domain.rb, line 109 def reload_attributes! new_domain = self.class.find_by_uuid(account, Helpers.extract_uuid(last_response.headers[:location]) || uuid) (COMMON_ATTRIBUTES + READ_ONLY_ATTRIBUTES).each do |attribute| instance_variable_set "@#{attribute}", new_domain.send(attribute) end self end