Class: RubyMint

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_mint.rb,
lib/ruby_mint/version.rb

Constant Summary collapse

JSON_HEADERS =
{ "accept" => "application/json" }
ACCOUNT_TYPES =
[
  "BANK",
  "CREDIT",
  "INVESTMENT",
  "LOAN",
  "MORTGAGE",
  "OTHER_PROPERTY",
  "REAL_ESTATE",
  "VEHICLE",
  "UNCLASSIFIED",
]
VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ RubyMint

Initialize RubyMint

Parameters:

  • username (String)

    usually an email address

  • password (String)


26
27
28
29
30
31
32
# File 'lib/ruby_mint.rb', line 26

def initialize(username, password)
  @username = username
  @password = password
  @request_id = 34

  @token = nil
end

Instance Method Details

#accounts(account_types = ACCOUNT_TYPES) ⇒ Hash

Get account data

Parameters:

  • account_types (Array<String>) (defaults to: ACCOUNT_TYPES)

    Type of accounts to retrieve. Defaults to all types.

Returns:

  • (Hash)

Raises:



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
# File 'lib/ruby_mint.rb', line 103

def accounts( = ACCOUNT_TYPES)
  # Use a new request_id
  @request_id += 1

   = {
    "input" => JSON.dump([{
      "args" => { "types" =>  },
      "id" => @request_id.to_s,
      "service" => "MintAccountService",
      "task" => "getAccountsSorted",
    }])}

  # Use token to get list of accounts
  results = agent.post("https://wwws.mint.com/bundledServiceController.xevent?legacy=false&token=#{@token}", , JSON_HEADERS)
  raise RubyMintError.new("Unable to obtain account information. Response code: #{results.code}") if results.code != "200"

  raise RubyMintError.new("Not logged in.") if results.body.include?('Session has expired.')

   = JSON.load(results.body)
  if ! || !["response"] || !["response"][@request_id.to_s] || !["response"][@request_id.to_s]["response"]
    raise RubyMintError.new("Unable to obtain account information (no account information in response).")
  end

  ["response"][@request_id.to_s]["response"]
end

#initiate_account_refresh(sleep_time = 3) ⇒ Object

Request that Mint.com refresh its account and transaction data

Parameters:

  • sleep_time (Integer) (defaults to: 3)

    Num of seconds to wait between calls to refreshing? when block is passed

  • block (Block)

    Code to execute upon completion of of refreshing



79
80
81
82
83
84
85
# File 'lib/ruby_mint.rb', line 79

def (sleep_time = 3)
  agent.post("https://wwws.mint.com/refreshFILogins.xevent", { "token" => @token }, JSON_HEADERS)
  if block_given?
    loop{ sleep sleep_time; break if !refreshing? }
    yield
  end
end

#logged_in?Boolean

Check if user is logged in already by the presence of a token.

Returns:

  • (Boolean)


71
72
73
# File 'lib/ruby_mint.rb', line 71

def logged_in?
  !@token.nil?
end

#login(use_token = nil) ⇒ Object

Login retrieves a user token from Mint.com

Parameters:

  • use_token (String) (defaults to: nil)

    Use an existing token

Raises:



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
# File 'lib/ruby_mint.rb', line 37

def (use_token = nil)
  @token = use_token and return if use_token

  response = agent.get("https://wwws.mint.com/login.event?task=L")
  raise RubyMintError.new("Unable to GET Mint login page.") if response.code != "200"

  response = agent.post("https://wwws.mint.com/getUserPod.xevent", { "username" => @username }, JSON_HEADERS)
  raise RubyMintError.new("Unable to POST to getUserPod.") if response.code != "200"

  query = {
    "username" => @username,
    "password" => @password,
    "task" => "L",
    "browser" => "firefox",
    "browserVersion" => "27",
    "os" => "Linux",
  }

  response = agent.post("https://wwws.mint.com/loginUserSubmit.xevent", query, JSON_HEADERS)
  if response.code != "200" || !response.body.include?("token")
    raise RubyMintError.new("Mint.com login failed. Response code: #{response.code}")
  end

   = JSON.load(response.body)
  if ! || !["sUser"] || !["sUser"]["token"]
    raise RubyMintError.new("Mint.com login failed (no token in login response body).")
  end

  @token = ["sUser"]["token"]
end

#refreshing?Boolean

Is Mint.com in the process of refreshing its data?

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/ruby_mint.rb', line 90

def refreshing?
  response = agent.get("https://wwws.mint.com/userStatus.xevent", JSON_HEADERS)
  if response.code != "200" || !response.body.include?("isRefreshing")
    raise RubyMintError.new("Unable to check if account is refreshing.")
  end

  JSON.parse(response.body)["isRefreshing"]
end

#transactions(start_date, end_date = Time.now, options = {}) ⇒ Object

Get transactions from mint. Returned as JSON. Paginate

Options:

include_pending [Boolean] default false
search_term     [String]  default ""

Parameters:

  • start_date (Time)

    get all transactions on or after this date

  • end_date (Time) (defaults to: Time.now)

    get all transactions up to and including this date

  • options (Hash) (defaults to: {})

    options hash



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
# File 'lib/ruby_mint.rb', line 151

def transactions(start_date, end_date = Time.now, options = {})
  include_pending = options.fetch('include_pending', false)
  search_term     = options.fetch('search_term', '')
  offset = 0
  results = []

  # Convert start and end dates 
  start_date = Time.local(start_date.year, start_date.month, start_date.day)
  end_date = Time.local(end_date.year, end_date.month, end_date.day)

  loop do
    next_page = transaction_page(offset, search_term)
    break if next_page.empty?

    # Filter out pending transactions
    if !include_pending
      next_page.reject!{ |t| t['isPending'] }
    end

    results.concat next_page
    break if earliest_mint_date(next_page) < start_date

    offset += next_page.count
  end

  # Filter by date
  results.select do |t|
    t['date'] >= start_date && t['date'] <= end_date
  end
end

#transactions_csvString

Get transactions from mint. They are returned as CSV and include ALL the transactions available

Returns:

  • (String)

    CSV of all transactions

Raises:



133
134
135
136
137
138
139
# File 'lib/ruby_mint.rb', line 133

def transactions_csv
  results = agent.get("https://wwws.mint.com/transactionDownload.event", JSON_HEADERS)
  raise RubyMintError.new("Unable to obtain transactions.") if results.code != "200"
  raise RubyMintError.new("Non-CSV content returned.") if !results.header["content-type"].include?("text/csv")

  results.body
end