Class: OnSIP::Connection

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

Constant Summary collapse

USER_AGENT =
"onsip-client v#{OnSIP::VERSION}"
DEFAULT_OPTIONS =
{
  :log_response_headers => false,
  :log_response_body => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
# File 'lib/onsip/connection.rb', line 12

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @faraday = self.create_faraday(options[:uri])
end

Instance Attribute Details

#faradayObject

Returns the value of attribute faraday.



10
11
12
# File 'lib/onsip/connection.rb', line 10

def faraday
  @faraday
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/onsip/connection.rb', line 10

def options
  @options
end

Instance Method Details

#config_request(request, method, path, params, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/onsip/connection.rb', line 50

def config_request(request, method, path, params, options)
  request.headers['Content-Type'] = 'application/json'

  case method.to_sym
  when :delete, :get
    request.url(path, params)
  when :post, :put
    request.path = path
    request.body = MultiJson.dump(params) unless params.empty?
  end

  request
end

#create_faraday(uri) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/onsip/connection.rb', line 17

def create_faraday(uri)
  @faraday = Faraday.new uri do |c|
    c.headers['User-Agent'] = USER_AGENT

    c.request :multipart
    c.request :url_encoded

    c.response :json,  :content_type => /\bjson$/
    c.response :mashify
    c.response :logger, OnSIP.logger if @options[:log_response_headers]

    c.use :instrumentation
    c.adapter Faraday.default_adapter
  end
end

#delete(path, params = {}, options = {}, &callback) ⇒ Object



68
69
70
# File 'lib/onsip/connection.rb', line 68

def delete(path, params={}, options={}, &callback)
  request(:delete, path, params, options, &callback)
end

#get(path, params = {}, options = {}, &callback) ⇒ Object



64
65
66
# File 'lib/onsip/connection.rb', line 64

def get(path, params={}, options={}, &callback)
  request(:get, path, params, options, &callback)
end

#post(path, params = {}, options = {}, &callback) ⇒ Object



72
73
74
# File 'lib/onsip/connection.rb', line 72

def post(path, params={}, options={}, &callback)
  request(:post, path, params, options, &callback)
end

#put(path, params = {}, options = {}, &callback) ⇒ Object



76
77
78
# File 'lib/onsip/connection.rb', line 76

def put(path, params={}, options={}, &callback)
  request(:put, path, params, options, &callback)
end

#request(method, path, params, options, &callback) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/onsip/connection.rb', line 33

def request(method, path, params, options, &callback)
  sent_at = nil

  response = @faraday.send(method) { |request|
    sent_at = Time.now
    request = config_request(request, method, path, params, options)
  }.on_complete { |env|
    env[:total_time] = Time.now.utc.to_f - sent_at.utc.to_f if sent_at
    env[:request_params] = params
    env[:request_options] = options
    OnSIP.logger.debug env.body if @options[:log_response_body]
    callback.call(env) if callback
  }

  response
end