Class: WealthForge3::Connection

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

Class Method Summary collapse

Class Method Details

.connectionObject



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

def self.connection
  Faraday.new(url: WealthForge3.configuration.api_url) do |faraday|
    faraday.request :url_encoded
    faraday.options.timeout = 15
    faraday.options.open_timeout = 15
    faraday.use CustomErrors
    faraday.adapter Faraday.default_adapter
  end
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 WealthForge3::ApiException.new(e)
  end
  JSON.parse(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 WealthForge3::ApiException.new(e)
  end
  JSON.parse(response.body)
end