class Charging::Domain

Represents a Charging domain.

Constants

ATTRIBUTES
READ_ONLY_ATTRIBUTES

Public Class Methods

find_all(account, page = DEFAULT_PAGE, limit = DEFAULT_LIMIT) click to toggle source

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
find_by_token(token) click to toggle source

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
find_by_uuid(account, uuid) click to toggle source

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
new(attributes, account, response = nil) click to toggle source

Initializes a domain instance

Calls superclass method 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

delete_account_domains(domain) click to toggle source
# 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
post_account_domains(token, attributes) click to toggle source
# 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

create!() click to toggle source

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

Calls superclass method 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
destroy!() click to toggle source

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

Calls superclass method 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

invalid_account?() click to toggle source
# File lib/charging/domain.rb, line 123
def invalid_account?
  account.nil?
end
load_errors(*error_messages) click to toggle source
# File lib/charging/domain.rb, line 119
def load_errors(*error_messages)
  @errors = error_messages.flatten
end
reload_attributes!() click to toggle source
# 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