Class: MFYNAB::MoneyForward::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/mfynab/money_forward/session.rb

Constant Summary collapse

"_moneybook_session"
DEFAULT_BASE_URL =
"https://moneyforward.com"
SIGNIN_PATH =
"/sign_in"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:, password:, logger:, base_url: DEFAULT_BASE_URL) ⇒ Session

Returns a new instance of Session.



14
15
16
17
18
19
# File 'lib/mfynab/money_forward/session.rb', line 14

def initialize(username:, password:, logger:, base_url: DEFAULT_BASE_URL)
  @username = username
  @password = password
  @logger = logger
  @base_url = URI(base_url)
end

Instance Attribute Details



33
34
35
# File 'lib/mfynab/money_forward/session.rb', line 33

def cookie
  @cookie || 
end

Instance Method Details

#csrf_tokenObject

FIXME: not sure a CSRF token is generic to the session Maybe it has different instances depending on the page it’s on?



56
57
58
59
# File 'lib/mfynab/money_forward/session.rb', line 56

def csrf_token
  @_csrf_token ||= Nokogiri::HTML(http_get("/accounts"))
    .at_css("meta[name='csrf-token']")[:content]
end

#http_get(path, params = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/mfynab/money_forward/session.rb', line 37

def http_get(path, params = {})
  path = URI.join(base_url, path)
  path.query = URI.encode_www_form(params) unless params.empty?
  request = Net::HTTP::Get.new(path, "Cookie" => "#{COOKIE_NAME}=#{cookie.value}")
  http_request(request)
end

#http_post(path, body, headers = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/mfynab/money_forward/session.rb', line 44

def http_post(path, body, headers = {})
  request = Net::HTTP::Post.new(
    path,
    "Cookie" => "#{COOKIE_NAME}=#{cookie.value}",
    **headers,
  )
  request.body = body
  http_request(request)
end

#loginObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mfynab/money_forward/session.rb', line 21

def 
  logger.info("Logging in to Money Forward...")
  with_ferrum do |browser|
    (browser)

    self.cookie = browser.cookies[COOKIE_NAME]
  rescue Timeout::Error
    # FIXME: use custom error class
    raise "Login failed"
  end
end