Class: RubyMint
- Inherits:
-
Object
- Object
- RubyMint
- 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
-
#accounts(account_types = ACCOUNT_TYPES) ⇒ Hash
Get account data.
-
#initialize(username, password) ⇒ RubyMint
constructor
Initialize RubyMint.
-
#initiate_account_refresh(sleep_time = 3) ⇒ Object
Request that Mint.com refresh its account and transaction data.
-
#logged_in? ⇒ Boolean
Check if user is logged in already by the presence of a token.
-
#login(use_token = nil) ⇒ Object
Login retrieves a user token from Mint.com.
-
#refreshing? ⇒ Boolean
Is Mint.com in the process of refreshing its data?.
-
#transactions(start_date, end_date = Time.now, options = {}) ⇒ Object
Get transactions from mint.
-
#transactions_csv ⇒ String
Get transactions from mint.
Constructor Details
#initialize(username, password) ⇒ RubyMint
Initialize RubyMint
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
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 = ACCOUNT_TYPES) # Use a new request_id @request_id += 1 account_query = { "input" => JSON.dump([{ "args" => { "types" => account_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}", account_query, 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.') account_body = JSON.load(results.body) if !account_body || !account_body["response"] || !account_body["response"][@request_id.to_s] || !account_body["response"][@request_id.to_s]["response"] raise RubyMintError.new("Unable to obtain account information (no account information in response).") end account_body["response"][@request_id.to_s]["response"] end |
#initiate_account_refresh(sleep_time = 3) ⇒ Object
Request that Mint.com refresh its account and transaction data
79 80 81 82 83 84 85 |
# File 'lib/ruby_mint.rb', line 79 def initiate_account_refresh(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.
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
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 login(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 login_body = JSON.load(response.body) if !login_body || !login_body["sUser"] || !login_body["sUser"]["token"] raise RubyMintError.new("Mint.com login failed (no token in login response body).") end @token = login_body["sUser"]["token"] end |
#refreshing? ⇒ Boolean
Is Mint.com in the process of refreshing its data?
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 ""
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, = {}) include_pending = .fetch('include_pending', false) search_term = .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_csv ⇒ String
Get transactions from mint. They are returned as CSV and include ALL the transactions available
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 |