Class: Sipwizard::Connection

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

Constant Summary collapse

BASE_REST_API =
"https://www.sipsorcery.com/rest/v0.1"
API_PATHS =
{
  provisioning: "#{BASE_REST_API}/provisioning.svc",
  accounting: "#{BASE_REST_API}/accounting.svc",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(options={})
  faraday_adapter = options.fetch(:adapter){ Faraday.default_adapter }
  api_type        = options.fetch(:api_type){ :provisioning }
  params          = options.fetch(:connection_params){{}}
  debug_enabled   = options.fetch(:debug_enabled){ false }
  @faraday_connection = Faraday.new(API_PATHS[api_type], params ) do |faraday|
    faraday.request :url_encoded #for post/put params

    faraday.response :logger if debug_enabled
    faraday.response :raise_error
    faraday.response :json, content_type: /\bjson\z/

    faraday.adapter faraday_adapter
  end

  @faraday_connection.headers['apikey'] = Sipwizard.config.api_key
end

Instance Attribute Details

#faraday_connectionObject

Returns the value of attribute faraday_connection.



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

def faraday_connection
  @faraday_connection
end

Instance Method Details

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

Public make a get request to the api

path - The path of the api params - The query parameters (default: {}):

Examples

get('cdr/count')

get('cdr/count', { where: 'FromName="John Doe"'})

Returns Faraday::Response



45
46
47
# File 'lib/sipwizard/connection.rb', line 45

def get(path, params={})
  self.faraday_connection.get(path, params).body
end

#post(path, params) ⇒ Object



49
50
51
52
53
# File 'lib/sipwizard/connection.rb', line 49

def post(path, params)
  self.faraday_connection.headers['Content-Type'] ='application/json; charset=utf-8'
  payload = params.to_json
  self.faraday_connection.post(path, payload).body
end