class WealthForge::Connection
Public Class Methods
connection()
click to toggle source
# 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_multipart()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_token()
click to toggle source
# 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_token()
click to toggle source
# 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