Class: Callstacking::Rails::Client::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/callstacking/rails/client/base.rb

Direct Known Subclasses

Authenticate, Trace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, auth_token) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
# File 'lib/callstacking/rails/client/base.rb', line 13

def initialize(url, auth_token)
  @url = url
  @auth_token = auth_token

  @threads = []
  @async   = false
end

Instance Attribute Details

#asyncObject

Returns the value of attribute async.



10
11
12
# File 'lib/callstacking/rails/client/base.rb', line 10

def async
  @async
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



11
12
13
# File 'lib/callstacking/rails/client/base.rb', line 11

def auth_token
  @auth_token
end

#threadsObject

Returns the value of attribute threads.



10
11
12
# File 'lib/callstacking/rails/client/base.rb', line 10

def threads
  @threads
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/callstacking/rails/client/base.rb', line 11

def url
  @url
end

Instance Method Details

#connectionObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/callstacking/rails/client/base.rb', line 20

def connection
  # https://github.com/lostisland/awesome-faraday
  @connection ||= Faraday.new(url) do |c|
    c.response :json
    c.response :follow_redirects
    c.use Faraday::Response::RaiseError # raise exceptions on 40x, 50x responses
    c.request :json # This will set the "Content-Type" header to application/json and call .to_json on the body
    c.adapter Faraday.default_adapter
    c.options.timeout = 120

    if auth_token.present?
      c.request :authorization, :Bearer, auth_token
    end
  end
end

#get(url, params = {}, headers = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/callstacking/rails/client/base.rb', line 36

def get(url, params = {}, headers = {})
  if async
    threads << Thread.new do
      connection.get(url, params, headers)
    end
  else
    connection.get(url, params, headers)
  end
ensure
  Faraday.default_connection.close if async
end

#patch(url, params = {}, body = {}, headers_ext = {}) ⇒ Object



52
53
54
# File 'lib/callstacking/rails/client/base.rb', line 52

def patch(url, params = {}, body = {}, headers_ext = {})
  r(:patch, url, params, body, headers_ext)
end

#post(url, params = {}, body = {}, headers_ext = {}) ⇒ Object



48
49
50
# File 'lib/callstacking/rails/client/base.rb', line 48

def post(url, params = {}, body = {}, headers_ext = {})
  r(:post, url, params, body, headers_ext)
end

#r(action, url, params = {}, body = {}, _headers_ext = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/callstacking/rails/client/base.rb', line 56

def r(action, url, params = {}, body = {}, _headers_ext = {})
  if async
    threads << Thread.new do
      connection.send(action, url) do |req|
        req.params.merge!(params)
        req.body = body
      end
    end
  else
    connection.send(action, url) do |req|
      req.params.merge!(params)
      req.body = body
    end
  end
ensure
  Faraday.default_connection.close if async
end