Class: MFYNAB::MoneyForward::Session
- Inherits:
-
Object
- Object
- MFYNAB::MoneyForward::Session
- Defined in:
- lib/mfynab/money_forward/session.rb
Constant Summary collapse
- COOKIE_NAME =
"_moneybook_session"- DEFAULT_BASE_URL =
"https://moneyforward.com"- SIGNIN_PATH =
"/sign_in"
Instance Attribute Summary collapse
- #cookie ⇒ Object readonly
Instance Method Summary collapse
-
#csrf_token ⇒ Object
FIXME: not sure a CSRF token is generic to the session Maybe it has different instances depending on the page it’s on?.
- #http_get(path, params = {}) ⇒ Object
- #http_post(path, body, headers = {}) ⇒ Object
-
#initialize(username:, password:, logger:, base_url: DEFAULT_BASE_URL) ⇒ Session
constructor
A new instance of Session.
- #login ⇒ Object
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
#cookie ⇒ Object
33 34 35 |
# File 'lib/mfynab/money_forward/session.rb', line 33 def || login end |
Instance Method Details
#csrf_token ⇒ Object
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 |
#login ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mfynab/money_forward/session.rb', line 21 def login logger.info("Logging in to Money Forward...") with_ferrum do |browser| submit_login_form(browser) self. = browser.[COOKIE_NAME] rescue Timeout::Error # FIXME: use custom error class raise "Login failed" end end |