Class: WealthForge::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/wealthforge/connection.rb

Class Method Summary collapse

Class Method Details

.connectionObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wealthforge/connection.rb', line 62

def self.connection
  set_token
  Faraday.new(url: @api_url) do |faraday|
    faraday.request :url_encoded
    faraday.options.timeout = 5
    faraday.options.open_timeout = 5
    faraday.headers["Authorization"] = @wf_token
    faraday.use CustomErrors
    faraday.adapter Faraday.default_adapter
  end
end

.connection_multipartObject



74
75
76
77
78
79
80
81
82
# File 'lib/wealthforge/connection.rb', line 74

def self.connection_multipart
  set_token
  Faraday.new(url: @api_url) do |faraday|
    faraday.request :multipart
    faraday.headers["Authorization"] = @wf_token
    faraday.use CustomErrors
    faraday.adapter Faraday.default_adapter
  end
end

.file_upload(endpoint, file, filename, mime_type) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wealthforge/connection.rb', line 49

def self.file_upload(endpoint, file, filename, mime_type)
  payload = {file: Faraday::UploadIO.new(file, mime_type, filename)}
  begin
    response = connection_multipart.post do |req|
      req.url endpoint
      req.body = payload
    end
  rescue => e
    raise WealthForge::ApiException.new(e)
  end
  JSON.parse(response.body)
end

.get(endpoint, params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wealthforge/connection.rb', line 23

def self.get(endpoint, params)
  begin
    response = connection.get do |req|
      req.url endpoint
      req.headers["Content-Type"] = "application/json"
      req.body = params.to_json
    end
  rescue => e
    raise WealthForge::ApiException.new(e)
  end
  JSON.parse(response.body)
end

.patch(endpoint, params) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wealthforge/connection.rb', line 36

def self.patch(endpoint, params)
  begin
    response = connection.patch do |req|
      req.url endpoint
      req.headers["Content-Type"] = "application/json"
      req.body = params.to_json
    end
  rescue => e
    raise WealthForge::ApiException.new(e)
  end
  response.body
end

.post(endpoint, params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/wealthforge/connection.rb', line 10

def self.post(endpoint, params)
  begin
    response = connection.post do |req|
      req.url endpoint
      req.headers["Content-Type"] = "application/json"
      req.body = params.to_json
    end
  rescue => e
    raise WealthForge::ApiException.new(e)
  end
  JSON.parse(response.body)
end

.retrieve_tokenObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wealthforge/connection.rb', line 108

def self.retrieve_token
  auth = Faraday.new.post(@token_url) do |faraday|
    faraday.body = {
      data: {
        attributes: {
          clientId: @wf_client_id,
          clientSecret: @wf_client_secret
        },
        type: "token"
      }
    }.to_json
  end.body
  @wf_token = "Bearer " + JSON.parse(auth)["data"]["attributes"]["accessToken"]
end

.set_tokenObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wealthforge/connection.rb', line 84

def self.set_token
  if @wf_token.nil?
    @wf_client_id = WealthForge.configuration.client_id
    @wf_client_secret = WealthForge.configuration.client_secret
    @api_url = WealthForge.configuration.api_url
    @token_url = WealthForge.configuration.token_url
    @wf_token = retrieve_token
  end

  # test to see if token has expired
  t = @wf_token.reverse.chomp("Bearer ".reverse).reverse
  decoded_token = JWT.decode t, nil, false
  token_exp = decoded_token[0]["exp"]
  leeway = 60
  now = Time.now.to_i
  token_window = token_exp - leeway
  refresh_token = !(token_window > now)

  if refresh_token == true
    # makes a call to get a new token
    @wf_token = retrieve_token
  end
end