Class: WealthForge::Connection
- Inherits:
-
Object
- Object
- WealthForge::Connection
- Defined in:
- lib/wealthforge/connection.rb
Class Method Summary collapse
- .connection ⇒ Object
- .connection_multipart ⇒ Object
- .file_upload(endpoint, file, filename, mime_type) ⇒ Object
- .get(endpoint, params) ⇒ Object
- .post(endpoint, params) ⇒ Object
- .retrieve_token ⇒ Object
- .set_token ⇒ Object
Class Method Details
.connection ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/wealthforge/connection.rb', line 53 def self.connection set_token return Faraday.new(:url => @api_url) do |faraday| faraday.request :url_encoded faraday.headers['Authorization'] = @wf_token faraday.adapter Faraday.default_adapter faraday.use CustomErrors end end |
.connection_multipart ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/wealthforge/connection.rb', line 64 def self.connection_multipart set_token return Faraday.new(:url => @api_url) do |faraday| faraday.request :multipart faraday.headers['Authorization'] = @wf_token faraday.adapter Faraday.default_adapter faraday.use CustomErrors end end |
.file_upload(endpoint, file, filename, mime_type) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wealthforge/connection.rb', line 39 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
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/wealthforge/connection.rb', line 25 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 |
.post(endpoint, params) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/wealthforge/connection.rb', line 11 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 ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/wealthforge/connection.rb', line 100 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 ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/wealthforge/connection.rb', line 75 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 |