Class: WealthForge::Connection

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

Class Method Summary collapse

Class Method Details

.connectionObject



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 => ssl_options) 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_optionsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wealthforge/connection.rb', line 77

def self.ssl_options
  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)
  ssl_options = {
    :version => :TLSv1,
    :client_cert => OpenSSL::X509::Certificate.new(wf_cert),
    :client_key  => OpenSSL::PKey::RSA.new(wf_key)
  }
  if WealthForge.configuration.environment.eql? 'production'
    ssl_options[:ca_file] = ENV['CA_FILE']
  else
    ssl_options[:ca_path] = '/usr/lib/ssl/certs'
  end
  ssl_options
end