Class: Iro::ApplicationController

Inherits:
Wco::ApplicationController
  • Object
show all
Defined in:
app/controllers/iro/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#homeObject



18
19
20
# File 'app/controllers/iro/application_controller.rb', line 18

def home
  authorize! :home, Iro
end

#schwab_syncObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/iro/application_controller.rb', line 22

def schwab_sync
  authorize! :shwab_sync, Iro
  profile = Wco::Profile.find_by email: 'piousbox@gmail.com'

  out = Schwab.post( "https://api.schwabapi.com/v1/oauth/token", {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    basic_auth: { username: SCHWAB_DATA[:key], password: SCHWAB_DATA[:secret] },
    body: {
      grant_type: 'refresh_token',
      refresh_token: profile.schwab_refresh_token
    },
  })
  out = out.parsed_response
  # puts! out, 'out'

  attrs = {
    schwab_access_token:  out['access_token'],
    schwab_refresh_token: out['refresh_token'],
    schwab_id_token:      out['id_token'],
  }
  # puts! attrs, 'attrs'

  if attrs[:schwab_refresh_token]
    profile.update(attrs)
    profile.save!
  end

  render json: { attrs: attrs, out: out }
end