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.1.0"
Instance Method Summary collapse
-
#accounts(account_types = ACCOUNT_TYPES) ⇒ Object
Get account data.
-
#initialize(username, password) ⇒ RubyMint
constructor
Initialize RubyMint.
-
#initiate_account_refresh(sleep_time = 3, &block) ⇒ 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 ⇒ Object
Login retrieves a user token from Mint.com.
-
#refreshing? ⇒ Boolean
Is Mint.com in the process of refreshing its data?.
-
#transactions ⇒ Object
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) ⇒ Object
Get account data
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ruby_mint.rb', line 99 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" 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, &block) ⇒ Object
Request that Mint.com refresh its account and transaction data
75 76 77 78 79 80 81 |
# File 'lib/ruby_mint.rb', line 75 def initiate_account_refresh(sleep_time = 3, &block) agent.post("https://wwws.mint.com/refreshFILogins.xevent", { "token" => @token }, JSON_HEADERS) if block 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.
67 68 69 |
# File 'lib/ruby_mint.rb', line 67 def logged_in? !@token.nil? end |
#login ⇒ Object
Login retrieves a user token from Mint.com
35 36 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 |
# File 'lib/ruby_mint.rb', line 35 def login 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?
86 87 88 89 90 91 92 93 |
# File 'lib/ruby_mint.rb', line 86 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 ⇒ Object
Get transactions from mint. They are returned as CSV and include ALL the transactions available
127 128 129 130 131 132 133 |
# File 'lib/ruby_mint.rb', line 127 def transactions results = agent.get("https://wwws.mint.com/transactionDownload.event", JSON_HEADERS) raise RubyMintError.new("Unable to obtain transations.") if results.code != "200" raise RubyMintError.new("Non-CSV content returned.") if !results.header["content-type"].include?("text/csv") results.body end |