Class: Buxfer::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/buxfer/base.rb

Overview

The Buxfer::Base class provides the API methods. It can be instansiated directly or a default instance is used with the Buxfer module.

Default usage:

Buxfer.auth('username', 'password')
Buxfer.accounts

Instance usage:

client = Buxfer::Base.new('username', 'password')
client.accounts

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Base

Create a new Buxfer::Base object with a username and password.



56
57
58
59
# File 'lib/buxfer/base.rb', line 56

def initialize(username, password)
  @username = username
  @password = password
end

Instance Method Details

#accountsObject

Returns an array of Buxfer::Account objects

www.buxfer.com/help.php?topic=API#accounts



154
155
156
# File 'lib/buxfer/base.rb', line 154

def accounts
  get_collection 'accounts', :class => Buxfer::Account
end

#add_transaction(amount, description, account = nil, status = nil, tags = []) ⇒ Object

Add a transaction to Buxfer. The amount and description must be specified.

An account name or Buxfer::Account object can be specified if the transaction is to be associated with a particular account.

An array of tag names can be specified.

Examples:

Buxfer.add_transaction(-10.0, 'Internet bill', 'Current account', nil, %w(bill payment))
Buxfer.add_transaction(1000, 'Salary', 'Current account', 'pending', %w(salary))

See: www.buxfer.com/help.php?topic=API#add_transaction



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/buxfer/base.rb', line 75

def add_transaction(amount, description,  = nil, status = nil, tags = [])
  amount = (amount < 0 ? amount.to_s : '+' + amount.to_s)
  tags = tags.join(',')
  attrs = {}
  text = [description, amount]
  .respond_to?(:name) ? .name : .to_s

  {:acct => , :status => status, :tags => tags}.each do |k, v|
    text << '%s:%s' % [k, v] unless v.blank?
  end

  self.class.post('/add_transaction.xml', auth_query({:text => text, :format => 'sms'}, :body))
end

#budgetsObject



168
# File 'lib/buxfer/base.rb', line 168

def budgets;   get_collection('budgets');   end

#contactsObject



174
# File 'lib/buxfer/base.rb', line 174

def contacts;  get_collection('contacts');  end

#groupsObject



172
# File 'lib/buxfer/base.rb', line 172

def groups;    get_collection('groups');    end

#loansObject



166
# File 'lib/buxfer/base.rb', line 166

def loans;     get_collection('loans');     end

#remindersObject



170
# File 'lib/buxfer/base.rb', line 170

def reminders; get_collection('reminders'); end

#reports(options = {}) ⇒ Object

Return a Buxfer::Report object.

www.buxfer.com/help.php?topic=API#reports



147
148
149
# File 'lib/buxfer/base.rb', line 147

def reports(options = {})
  Buxfer::Report.new(get('/reports.xml', auth_query(options))['response'])
end

#tagsObject

Returns an array of Buxfer::Tag objects

www.buxfer.com/help.php?topic=API#tags



161
162
163
# File 'lib/buxfer/base.rb', line 161

def tags
  get_collection 'tags', :class => Buxfer::Tag
end

#transactions(options = {}) ⇒ Object

Return an array of the last 25 transactions.

Valid options:

  • :account - A Buxfer::Account or account name

  • :tag - A Buxfer::Tag object or tag name

  • :startDate - Date in format ‘10 feb 2008’ or ‘2008-02-10’

  • :endDate - Date in format ‘10 feb 2008’ or ‘2008-02-10’

  • :month - Month name and year in short format ‘feb 08’

www.buxfer.com/help.php?topic=API#transactions



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/buxfer/base.rb', line 130

def transactions(options = {})
  options.symbolize_keys!

  if  = options.delete(:account)
    options[:accountName] = .is_a?(Buxfer::Account) ? .name : 
  end

  if tag = options.delete(:tag)
    options[:tagName] = tag.is_a?(Buxfer::Tag) ? tag.name : tag
  end

  get_collection('transactions', options)
end

#upload_statement(account, statement, date_format = 'DD/MM/YYYY') ⇒ Object

Upload a file containing a transaction statement to Buxfer account. The account specified can be a Buxfer::Account (see #accounts) or an account id string.

The statement can be a String or an IO object.

An optional date format can be passed indicating if the dates used in the statement are in the format ‘DD/MM/YYYY’ (default) or ‘MM/DD/YYYY’

Example:

 = Buxfer.accounts.first
Buxfer.upload_statement(, open('/path/to/file')) => true

www.buxfer.com/help.php?topic=API#upload_statement



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/buxfer/base.rb', line 103

def upload_statement(, statement, date_format = 'DD/MM/YYYY')
   = .is_a?(Buxfer::Account) ? .id : 
  
  unless statement.is_a?(String)
    if statement.respond_to?(:read)
      statement = statement.read
    else
      statement = statement.to_s
    end
  end

  options = {:accountId => , :statement => statement, :dateFormat => date_format}

  self.class.post('/upload_statement.xml', auth_query(options, :body))
end