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.1.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) ⇒ Object

Get account data

Parameters:

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

    Type of accounts to retrieve. Defaults to all types.

Raises:



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)
  # 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"

   = 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, &block) ⇒ 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



75
76
77
78
79
80
81
# File 'lib/ruby_mint.rb', line 75

def (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.

Returns:

  • (Boolean)


67
68
69
# File 'lib/ruby_mint.rb', line 67

def logged_in?
  !@token.nil?
end

#loginObject

Login retrieves a user token from Mint.com

Raises:



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 
  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)


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

#transactionsObject

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

Raises:



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