class Charging::ChargeAccount

Constants

ATTRIBUTES
DEFAULT_LIMIT
DEFAULT_PAGE
READ_ONLY_ATTRIBUTES

Public Class Methods

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

Finds all charge accounts for a domain. It requites an domain, and you should pass page and/or limit to apply on find.

Returns a Collection (Array-like) of ChargeAccount

API method: GET /charge-accounts/?page=:page&limit=:limit

API documentation: charging.financeconnect.com.br/static/docs/charges.html#get-charge-accounts-limit-limit-page-page

# File lib/charging/charge_account.rb, line 103
def self.find_all(domain, page = DEFAULT_PAGE, limit = DEFAULT_LIMIT)
  Helpers.required_arguments!(domain: domain)

  response = get_charge_accounts(domain, page, limit)

  Collection.new(domain, response)
end
find_by_uri(domain, uri) click to toggle source

Finds a charge account by uri. It requites an domain and a String.

Returns a ChargeAccount instance or raises a Http::LastResponseError if something went wrong, like unauthorized request, not found.

# File lib/charging/charge_account.rb, line 116
def self.find_by_uri(domain, uri)
  Helpers.required_arguments!(domain: domain, uri: uri)
  
  response = Http.get(uri, domain.token)
  
  raise_last_response_unless 200, response
  
  ChargeAccount.load_persisted_charge_account(MultiJson.decode(response.body), response, domain)
end
find_by_uuid(domain, uuid) click to toggle source

Finds a charge account by uuid. It requites an domain and a uuid.

Returns a ChargeAccount instance or raises a Http::LastResponseError if something went wrong, like unauthorized request, not found.

API method: GET /charge-accounts/:uuid/

API documentation: charging.financeconnect.com.br/static/docs/charges.html#get-charge-accounts-uuid

# File lib/charging/charge_account.rb, line 85
def self.find_by_uuid(domain, uuid)
  Helpers.required_arguments!(domain: domain, uuid: uuid)
  
  response = ChargeAccount.get_charge_account(domain, uuid)
  
  raise_last_response_unless 200, response
  
  load_persisted_charge_account(MultiJson.decode(response.body), response, domain)
end
load_persisted_charge_account(attributes, response, domain) click to toggle source
# File lib/charging/charge_account.rb, line 126
def self.load_persisted_charge_account(attributes, response, domain)
  validate_attributes!(attributes)
  ChargeAccount.new(attributes, domain, response)
end
new(attributes, domain, response = nil) click to toggle source
Calls superclass method Charging::Base::new
# File lib/charging/charge_account.rb, line 20
def initialize(attributes, domain, response = nil)
  super(attributes, response)
  @domain = domain
end

Private Class Methods

get_charge_account(domain, uuid) click to toggle source
# File lib/charging/charge_account.rb, line 151
def self.get_charge_account(domain, uuid)
  Http.get("/charge-accounts/#{uuid}/", domain.token)
end
get_charge_accounts(domain, page, limit) click to toggle source
# File lib/charging/charge_account.rb, line 147
def self.get_charge_accounts(domain, page, limit)
  Http.get("/charge-accounts/?page=#{page}&limit=#{limit}", domain.token)
end
post_charge_accounts(domain, attributes) click to toggle source
# File lib/charging/charge_account.rb, line 155
def self.post_charge_accounts(domain, attributes)
  Http.post('/charge-accounts/', domain.token, MultiJson.encode(attributes))
end

Public Instance Methods

create!() click to toggle source

Creates current charge account 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/charge_account.rb, line 30
def create!
  super do
    raise 'can not create without a domain' if invalid_domain?

    ChargeAccount.post_charge_accounts(domain, attributes)
  end

  reload_attributes!
end
destroy!() click to toggle source

Deletes the charge account at API

API method: DELETE /charge-accounts/:uuid/

API documentation: charging.financeconnect.com.br/static/docs/charges.html#delete-charge-accounts-uuid

Calls superclass method Charging::Base#destroy!
# File lib/charging/charge_account.rb, line 45
def destroy!
  super do
    Http.delete("/charge-accounts/#{uuid}/", domain.token, etag)
  end
end
update_attribute!(attribute, value, should_reload_attributes = true) click to toggle source

Update an attribute on charge account at API.

API method: PATCH /charge-accounts/:uuid/

API documentation: charging.financeconnect.com.br/static/docs/charges.html#patch-charge-accounts-uuid

# File lib/charging/charge_account.rb, line 56
def update_attribute!(attribute, value, should_reload_attributes = true)
  execute_and_capture_raises_at_errors(204) do
    @last_response = Http.patch("/charge-accounts/#{uuid}/", domain.token, etag, attribute => value)
  end
  
  reload_attributes! if should_reload_attributes
end
update_attributes!(attributes_values) click to toggle source

Update all attributes at charge_account. This method uses update_attribute! recurring for each attrubute. attrubutes_valies should be a hash with attribute and value to be updated.

# File lib/charging/charge_account.rb, line 68
def update_attributes!(attributes_values)
  attributes_values.each do |attribute, value|
    update_attribute! attribute, value, false
  end
ensure
  reload_attributes!
end

Private Instance Methods

invalid_domain?() click to toggle source
# File lib/charging/charge_account.rb, line 133
def invalid_domain?
  domain.nil?
end
reload_attributes!() click to toggle source
# File lib/charging/charge_account.rb, line 137
def reload_attributes!
  new_charge_account = self.class.find_by_uuid(domain, Helpers.extract_uuid(last_response.headers[:location]) || uuid)
  
  (ATTRIBUTES + COMMON_ATTRIBUTES + READ_ONLY_ATTRIBUTES).each do |attribute|
    instance_variable_set "@#{attribute}", new_charge_account.send(attribute)
  end

  self
end