Class: WealthForge::Connection
- Inherits:
-
Object
- Object
- WealthForge::Connection
- Defined in:
- lib/wealthforge/connection.rb
Class Method Summary collapse
- .connection ⇒ Object
- .get(endpoint, params, raw = false) ⇒ Object
- .post(endpoint, params) ⇒ Object
- .prep_params(params) ⇒ Object
- .put(endpoint, params) ⇒ Object
- .ssl_options ⇒ Object
Class Method Details
.connection ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/wealthforge/connection.rb', line 66 def self.connection api_endpoint = (!WealthForge.configuration.environment.nil? and WealthForge.configuration.environment.eql? 'production') ? 'https://www.capitalforge.com/capitalforge-transaction/api/' : 'https://sandbox.capitalforge.com/capitalforge-transaction/api/' return Faraday.new(:url => api_endpoint, :ssl => ) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter end end |
.get(endpoint, params, raw = false) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wealthforge/connection.rb', line 39 def self.get(endpoint, params, raw=false) begin response = connection.get do |req| req.url endpoint req.headers['Content-Type'] = 'application/json' req.body = prep_params(params) end raw == false ? JSON.parse(response.body, symbolize_names: true) : response.body rescue => e raise WealthForge::ApiException.new(e) end 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 = prep_params(params) end JSON.parse(response.body, symbolize_names: true) rescue => e raise WealthForge::ApiException.new(e) end end |
.prep_params(params) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/wealthforge/connection.rb', line 56 def self.prep_params(params) return if params.nil? wf_params = {} params.each do |key, value| wf_params[key.to_s.camelize(:lower)] = value end wf_params.to_json end |
.put(endpoint, params) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/wealthforge/connection.rb', line 25 def self.put(endpoint, params) begin response = connection.put do |req| req.url endpoint req.headers['Content-Type'] = 'application/json' req.body = prep_params(params) end JSON.parse(response.body, symbolize_names: true) rescue => e raise WealthForge::ApiException.new(e) end end |
.ssl_options ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/wealthforge/connection.rb', line 77 def self. wf_cert = !WealthForge.configuration.wf_crt.nil? ? WealthForge.configuration.wf_crt : File.read(WealthForge.configuration.wf_crt_file) wf_key = !WealthForge.configuration.wf_key.nil? ? WealthForge.configuration.wf_key : File.read(WealthForge.configuration.wf_key_file) = { :version => :TLSv1, :client_cert => OpenSSL::X509::Certificate.new(wf_cert), :client_key => OpenSSL::PKey::RSA.new(wf_key) } if WealthForge.configuration.environment.eql? 'production' [:ca_file] = ENV['CA_FILE'] else [:ca_path] = '/usr/lib/ssl/certs' end end |